C API Documentation
rrStringUtils.h
1 #ifndef rrStringUtilsH
2 #define rrStringUtilsH
3 #include <string>
4 #include <list>
5 #include <vector>
6 #include <complex>
7 #include <map>
8 #include <unordered_map>
9 #include <set>
10 #include "rrConstants.h"
11 #include "rrExporter.h"
12 #include "Variant.h"
13 
14 namespace rr
15 {
16 using std::string;
17 using std::list;
18 using std::vector;
19 using std::complex;
20 
21 RR_DECLSPEC char* createText(const string& str);
22 RR_DECLSPEC char* createText(const int& count);
23 RR_DECLSPEC bool freeText(char* str);
24 RR_DECLSPEC string replaceWord(const string& str1, const string& str2, const string& theString);
25 RR_DECLSPEC bool convertFunctionCallToUseVarArgsSyntax(const string& funcName, string& expression);
26 RR_DECLSPEC string removeChars(const string& str, const string& chars);
27 RR_DECLSPEC bool isUnwantedChar(char ch); //Predicate for find_if algorithms..
28 RR_DECLSPEC size_t findMatchingRightParenthesis(const string& expression, const size_t startFrom);
29 RR_DECLSPEC int getNumberOfFunctionArguments(const string& expression);
30 RR_DECLSPEC string tabs(const int& nr);
31 RR_DECLSPEC string NL();
32 
33 RR_DECLSPEC string toUpperOrLowerCase(const string& inStr, int (*func)(int));
34 RR_DECLSPEC string toUpper(const string& str);
35 RR_DECLSPEC string toLower(const string& str);
36 
37 RR_DECLSPEC string getFilePath(const string& fileN);
38 RR_DECLSPEC string getFileName(const string& fileN);
39 RR_DECLSPEC string getFileNameNoExtension(const string& fileN);
40 RR_DECLSPEC string getFileExtension(const string& fileN);
41 
42 RR_DECLSPEC string changeFileExtensionTo(const string& theFileName, const string& newExtension);
43 
44 RR_DECLSPEC int compareNoCase(const string& str1, const string& str2);
45 RR_DECLSPEC string trim(const string& str, const char& toTrim = ' ');
46 RR_DECLSPEC bool startsWith(const string& src, const string& sub);
47 RR_DECLSPEC bool endsWith(const string& src, const string& sub);
48 
49 //conversions
50 RR_DECLSPEC string intToStr(const int& nt);
51 RR_DECLSPEC int strToInt(const string& nt);
52 RR_DECLSPEC string dblToStr(const double& nt);
53 RR_DECLSPEC double strToDbl(const string& nt);
54 RR_DECLSPEC vector<string> splitString(const string &text, const string &separators);
55 RR_DECLSPEC vector<string> splitString(const string& input, const char& delimiters);
56 RR_DECLSPEC int toInt(const string& str);
57 RR_DECLSPEC bool toBool(const string& str);
58 RR_DECLSPEC double toDouble(const string& str);
59 
60 RR_DECLSPEC vector<double> toDoubleVector(const string& str);
61 RR_DECLSPEC complex<double> toComplex(const string& str);
62 
63 RR_DECLSPEC string toString(const bool& b);
64 RR_DECLSPEC string toString(const double& d, const string& format = gDoubleFormat);
65 RR_DECLSPEC string toString(const unsigned int& n, const string& format = gIntFormat, const int nBase=10);
66 RR_DECLSPEC string toString(const int& n, const string& format = gIntFormat, const int nBase=10);
67 RR_DECLSPEC string toString(const long n, const int nBase=10);
68 RR_DECLSPEC string toString(const unsigned long n, const int nBase=10);
69 RR_DECLSPEC string toString(const unsigned short n, const int nBase=10);
70 RR_DECLSPEC string toString(const short n, const int nBase=10);
71 RR_DECLSPEC string toString(const char n);
72 RR_DECLSPEC string toString(const unsigned char n);
73 RR_DECLSPEC string toString(const string& s);
74 RR_DECLSPEC string toString(const char* str);
75 RR_DECLSPEC string toString(const vector<string>& vec, const string& sep = ", ");
76 RR_DECLSPEC string toStringSize(size_t n);
77 
78 RR_DECLSPEC string format(const string& src, const int& arg);
79 RR_DECLSPEC string format(const string& str, const int& arg1);
80 
81 
82 RR_DECLSPEC string format(const string& src, const string& arg);
83 RR_DECLSPEC string format(const string& src, const string& arg1, const string& arg2, const string& arg3);
84 RR_DECLSPEC string format(const string& src, const string& arg1, const string& arg2);
85 RR_DECLSPEC string format(const string& src, const string& arg1, const int& arg2);
86 RR_DECLSPEC string format(const string& src, const string& arg1, const int& arg2, const string& arg3);
87 RR_DECLSPEC string format(const string& str1, const string& str2);
88 RR_DECLSPEC string format(const string& str1, const string& arg1, const string& arg2);
89 RR_DECLSPEC string format(const string& str1, const string& arg1, const int& arg2);
90 RR_DECLSPEC string format(const string& str1, const string& arg1, const int& arg2, const string& arg3);
91 RR_DECLSPEC string format(const string& str1, const string& arg1, const string& arg2, const string& arg3);
92 RR_DECLSPEC string format(const string& str1, const string& arg1, const string& arg2, const string& arg3, const string& arg4);
93 RR_DECLSPEC string format(const string& str1, const string& arg1, const string& arg2, const string& arg3, const string& arg4, const string& arg5);
94 
95 RR_DECLSPEC string format(const string& str1, const unsigned int& arg1, const string& arg2);
96 
97 RR_DECLSPEC string format(const string& str1, const unsigned int& arg1, const string& arg2, const string& arg3);
98 
99 RR_DECLSPEC string format(const string& str1, const unsigned int& arg1, const unsigned int& arg2, const string& arg3, const string& arg4);
100 
101 RR_DECLSPEC string append(const string& str);
102 RR_DECLSPEC string append(const int& str);
103 RR_DECLSPEC string append(const unsigned int& str);
104 RR_DECLSPEC string append(const string& s1, const string& s2);
105 RR_DECLSPEC string append(const string& s1, const string& s2, const string& s3);
106 RR_DECLSPEC string append(const string& s1, const unsigned int& s2, const string& s3);
107 RR_DECLSPEC string append(const string& s1, const unsigned int& s2, const string& s3, const string& s4);
108 
109 RR_DECLSPEC string substitute(const string& src, const string& thisOne, const string& withThisOne, const int& howMany = -1);
110 RR_DECLSPEC string substitute(const string& src, const string& thisOne, const int& withThisOne, const int& howMany = -1);
111 //RR_DECLSPEC string substitute(const string& src, const string& thisOne, const double& withThisOne, const int& howMany = -1);
112 RR_DECLSPEC string removeNewLines(const string& str, const int& howMany = -1);
113 
114 
115 /*
116 * Saves t in a binary format that can then be reloaded with loadBinary
117 * T must be a primitive, POD with no pointers, std::vector, std::string, std::map, std::unordered_map, std::set, rr::Variant
118 * or combinations of those, like std::map<rr::Variant, std::vector<std::string> >
119 */
120 template <typename T>
121 inline void saveBinary(std::ostream& out, const T& t)
122 {
123  out.write((char*)&t, sizeof(T));
124 }
125 
126 template <>
127 inline void saveBinary(std::ostream& out, const std::string& s)
128 {
129  saveBinary(out, s.size());
130  out.write(s.c_str(), s.size());
131 }
132 
133 template <typename T>
134 inline void saveBinary(std::ostream& out, const std::vector<T>& v)
135 {
136  saveBinary(out, v.size());
137  for (T t : v)
138  {
139  saveBinary(out, t);
140  }
141 }
142 
143 template <typename K, typename V>
144 inline void saveBinary(std::ostream& out, const std::map<K, V>& m)
145 {
146  saveBinary(out, m.size());
147  for (std::pair<K, V> p : m)
148  {
149  saveBinary(out, p.first);
150  saveBinary(out, p.second);
151  }
152 }
153 
154 template <typename K, typename V>
155 inline void saveBinary(std::ostream& out, const std::unordered_map<K, V>& m)
156 {
157  saveBinary(out, m.size());
158  for (std::pair<K, V> p : m)
159  {
160  saveBinary(out, p.first);
161  saveBinary(out, p.second);
162  }
163 }
164 
165 template <typename T>
166 inline void saveBinary(std::ostream& out, const std::set<T>& s)
167 {
168  saveBinary(out, s.size());
169  for (T t : s)
170  {
171  saveBinary(out, t);
172  }
173 }
174 
175 template<>
176 inline void saveBinary<rr::Variant>(std::ostream& out, const rr::Variant& var)
177 {
178  saveBinary(out, var.type());
179  switch (var.type())
180  {
181  case Variant::BOOL:
182  saveBinary(out, (bool)var);
183  break;
184  case Variant::CHAR:
185  saveBinary(out, (char)var);
186  break;
187  case Variant::DOUBLE:
188  saveBinary(out, (double)var);
189  break;
190  case Variant::FLOAT:
191  saveBinary(out, (float)var);
192  break;
193  case Variant::INT32:
194  saveBinary(out, (int32_t)var);
195  break;
196  case Variant::INT64:
197  saveBinary(out, (long)var);
198  break;
199  case Variant::STRING:
200  saveBinary(out, (std::string)var);
201  break;
202  case Variant::UCHAR:
203  saveBinary(out, (unsigned char)var);
204  break;
205  case Variant::UINT32:
206  saveBinary(out, (unsigned int)var);
207  break;
208  case Variant::UINT64:
209  saveBinary(out, (unsigned long)var);
210  break;
211  case Variant::DOUBLEVECTOR:
212  saveBinary(out, var.convert<std::vector<double> >());
213  default:
214  break;
215  }
216 }
217 
218 /*
219 * Loads an object saved by saveBinary from an istream and puts the result in t
220 * t must be of the same type that was saved by saveBinary
221 * Objects must be loaded in the order they were saved to the stream
222 */
223 template <typename T>
224 inline void loadBinary(std::istream& in, T& t)
225 {
226  T temp;
227  in.read((char*)&temp, sizeof(T));
228  t = temp;
229 }
230 
231 template <>
232 inline void loadBinary(std::istream& in, std::string& s)
233 {
234  size_t ssize;
235  loadBinary(in, ssize);
236  s.resize(ssize);
237  in.read(&s[0], ssize);
238 }
239 
240 template <typename T>
241 inline void loadBinary(std::istream& in, std::vector<T>& v)
242 {
243  size_t vsize;
244  loadBinary(in, vsize);
245  v.clear();
246  for (int i = 0; i < vsize; i++)
247  {
248  T next;
249  loadBinary(in, next);
250  v.push_back(next);
251  }
252 }
253 
254 template <typename K, typename V>
255 inline void loadBinary(std::istream& in, std::map<K, V>& m)
256 {
257  size_t msize;
258  loadBinary(in, msize);
259  m.clear();
260  for (int i = 0; i < msize; i++)
261  {
262  K k;
263  V v;
264  loadBinary(in, k);
265  loadBinary(in, v);
266  m.emplace(k, v);
267  }
268 }
269 
270 template <typename K, typename V>
271 inline void loadBinary(std::istream& in, std::unordered_map<K, V>& m)
272 {
273  size_t msize;
274  loadBinary(in, msize);
275  m.clear();
276  for (int i = 0; i < msize; i++)
277  {
278  K k;
279  V v;
280  loadBinary(in, k);
281  loadBinary(in, v);
282  m.emplace(k, v);
283  }
284 }
285 
286 template <typename T>
287 inline void loadBinary(std::istream& in, std::set<T>& s)
288 {
289  size_t ssize;
290  loadBinary(in, ssize);
291  s.clear();
292  for (int i = 0; i < ssize; i++)
293  {
294  T next;
295  loadBinary(in, next);
296  s.emplace(next);
297  }
298 }
299 
300 template<>
301 inline void loadBinary<rr::Variant>(std::istream& in, rr::Variant& var)
302 {
303  Variant::TypeId type;
304  std::string strVal;
305  std::vector<double> vectorVal;
306  loadBinary(in, type);
307  switch (type)
308  {
309  case Variant::BOOL:
310  bool boolVal;
311  loadBinary(in, boolVal);
312  var = boolVal;
313  break;
314  case Variant::CHAR:
315  char charVal;
316  loadBinary(in, charVal);
317  var = charVal;
318  break;
319  case Variant::DOUBLE:
320  double doubleVal;
321  loadBinary(in, doubleVal);
322  var = doubleVal;
323  break;
324  case Variant::FLOAT:
325  float floatVal;
326  loadBinary(in, floatVal);
327  var = floatVal;
328  break;
329  case Variant::INT32:
330  int int32Val;
331  loadBinary(in, int32Val);
332  var = int32Val;
333  break;
334  case Variant::INT64:
335  long int64Val;
336  loadBinary(in, int64Val);
337  var = int64Val;
338  break;
339  case Variant::STRING:
340  loadBinary(in, strVal);
341  var = strVal;
342  break;
343  case Variant::UCHAR:
344  unsigned char ucharVal;
345  loadBinary(in, ucharVal);
346  var = ucharVal;
347  break;
348  case Variant::UINT32:
349  unsigned int uint32Val;
350  loadBinary(in, uint32Val);
351  var = uint32Val;
352  break;
353  case Variant::UINT64:
354  unsigned long uint64Val;
355  loadBinary(in, uint64Val);
356  var = uint64Val;
357  break;
358  case Variant::DOUBLEVECTOR:
359  loadBinary(in, vectorVal);
360  var = vectorVal;
361  break;
362  default:
363  break;
364  }
365 }
366 
367 }
368 #endif
Definition: Variant.h:75
TypeId
Definition: Variant.h:82
T convert() const
Definition: Variant.h:178
TypeId type() const
C_DECL_SPEC int rrcCallConv freeText(char *text)
Free char* generated by library routines.
C_DECL_SPEC char *rrcCallConv createText(const char *text)
Creates memory for holding a string.