string_utilities.cpp
Go to the documentation of this file.
1 
10 #include "string_utilities.h"
11 #include <cstdlib>
12 #include <regex.h>
13 
14 namespace argos {
15 
16  void Tokenize(const std::string& str_string,
17  std::vector<std::string>& vec_tokens,
18  const std::string& str_delimiters) {
19  // this taken from
20  // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
21 
22  /* Skip delimiters at beginning */
23  std::string::size_type lastPos =
24  str_string.find_first_not_of(str_delimiters, 0);
25 
26  /* Find first "non-delimiter" */
27  std::string::size_type pos = str_string.find_first_of(str_delimiters,
28  lastPos);
29 
30  while(std::string::npos != pos || std::string::npos != lastPos) {
31  /* Found a token, add it to the vector */
32  vec_tokens.push_back(str_string.substr(lastPos, pos - lastPos));
33 
34  /* Skip delimiters. Note the "not_of" */
35  lastPos = str_string.find_first_not_of(str_delimiters, pos);
36 
37  /* Find next "non-delimiter" */
38  pos = str_string.find_first_of(str_delimiters, lastPos);
39  }
40  }
41 
42  /****************************************/
43  /****************************************/
44 
45  std::string StringToUpperCase(const std::string& str_string) {
46  auto* buf = new char[str_string.length()];
47  str_string.copy(buf, str_string.length());
48 
49  for(unsigned int i = 0; i < str_string.length(); ++i)
50  buf[i] = toupper(buf[i]);
51 
52  std::string r(buf, str_string.length());
53 
54  delete[] buf;
55 
56  return r;
57  }
58 
59  /****************************************/
60  /****************************************/
61 
62  std::string StringToLowerCase(const std::string& str_string) {
63  auto* buf = new char[str_string.length()];
64  str_string.copy(buf, str_string.length());
65 
66  for(unsigned int i = 0; i < str_string.length(); ++i)
67  buf[i] = tolower(buf[i]);
68 
69  std::string r(buf, str_string.length());
70 
71  delete[] buf;
72 
73  return r;
74  }
75 
76  /****************************************/
77  /****************************************/
78 
79  void Replace(std::string& str_buffer,
80  const std::string& str_original,
81  const std::string& str_new) {
82  /* Start from the beginning of the string */
83  size_t unPos = 0;
84  do {
85  /* Look for the substring to replace */
86  unPos = str_buffer.find(str_original, unPos);
87  /* Has it been found? */
88  if(unPos != std::string::npos) {
89  /* Yes, it has been found */
90  /* Replace the substring with the new one */
91  str_buffer.replace(unPos, str_original.length(), str_new);
92  /* Update unPos so that it point past the end of the replaced portion */
93  unPos += str_new.length();
94  /* Make sure unPos does not exceed the string length */
95  if(unPos >= str_buffer.length()) {
96  unPos = std::string::npos;
97  }
98  }
99  /* Continue until the original string has been found */
100  } while(unPos != std::string::npos);
101  }
102 
103  /****************************************/
104  /****************************************/
105 
106  bool MatchPattern(const std::string& str_input,
107  const std::string& str_pattern) {
108  /* Code taken from
109  http://www.opengroup.org/onlinepubs/000095399/functions/regexec.html
110  */
111  UInt32 nStatus;
112  regex_t tRegExp;
113  if(::regcomp(&tRegExp, str_pattern.c_str(), REG_EXTENDED | REG_NOSUB) != 0) {
114  return false;
115  }
116  nStatus = ::regexec(&tRegExp, str_input.c_str(), 0, nullptr, 0);
117  ::regfree(&tRegExp);
118  if (nStatus != 0) {
119  return false;
120  }
121  return true;
122  }
123 
124  /****************************************/
125  /****************************************/
126 
127  std::string& ExpandEnvVariables(std::string& str_buffer) {
128  size_t unStart = 0, unEnd;
129  std::string strVarName;
130  char* pchVarValue;
131  bool bDone = false;
132  do {
133  /* Look for the $ character */
134  unStart = str_buffer.find_first_of('$');
135  /* Has it been found, and is it not the last character? */
136  if(unStart != std::string::npos &&
137  unStart+1 < str_buffer.length()) {
138  /* Yes, it has been found */
139  /* Look for the first non-alphanumeric character */
140  unEnd = str_buffer.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", unStart+1);
141  /* Has it been found? */
142  if(unEnd != std::string::npos) {
143  /* Yes, it has been found */
144  strVarName = str_buffer.substr(unStart+1, unEnd-unStart-1);
145  }
146  else {
147  /* No, it has not been found */
148  strVarName = str_buffer.substr(unStart+1, str_buffer.length()-unStart-1);
149  }
150  /* Get the variable value */
151  pchVarValue = ::getenv(strVarName.c_str());
152  /* Was the value found? */
153  if(pchVarValue != nullptr) {
154  /* Yes, it was */
155  /* Replace the variable name with its value */
156  str_buffer.replace(unStart, strVarName.length()+1, pchVarValue);
157  }
158  else {
159  /* Erase the variable name from the string */
160  str_buffer.erase(unStart, strVarName.length()+1);
161  }
162  /* Reset the start pointer to prepare the next search */
163  unStart = 0;
164  }
165  else {
166  /* No further $ chars found or end of string reached */
167  bDone = true;
168  }
169  } while(!bDone);
170  return str_buffer;
171  }
172 
173  /****************************************/
174  /****************************************/
175 
176 }
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
void Replace(std::string &str_buffer, const std::string &str_original, const std::string &str_new)
Searches into str_buffer for occurrences of str_original and substitutes them with str_new.
std::string & ExpandEnvVariables(std::string &str_buffer)
Searches into str_buffer for occurrences of an environment variable of the form $VAR and substitutes ...
bool MatchPattern(const std::string &str_input, const std::string &str_pattern)
Returns true if str_pattern is matched by str_input.
std::string StringToLowerCase(const std::string &str_string)
Converts a string to lower case.
void Tokenize(const std::string &str_string, std::vector< std::string > &vec_tokens, const std::string &str_delimiters)
Tokenizes the given string according to the wanted delimiters (by default just a " ").
std::string StringToUpperCase(const std::string &str_string)
Converts a string to upper case.