Fawkes API  Fawkes Development Version
utils.h
1 
2 /***************************************************************************
3  * utils.h - PLEXIL utils
4  *
5  * Created: Fri Aug 17 16:15:54 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef __PLUGINS_PLEXIL_UTILS_H_
23 #define __PLUGINS_PLEXIL_UTILS_H_
24 
25 #include <Debug.hh>
26 #include <Error.hh>
27 #include <Value.hh>
28 #include <cstring>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 inline void
34 replace_tokens(std::string &s)
35 {
36  std::map<std::string, std::string> tokens = {{"@CFGDIR@", CONFDIR},
37  {"@BASEDIR@", BASEDIR},
38  {"@FAWKES_BASEDIR@", FAWKES_BASEDIR},
39  {"@RESDIR@", RESDIR}};
40 
41  for (const auto &token : tokens) {
42  std::string::size_type pos;
43  if ((pos = s.find(token.first)) != std::string::npos) {
44  s.replace(pos, token.first.size(), token.second);
45  }
46  }
47 }
48 
49 inline bool
50 verify_args(const std::vector<PLEXIL::Value> & args,
51  const std::string & func,
52  const std::vector<std::pair<std::string, PLEXIL::ValueType>> &types)
53 {
54  if (args.size() != types.size()) {
55  warn(func << ": Command requires " << types.size() << " arguments, got " << args.size());
56  for (size_t i = 0; i < args.size(); ++i) {
57  warn(func << ": Argument " << i << " = " << args[i]);
58  }
59  return false;
60  }
61  for (size_t i = 0; i < args.size(); ++i) {
62  // Treat UNKNOWN_TYPE as "any type and we don't care/inspect later"
63  if (types[i].second == PLEXIL::UNKNOWN_TYPE)
64  continue;
65 
66  if (args[i].valueType() != types[i].second) {
67  warn(func << ":"
68  << "Command argument " << i << "(" << types[i].first << ") expected to be of type "
69  << PLEXIL::valueTypeName(types[i].second) << ", but is of type "
70  << PLEXIL::valueTypeName(args[i].valueType()));
71  return false;
72  }
73  }
74  return true;
75 }
76 
77 /** Get configuration value from XML adapter config.
78  * Value name can either reference an attribute name, or a <Parameter> tag with
79  * the key attribte set to the given value. The attribute is preferred.
80  * @param config configuration to query, typically retrieved as getXml()
81  * @param name name/key of configuration value
82  * @return value as string
83  */
84 inline std::string
85 get_xml_config_value(const pugi::xml_node &config, const std::string &name)
86 {
87  pugi::xml_attribute xml_attr = config.attribute(name.c_str());
88  if (xml_attr) {
89  return xml_attr.value();
90  } else {
91  for (const auto &c : config.children()) {
92  if (strcmp(c.name(), "Parameter") == 0) {
93  pugi::xml_attribute xml_key_attr = c.attribute("key");
94  if (xml_key_attr && strcmp(xml_key_attr.value(), name.c_str()) == 0) {
95  return c.text().get();
96  }
97  }
98  }
99  }
100 
101  return "";
102 }
103 
104 #endif