gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
yaml_node_utility.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
17 
18 #include <cstring>
19 #include <regex>
20 
22 #include <cstring>
23 
24 namespace Gambit
25 {
26 
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  }
60 
62  void NodeUtility::removeCharsFromString(std::string& text, const char* charsToRemove)
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  }
69 
72  std::string NodeUtility::expandEnvironmentVariables(const std::string& input)
73  {
74  static const char* escape_character = "\\";
75  std::string text = input;
77  NodeUtility::removeCharsFromString(text, escape_character);
78  return text;
79  }
80 
81 }
void removeCharsFromString(std::string &text, const char *charsToRemove)
Remove characters in the given string.
Funk var(std::string arg)
Definition: daFunk.hpp:921
std::string expandEnvironmentVariables(const std::string &input)
Leave input alone and return new string, which has environment variables substituted and escpae chara...
void autoExpandEnvironmentVariables(std::string &text)
Expand environment variables in the given string.
Wrapper functionality to get yaml nodes with some extras.
TODO: see if we can use this one:
Definition: Analysis.hpp:33