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