gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
Gambit::NodeUtility Namespace Reference

Functions

template<class TYPE >
TYPE safeIntegerTypeCast (const std::string &s)
 Wrapper for integer type casts from a double in string representation. More...
 
void autoExpandEnvironmentVariables (std::string &text)
 Expand environment variables in the given string. More...
 
void removeCharsFromString (std::string &text, const char *charsToRemove)
 Remove characters in the given string. More...
 
std::string expandEnvironmentVariables (const std::string &input)
 Leave input alone and return new string, which has environment variables substituted and escpae characters removed. More...
 
template<class TYPE >
TYPE getNode (const YAML::Node node)
 Wrapper for reading the node for a given type. More...
 
template<>
int getNode< int > (const YAML::Node node)
 Allows to read scientific notation integer numbers. More...
 
template<>
unsigned int getNode< unsigned int > (const YAML::Node node)
 See int specialization. More...
 
template<>
long getNode< long > (const YAML::Node node)
 See int specialization. More...
 
template<>
unsigned long getNode< unsigned long > (const YAML::Node node)
 See int specialization. More...
 
template<>
long long getNode< long long > (const YAML::Node node)
 See int specialization. More...
 
template<>
unsigned long long getNode< unsigned long long > (const YAML::Node node)
 See int specialization. More...
 
template<>
std::string getNode< std::string > (const YAML::Node node)
 Read string and expand environment variables of the type ${MYVAR}. More...
 

Function Documentation

◆ autoExpandEnvironmentVariables()

void Gambit::NodeUtility::autoExpandEnvironmentVariables ( std::string &  text)

Expand environment variables in the given string.

Definition at line 28 of file yaml_node_utility.cpp.

References daFunk::var().

Referenced by expandEnvironmentVariables(), and safeIntegerTypeCast().

29  {
30  // C++ regex does not support negative lookahead. So let us reverse the string.
31  std::reverse(text.begin(), text.end());
32 
33  // Matches reverse ${MYVAR}, but not \${MYVAR} and $MYVAR
34  const static std::regex env( "\\}([^{]+)\\{\\$(?!\\\\)" );
35 
36  std::smatch match;
37  while (std::regex_search(text, match, env))
38  {
39  // Reverse the found match into a temporary variable, this is what we actually
40  // want to look for, e.g. we found RAV, but we want to look for VAR.
41  std::string tmp = match[1].str();
42  std::reverse(tmp.begin(), tmp.end());
43 
44  // Look for VAR
45  const char * s = std::getenv(tmp.c_str());
46 
47  std::string var(s == NULL ? "" : s);
48  if (s == NULL)
49  {
50  std::cout << "Environment variable " << match.str() << " not set";
51  }
52  // Reverse the found environment variable...
53  std::reverse(var.begin(), var.end());
54  // and plug it into the text string.
55  text.replace(match[0].first, match[0].second, var);
56  }
57  // Finally return the text string in the normal order.
58  std::reverse(text.begin(), text.end());
59  }
Funk var(std::string arg)
Definition: daFunk.hpp:921
Here is the call graph for this function:
Here is the caller graph for this function:

◆ expandEnvironmentVariables()

std::string Gambit::NodeUtility::expandEnvironmentVariables ( const std::string &  input)

Leave input alone and return new string, which has environment variables substituted and escpae characters removed.

Definition at line 72 of file yaml_node_utility.cpp.

References autoExpandEnvironmentVariables(), and removeCharsFromString().

Referenced by getNode< std::string >(), and safeIntegerTypeCast().

73  {
74  static const char* escape_character = "\\";
75  std::string text = input;
77  NodeUtility::removeCharsFromString(text, escape_character);
78  return text;
79  }
void removeCharsFromString(std::string &text, const char *charsToRemove)
Remove characters in the given string.
void autoExpandEnvironmentVariables(std::string &text)
Expand environment variables in the given string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNode()

template<class TYPE >
TYPE Gambit::NodeUtility::getNode ( const YAML::Node  node)

Wrapper for reading the node for a given type.

Default case does nothing. However in some instances we want to catch the yamlcpp exception and try to interpret it, e.g. scientific notation numbers as integers.

Definition at line 92 of file yaml_node_utility.hpp.

92 { return node.as<TYPE>(); }

◆ getNode< int >()

template<>
int Gambit::NodeUtility::getNode< int > ( const YAML::Node  node)
inline

Allows to read scientific notation integer numbers.

If the number does not fit into the given type (here int) or is not an integer, this function will raise. This exception is then caught by getValue and handled.

Definition at line 98 of file yaml_node_utility.hpp.

99  {
100  try { return node.as<int>(); }
101  catch (...) { return safeIntegerTypeCast<int>(node.as<std::string>()); }
102  }

◆ getNode< long >()

template<>
long Gambit::NodeUtility::getNode< long > ( const YAML::Node  node)
inline

See int specialization.

Definition at line 114 of file yaml_node_utility.hpp.

115  {
116  try { return node.as<long>(); }
117  catch (...) { return safeIntegerTypeCast<long>(node.as<std::string>()); }
118  }

◆ getNode< long long >()

template<>
long long Gambit::NodeUtility::getNode< long long > ( const YAML::Node  node)
inline

See int specialization.

Definition at line 130 of file yaml_node_utility.hpp.

131  {
132  try { return node.as<long long>(); }
133  catch (...) { return safeIntegerTypeCast<long long>(node.as<std::string>()); }
134  }

◆ getNode< std::string >()

template<>
std::string Gambit::NodeUtility::getNode< std::string > ( const YAML::Node  node)
inline

Read string and expand environment variables of the type ${MYVAR}.

Expansion of environment variables is not performed if given as $MYVAR and ${MYVAR}.

Definition at line 148 of file yaml_node_utility.hpp.

References expandEnvironmentVariables().

149  {
150  return NodeUtility::expandEnvironmentVariables(node.as<std::string>());
151  }
std::string expandEnvironmentVariables(const std::string &input)
Leave input alone and return new string, which has environment variables substituted and escpae chara...
Here is the call graph for this function:

◆ getNode< unsigned int >()

template<>
unsigned int Gambit::NodeUtility::getNode< unsigned int > ( const YAML::Node  node)
inline

See int specialization.

Definition at line 106 of file yaml_node_utility.hpp.

107  {
108  try { return node.as<unsigned int>(); }
109  catch (...) { return safeIntegerTypeCast<unsigned int>(node.as<std::string>()); }
110  }

◆ getNode< unsigned long >()

template<>
unsigned long Gambit::NodeUtility::getNode< unsigned long > ( const YAML::Node  node)
inline

See int specialization.

Definition at line 122 of file yaml_node_utility.hpp.

123  {
124  try { return node.as<unsigned long>(); }
125  catch (...) { return safeIntegerTypeCast<unsigned long>(node.as<std::string>()); }
126  }

◆ getNode< unsigned long long >()

template<>
unsigned long long Gambit::NodeUtility::getNode< unsigned long long > ( const YAML::Node  node)
inline

See int specialization.

Definition at line 138 of file yaml_node_utility.hpp.

139  {
140  try { return node.as<unsigned long long>(); }
141  catch (...) { return safeIntegerTypeCast<unsigned long long>(node.as<std::string>()); }
142  }

◆ removeCharsFromString()

void Gambit::NodeUtility::removeCharsFromString ( std::string &  text,
const char *  charsToRemove 
)

Remove characters in the given string.

Definition at line 62 of file yaml_node_utility.cpp.

Referenced by expandEnvironmentVariables(), and safeIntegerTypeCast().

63  {
64  for (unsigned int i = 0; i < std::strlen(charsToRemove); ++i)
65  {
66  text.erase(std::remove(text.begin(), text.end(), charsToRemove[i]), text.end());
67  }
68  }
Here is the caller graph for this function:

◆ safeIntegerTypeCast()

template<class TYPE >
TYPE Gambit::NodeUtility::safeIntegerTypeCast ( const std::string &  s)

Wrapper for integer type casts from a double in string representation.

It does first try to safely convert the string to a double and then performs checks before casting to an integer type.

Definition at line 42 of file yaml_node_utility.hpp.

References autoExpandEnvironmentVariables(), expandEnvironmentVariables(), LOCAL_INFO, removeCharsFromString(), and Gambit::utils_error().

43  {
44  try
45  {
46  const double d = std::stod(s);
47  if (d != std::floor(d))
48  {
49  std::ostringstream os;
50  os << "Provided value " << d << " as option in the yaml file does not represent an integer.";
51  utils_error().raise(LOCAL_INFO, os.str());
52  }
53  if (std::numeric_limits<TYPE>::max() < d or std::numeric_limits<TYPE>::min() > d)
54  {
55  std::ostringstream os;
56  os << "Provided value " << d << " as option in the yaml file does not fit into the implemented integer type.";
57  utils_error().raise(LOCAL_INFO, os.str());
58  }
59  return static_cast<TYPE>(d);
60  }
61  catch (const std::out_of_range& e)
62  {
63  std::ostringstream os;
64  os << "Out of range error: " << e.what() << "\n";
65  os << "Provided value " << s << " as option in the yaml file does not fit into double.";
66  utils_error().raise(LOCAL_INFO, os.str());
67  }
68  catch (const std::invalid_argument& e)
69  {
70  std::ostringstream os;
71  os << "Invalid argument: " << e.what() << "\n";
72  os << "Provided value " << s << " as option in the yaml file can not be interpreted as double.";
73  utils_error().raise(LOCAL_INFO, os.str());
74  }
75  throw std::runtime_error("Reached end of function safeIntegerTypeCast. This should not happen.");
76  }
EXPORT_SYMBOLS error & utils_error()
Utility errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
Here is the call graph for this function: