otsdaq  v2_05_02_indev
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: // private constructor because all static members, should never instantiate
19  // this class
20  StringMacros (void);
21  ~StringMacros (void);
22 
23  public:
24  // Here is the list of static helper functions:
25  //
26  // wildCardMatch
27  // inWildCardSet
28  // getWildCardMatchFromMap
29  //
30  // decodeURIComponent
31  // convertEnvironmentVariables
32  // isNumber
33  // getNumber
34  // getTimestampString
35  //
36  // validateValueForDefaultStringDataType
37  //
38  // getSetFromString
39  // getVectorFromString
40  // getMapFromString
41  //
42  // setToString
43  // vectorToString
44  // mapToString
45  //
46  // demangleTypeName
47 
48  static bool wildCardMatch (const std::string& needle, const std::string& haystack, unsigned int* priorityIndex = 0);
49  static bool inWildCardSet (const std::string& needle, const std::set<std::string>& haystack);
50 
51  //========================================================================================================================
52  // getWildCardMatchFromMap ~
53  // returns value if needle is in haystack otherwise throws exception
54  // (considering wildcards AND match priority as defined by
55  // StringMacros::wildCardMatch)
56  template<class T>
57  static T& getWildCardMatchFromMap (
58  const std::string& needle,
59  std::map<std::string, T>& haystack,
60  std::string* foundKey = 0); // defined in included .icc source
61 
62  static std::string decodeURIComponent (const std::string& data);
63  static std::string encodeURIComponent (const std::string& data);
64  static std::string convertEnvironmentVariables (const std::string& data);
65 
66  static bool isNumber (const std::string& stringToCheck);
67  static std::string getNumberType (const std::string& stringToCheck);
68  template<class T>
69  static bool getNumber (const std::string& s, T& retValue); // defined in included .icc source
70  //template<>
71  static bool getNumber (const std::string& s, bool& retValue); // defined in included .icc source
72 
73  static std::string getTimestampString (const std::string& linuxTimeInSeconds);
74  static std::string getTimestampString (const time_t& linuxTimeInSeconds = time(0));
75 
76  //========================================================================================================================
77  // validateValueForDefaultStringDataType ~
78  // special validation ignoring any table info - just assuming type string
79  template<class T>
80  static T validateValueForDefaultStringDataType ( // defined in included .icc source
81  const std::string& value,
82  bool doConvertEnvironmentVariables = true);
83  static std::string validateValueForDefaultStringDataType (
84  const std::string& value,
85  bool doConvertEnvironmentVariables = true);
86 
87  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'});
88 
89  //========================================================================================================================
90  // getMapFromString ~
91  template<class T /*value type*/,
92  class S = std::string /*name string or const string*/>
93  static void getMapFromString ( // defined in included .icc source
94  const std::string& inputString,
95  std::map<S, T>& mapToReturn,
96  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
97  const std::set<char>& nameValueDelimiter = {'=', ':'},
98  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
99  static void getMapFromString (
100  const std::string& inputString,
101  std::map<std::string, std::string>& mapToReturn,
102  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
103  const std::set<char>& nameValueDelimiter = {'=', ':'},
104  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
105  static void getVectorFromString (
106  const std::string& inputString,
107  std::vector<std::string>& listToReturn,
108  const std::set<char>& delimiter = {',', '|', '&'},
109  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
110  std::vector<char>* listOfDelimiters = 0);
111  static std::vector<std::string> getVectorFromString (
112  const std::string& inputString,
113  const std::set<char>& delimiter = {',', '|', '&'},
114  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
115  std::vector<char>* listOfDelimiters = 0);
116 
117  //========================================================================================================================
118  // mapToString ~
119  // *ToString declarations (template definitions are in included .icc source)
120  template<class T>
121  static std::string mapToString ( // defined in included .icc source
122  const std::map<std::string, T>& mapToReturn,
123  const std::string& primaryDelimeter = ", ",
124  const std::string& secondaryDelimeter = ": ");
125  template<class T>
126  static std::string mapToString (
127  const std::map<std::pair<std::string, std::string>, T>& mapToReturn,
128  const std::string& primaryDelimeter = ", ",
129  const std::string& secondaryDelimeter = ": ");
130  template<class T>
131  static std::string mapToString (
132  const std::map<std::pair<std::string, std::pair<std::string, std::string>>, T>&
133  mapToReturn,
134  const std::string& primaryDelimeter = ", ",
135  const std::string& secondaryDelimeter = ": ");
136  template<class T>
137  static std::string mapToString (
138  const std::map<std::string, std::pair<std::string, T>>& mapToReturn,
139  const std::string& primaryDelimeter = ", ",
140  const std::string& secondaryDelimeter = ": ");
141  template<class T>
142  static std::string mapToString (
143  const std::map<std::string, std::map<std::string, T>>& mapToReturn,
144  const std::string& primaryDelimeter = ", ",
145  const std::string& secondaryDelimeter = ": ");
146  template<class T>
147  static std::string mapToString (
148  const std::map<std::string, std::set<T>>& mapToReturn,
149  const std::string& primaryDelimeter = ", ",
150  const std::string& secondaryDelimeter = ": ");
151  template<class T>
152  static std::string mapToString (
153  const std::map<std::string, std::vector<T>>& mapToReturn,
154  const std::string& primaryDelimeter = ", ",
155  const std::string& secondaryDelimeter = ": ");
156  static std::string mapToString (
157  const std::map<std::string, uint8_t>& mapToReturn,
158  const std::string& primaryDelimeter = ", ",
159  const std::string& secondaryDelimeter = ": ");
160 
161 
162  //========================================================================================================================
163  // setToString ~
164  template<class T>
165  static std::string setToString ( // defined in included .icc source
166  const std::set<T>& setToReturn,
167  const std::string& delimeter = ", ");
168  static std::string setToString (
169  const std::set<uint8_t>& setToReturn,
170  const std::string& delimeter = ", ");
171  template<class S, class T>
172  static std::string setToString (
173  const std::set<std::pair<S, T>>& setToReturn,
174  const std::string& primaryDelimeter = ", ",
175  const std::string& secondaryDelimeter = ":");
176 
177 
178  //========================================================================================================================
179  // vectorToString ~
180  template<class T>
181  static std::string vectorToString ( // defined in included .icc source
182  const std::vector<T>& setToReturn,
183  const std::string& delimeter = ", ");
184  static std::string vectorToString (
185  const std::vector<uint8_t>& setToReturn,
186  const std::string& delimeter = ", ");
187  template<class S, class T>
188  static std::string vectorToString (
189  const std::vector<std::pair<S, T>>& setToReturn,
190  const std::string& primaryDelimeter = "; ",
191  const std::string& secondaryDelimeter = ":");
192 
193  static bool extractCommonChunks (const std::vector<std::string>& haystack, std::vector<std::string>& commonChunksToReturn, std::vector<std::string>& wildcardStrings, unsigned int& fixedWildcardLength);
194 
195  static std::string demangleTypeName (const char* name);
196  template<class T>
197  static std::string getTypeName (void) {return StringMacros::demangleTypeName(typeid(T).name());}
198  static std::string stackTrace (void);
199  static std::string exec (const char* cmd);
200 
201  static char* otsGetEnvironmentVarable (const char* name, const std::string& location, const unsigned int& line);
202 
203 
204 
205  struct IgnoreCaseCompareStruct { //get string in order ignoring letter case
206  bool operator() (const std::string& lhs, const std::string& rhs) const; //comparison handler
207  }; //end IgnoreCaseCompareStruct
208 
209  // clang-format on
210 }; // end StringMarcos static class
211 
212 #include "otsdaq/Macros/StringMacros.icc" //define template functions
213 
214 } // namespace ots
215 #endif