otsdaq  3.04.02
StringMacros.h
1 #ifndef _ots_StringMacros_h_
2 #define _ots_StringMacros_h_
3 
4 #include "otsdaq/Macros/CoutMacros.h"
5 
6 #include <map>
7 #include <memory> //shared_ptr
8 #include <set>
9 #include <typeinfo> // operator typeid
10 #include <vector>
11 
12 namespace ots
13 {
15 {
16  // clang-format off
17 
18  private:
19  // this class
20  StringMacros (void);
21  ~StringMacros (void);
22 
23  public:
24 
25  //========================================================================================================================
65  //========================================================================================================================
66 
67  static bool wildCardMatch (const std::string& needle, const std::string& haystack, unsigned int* priorityIndex = 0);
68  static bool inWildCardSet (const std::string& needle, const std::set<std::string>& haystack);
69 
70  //========================================================================================================================
75  template<class T>
77  const std::string& needle,
78  std::map<std::string, T>& haystack,
79  std::string* foundKey = 0);
80 
81  static std::string decodeURIComponent (const std::string& data);
82  static std::string encodeURIComponent (const std::string& data);
83  static void sanitizeForSQL (std::string& data);
84  static std::string escapeString (std::string inString, bool allowWhiteSpace = false);
85  static std::string escapeJSONStringEntities (const std::string& str);
86  static std::string restoreJSONStringEntities (const std::string& str);
87  static const std::string& trim (std::string& s);
88  static std::string convertEnvironmentVariables (const std::string& data);
89  static std::map<std::string /* system variable */,
90  std::map<std::string /* property */,
91  std::string /* value */>> systemVariables_;
92  static const std::string TBD; //for to-be-defined system variables (so there is a value in wiz mode, before configuration, etc.)
93 
94  static bool isNumber (const std::string& stringToCheck);
95  static std::string getNumberType (const std::string& stringToCheck);
96  template<class T>
97  static bool getNumber (const std::string& s, T& retValue);
99  static bool getNumber (const std::string& s, bool& retValue);
100 
101  static std::string getTimestampString (const std::string& linuxTimeInSeconds);
102  static std::string getTimestampString (const time_t linuxTimeInSeconds = time(0));
103  static std::string getTimeDurationString (const time_t durationInSeconds = time(0));
104 
105  //========================================================================================================================
108  template<class T>
110  const std::string& value,
111  bool doConvertEnvironmentVariables = true);
112  static std::string validateValueForDefaultStringDataType (
113  const std::string& value,
114  bool doConvertEnvironmentVariables = true);
115 
116  static void getSetFromString (const std::string& inputString, std::set<std::string>& setToReturn, const std::set<char>& delimiter = {',', '|', '&'}, const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
117 
118  //========================================================================================================================
120  template<class T /*value type*/,
121  class S = std::string /*name string or const string*/>
122  static void getMapFromString (
123  const std::string& inputString,
124  std::map<S, T>& mapToReturn,
125  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
126  const std::set<char>& nameValueDelimiter = {'=', ':'},
127  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
128  static void getMapFromString (
129  const std::string& inputString,
130  std::map<std::string, std::string>& mapToReturn,
131  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
132  const std::set<char>& nameValueDelimiter = {'=', ':'},
133  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
134  static void getVectorFromString (
135  const std::string& inputString,
136  std::vector<std::string>& listToReturn,
137  const std::set<char>& delimiter = {',', '|', '&'},
138  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
139  std::vector<char>* listOfDelimiters = 0,
140  bool decodeURIComponents = false);
141  static std::vector<std::string> getVectorFromString (
142  const std::string& inputString,
143  const std::set<char>& delimiter = {',', '|', '&'},
144  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
145  std::vector<char>* listOfDelimiters = 0,
146  bool decodeURIComponents = false);
147 
148  //========================================================================================================================
151  template<class T>
152  static std::string mapToString (
153  const std::map<std::string, T>& mapToReturn,
154  const std::string& primaryDelimeter = ", ",
155  const std::string& secondaryDelimeter = ": ");
156  template<class T>
157  static std::string mapToString (
158  const std::map<std::pair<std::string, std::string>, T>& mapToReturn,
159  const std::string& primaryDelimeter = ", ",
160  const std::string& secondaryDelimeter = ": ");
161  template<class T>
162  static std::string mapToString (
163  const std::map<std::pair<std::string, std::pair<std::string, std::string>>, T>&
164  mapToReturn,
165  const std::string& primaryDelimeter = ", ",
166  const std::string& secondaryDelimeter = ": ");
167  template<class T>
168  static std::string mapToString (
169  const std::map<std::string, std::pair<std::string, T>>& mapToReturn,
170  const std::string& primaryDelimeter = ", ",
171  const std::string& secondaryDelimeter = ": ");
172  template<class T>
173  static std::string mapToString (
174  const std::map<std::string, std::map<std::string, T>>& mapToReturn,
175  const std::string& primaryDelimeter = ", ",
176  const std::string& secondaryDelimeter = ": ");
177  template<class T>
178  static std::string mapToString (
179  const std::map<std::string, std::set<T>>& mapToReturn,
180  const std::string& primaryDelimeter = ", ",
181  const std::string& secondaryDelimeter = ": ");
182  template<class T>
183  static std::string mapToString (
184  const std::map<std::string, std::vector<T>>& mapToReturn,
185  const std::string& primaryDelimeter = ", ",
186  const std::string& secondaryDelimeter = ": ");
187  static std::string mapToString (
188  const std::map<std::string, uint8_t>& mapToReturn,
189  const std::string& primaryDelimeter = ", ",
190  const std::string& secondaryDelimeter = ": ");
191 
192 
193  //========================================================================================================================
195  template<class T>
196  static std::string setToString (
197  const std::set<T>& setToReturn,
198  const std::string& delimeter = ", ");
199  static std::string setToString (
200  const std::set<uint8_t>& setToReturn,
201  const std::string& delimeter = ", ");
202  template<class S, class T>
203  static std::string setToString (
204  const std::set<std::pair<S, T>>& setToReturn,
205  const std::string& primaryDelimeter = ", ",
206  const std::string& secondaryDelimeter = ":");
207 
208 
209  //========================================================================================================================
211  template<class T>
212  static std::string vectorToString (
213  const std::vector<T>& setToReturn,
214  const std::string& delimeter = ", ");
215  static std::string vectorToString (
216  const std::vector<uint8_t>& setToReturn,
217  const std::string& delimeter = ", ");
218  template<class S, class T>
219  static std::string vectorToString (
220  const std::vector<std::pair<S, T>>& setToReturn,
221  const std::string& primaryDelimeter = "; ",
222  const std::string& secondaryDelimeter = ":");
223 
224  static bool extractCommonChunks (const std::vector<std::string>& haystack, std::vector<std::string>& commonChunksToReturn, std::vector<std::string>& wildcardStrings, unsigned int& fixedWildcardLength);
225 
226  static std::string demangleTypeName (const char* name);
227  template<class T>
228  static std::string getTypeName (void) {return StringMacros::demangleTypeName(typeid(T).name());}
229  static std::string stackTrace (void);
230  static std::string exec (const char* cmd);
231 
232  static char* otsGetEnvironmentVarable (const char* name, const std::string& location, const unsigned int& line);
233 
234 
235 
236  static std::string extractXmlField (const std::string &xml,
237  const std::string &field,
238  uint32_t occurrence, size_t after,
239  size_t *returnFindPos = nullptr,
240  const std::string &valueField = "value=",
241  const std::string &quoteType = "'");
242  static std::string rextractXmlField (const std::string &xml,
243  const std::string &field,
244  uint32_t occurrence,
245  size_t before,
246  size_t *returnFindPos = nullptr,
247  const std::string &valueField = "value=",
248  const std::string &quoteType = "'");
249  static void coutSplit (const std::string &str,
250  uint8_t traceLevel = TLVL_DEBUG,
251  const std::set<char>& delimiter = {',', '\n', ';'});
252 
254  bool operator() (const std::string& lhs, const std::string& rhs) const;
255  }; //end IgnoreCaseCompareStruct
256 
257  // clang-format on
258 }; // end StringMarcos static class
259 
260 #include "otsdaq/Macros/StringMacros.icc" //define template functions
261 
262 } // namespace ots
263 #endif
defines used also by OtsConfigurationWizardSupervisor
bool operator()(const std::string &lhs, const std::string &rhs) const
<get string in order ignoring letter case
static std::string getTimestampString(const std::string &linuxTimeInSeconds)
static const std::string & trim(std::string &s)
static std::string extractXmlField(const std::string &xml, const std::string &field, uint32_t occurrence, size_t after, size_t *returnFindPos=nullptr, const std::string &valueField="value=", const std::string &quoteType="'")
static void getVectorFromString(const std::string &inputString, std::vector< std::string > &listToReturn, const std::set< char > &delimiter={',', '|', '&'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'}, std::vector< char > *listOfDelimiters=0, bool decodeURIComponents=false)
static std::string exec(const char *cmd)
static void getSetFromString(const std::string &inputString, std::set< std::string > &setToReturn, const std::set< char > &delimiter={',', '|', '&'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'})
static std::string setToString(const std::set< T > &setToReturn, const std::string &delimeter=", ")
setToString ~
static T validateValueForDefaultStringDataType(const std::string &value, bool doConvertEnvironmentVariables=true)
static char * otsGetEnvironmentVarable(const char *name, const std::string &location, const unsigned int &line)
static std::string escapeString(std::string inString, bool allowWhiteSpace=false)
static void sanitizeForSQL(std::string &data)
StringMacros::sanitizeForSQL.
static void coutSplit(const std::string &str, uint8_t traceLevel=TLVL_DEBUG, const std::set< char > &delimiter={',', '\n', ';'})
static std::string vectorToString(const std::vector< T > &setToReturn, const std::string &delimeter=", ")
vectorToString ~
static std::string convertEnvironmentVariables(const std::string &data)
static T & getWildCardMatchFromMap(const std::string &needle, std::map< std::string, T > &haystack, std::string *foundKey=0)
defined in included .icc source
static std::string getNumberType(const std::string &stringToCheck)
Note: before call consider use of stringToCheck = StringMacros::convertEnvironmentVariables(stringToC...
static std::string escapeJSONStringEntities(const std::string &str)
static std::string demangleTypeName(const char *name)
static std::string rextractXmlField(const std::string &xml, const std::string &field, uint32_t occurrence, size_t before, size_t *returnFindPos=nullptr, const std::string &valueField="value=", const std::string &quoteType="'")
static bool extractCommonChunks(const std::vector< std::string > &haystack, std::vector< std::string > &commonChunksToReturn, std::vector< std::string > &wildcardStrings, unsigned int &fixedWildcardLength)
static bool inWildCardSet(const std::string &needle, const std::set< std::string > &haystack)
static bool isNumber(const std::string &stringToCheck)
Note: before call consider use of stringToCheck = StringMacros::convertEnvironmentVariables(stringToC...
static std::string mapToString(const std::map< std::string, T > &mapToReturn, const std::string &primaryDelimeter=", ", const std::string &secondaryDelimeter=": ")
static void getMapFromString(const std::string &inputString, std::map< S, T > &mapToReturn, const std::set< char > &pairPairDelimiter={',', '|', '&'}, const std::set< char > &nameValueDelimiter={'=', ':'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'})
getMapFromString ~
static std::string restoreJSONStringEntities(const std::string &str)
static std::string getTimeDurationString(const time_t durationInSeconds=time(0))
static bool wildCardMatch(const std::string &needle, const std::string &haystack, unsigned int *priorityIndex=0)
Definition: StringMacros.cc:24
static std::string decodeURIComponent(const std::string &data)
static std::string stackTrace(void)
static bool getNumber(const std::string &s, T &retValue)