otsdaq  v2_04_01
StringMacros.h
1 #ifndef _ots_StringMacros_h_
2 #define _ots_StringMacros_h_
3 
4 #include "otsdaq-core/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 convertEnvironmentVariables (const std::string& data);
64  static bool isNumber (const std::string& stringToCheck);
65 
66  template<class T>
67  static bool getNumber (const std::string& s, T& retValue); // defined in included .icc source
68  static std::string getTimestampString (const std::string& linuxTimeInSeconds);
69  static std::string getTimestampString (const time_t& linuxTimeInSeconds = time(0));
70 
71  //========================================================================================================================
72  // validateValueForDefaultStringDataType ~
73  // special validation ignoring any table info - just assuming type string
74  template<class T>
75  static T validateValueForDefaultStringDataType ( // defined in included .icc source
76  const std::string& value,
77  bool doConvertEnvironmentVariables = true);
78  static std::string validateValueForDefaultStringDataType (
79  const std::string& value,
80  bool doConvertEnvironmentVariables = true);
81 
82  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'});
83 
84  //========================================================================================================================
85  // getMapFromString ~
86  template<class T /*value type*/,
87  class S = std::string /*name string or const string*/>
88  static void getMapFromString ( // defined in included .icc source
89  const std::string& inputString,
90  std::map<S, T>& mapToReturn,
91  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
92  const std::set<char>& nameValueDelimiter = {'=', ':'},
93  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
94  static void getMapFromString (
95  const std::string& inputString,
96  std::map<std::string, std::string>& mapToReturn,
97  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
98  const std::set<char>& nameValueDelimiter = {'=', ':'},
99  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
100  static void getVectorFromString (
101  const std::string& inputString,
102  std::vector<std::string>& listToReturn,
103  const std::set<char>& delimiter = {',', '|', '&'},
104  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
105  std::vector<char>* listOfDelimiters = 0);
106 
107  //========================================================================================================================
108  // mapToString ~
109  // *ToString declarations (template definitions are in included .icc source)
110  template<class T>
111  static std::string mapToString ( // defined in included .icc source
112  const std::map<std::string, T>& mapToReturn,
113  const std::string& primaryDelimeter = ", ",
114  const std::string& secondaryDelimeter = ": ");
115  template<class T>
116  static std::string mapToString (
117  const std::map<std::pair<std::string, std::string>, T>& mapToReturn,
118  const std::string& primaryDelimeter = ", ",
119  const std::string& secondaryDelimeter = ": ");
120  template<class T>
121  static std::string mapToString (
122  const std::map<std::pair<std::string, std::pair<std::string, std::string>>, T>&
123  mapToReturn,
124  const std::string& primaryDelimeter = ", ",
125  const std::string& secondaryDelimeter = ": ");
126  template<class T>
127  static std::string mapToString (
128  const std::map<std::string, std::pair<std::string, T>>& mapToReturn,
129  const std::string& primaryDelimeter = ", ",
130  const std::string& secondaryDelimeter = ": ");
131  template<class T>
132  static std::string mapToString (
133  const std::map<std::string, std::map<std::string, T>>& 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::set<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::vector<T>>& mapToReturn,
144  const std::string& primaryDelimeter = ", ",
145  const std::string& secondaryDelimeter = ": ");
146  static std::string mapToString (
147  const std::map<std::string, uint8_t>& mapToReturn,
148  const std::string& primaryDelimeter = ", ",
149  const std::string& secondaryDelimeter = ": ");
150 
151 
152  //========================================================================================================================
153  // setToString ~
154  template<class T>
155  static std::string setToString ( // defined in included .icc source
156  const std::set<T>& setToReturn,
157  const std::string& delimeter = ", ");
158  static std::string setToString (
159  const std::set<uint8_t>& setToReturn,
160  const std::string& delimeter = ", ");
161  template<class S, class T>
162  static std::string setToString (
163  const std::set<std::pair<S, T>>& setToReturn,
164  const std::string& primaryDelimeter = ", ",
165  const std::string& secondaryDelimeter = ":");
166 
167 
168  //========================================================================================================================
169  // vectorToString ~
170  template<class T>
171  static std::string vectorToString ( // defined in included .icc source
172  const std::vector<T>& setToReturn,
173  const std::string& delimeter = ", ");
174  static std::string vectorToString (
175  const std::vector<uint8_t>& setToReturn,
176  const std::string& delimeter = ", ");
177  template<class S, class T>
178  static std::string vectorToString (
179  const std::vector<std::pair<S, T>>& setToReturn,
180  const std::string& primaryDelimeter = "; ",
181  const std::string& secondaryDelimeter = ":");
182 
183  static std::string demangleTypeName (const char* name);
184  static std::string stackTrace ();
185  static std::string exec (const char* cmd);
186 
187  static char* otsGetEnvironmentVarable (const char* name, const std::string& location, const unsigned int& line);
188 
189  // clang-format on
190 }; // end StringMarcos static class
191 
192 #include "otsdaq-core/Macros/StringMacros.icc" //define template functions
193 
194 } // namespace ots
195 #endif