Nemo  2.4.0
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
tstring.h
Go to the documentation of this file.
1 
28 #ifndef TSTRING_H
29 #define TSTRING_H
30 
31 #include <sstream>
32 #include <string>
33 #include <vector>
34 #include "output.h"
35 
36 using namespace std;
37 
40 class tstring {
41 
42 private:
43  tstring(){}
44 
45 public:
46  // ----------------------------------------------------------------------------------------
47  // str2uint
48  // ----------------------------------------------------------------------------------------
50  static unsigned int str2uint (const string& str)
51  {
52  istringstream IN(str);
53  unsigned int i;
54  IN >> i;
55  return i;
56  }
57  // ----------------------------------------------------------------------------------------
58  // str2uint
59  // ----------------------------------------------------------------------------------------
61  static unsigned long str2ulong (const string& str)
62  {
63  istringstream IN(str);
64  unsigned long i;
65  IN >> i;
66  return i;
67  }
68  // ----------------------------------------------------------------------------------------
69  // str2int
70  // ----------------------------------------------------------------------------------------
72  static int str2int (const string& str)
73  {
74  istringstream IN(str);
75  int i;
76  IN >> i;
77  return i;
78  }
79  // ----------------------------------------------------------------------------------------
80  // str2dble
81  // ----------------------------------------------------------------------------------------
83  static double str2dble (const string& str)
84  {
85  istringstream IN(str);
86  double d;
87  IN >> d;
88  return d;
89  }
90  // ----------------------------------------------------------------------------------------
91  // int2str
92  // ----------------------------------------------------------------------------------------
94  static string int2str (const int i)
95  {
96  ostringstream OUT;
97  OUT << i;
98  return OUT.str();
99  }
100  // ----------------------------------------------------------------------------------------
101  // ulong2str
102  // ----------------------------------------------------------------------------------------
104  static string ulong2str (const unsigned long i)
105  {
106  ostringstream OUT;
107  OUT << i;
108  return OUT.str();
109  }
110  // ----------------------------------------------------------------------------------------
111  // dble2str
112  // ----------------------------------------------------------------------------------------
114  static string dble2str (const double d)
115  {
116  ostringstream OUT;
117  OUT << d;
118  return OUT.str();
119  }
120  // ----------------------------------------------------------------------------------------
121  // split
122  // ----------------------------------------------------------------------------------------
127  static vector<string> split(const string& str, const char delim, bool splitOnce = false)
128  {
129  vector<string> tokens;
130  string out(str);
131  size_t pos = 0;
132 
133  while(out.find(delim) != string::npos) {
134  pos = out.find_first_of(delim);
135  tokens.push_back( removeEnclosingChar(out.substr(0, pos), ' ', ' ', true) );
136  out = out.substr(pos+1);
137  if(splitOnce) break;
138  }
139 
140  tokens.push_back( removeEnclosingChar(out, ' ', ' ', true) );
141 
142  return tokens;
143  }
144  // ----------------------------------------------------------------------------------------
145  // splitExcludeEnclosedDelimiters
146  // ----------------------------------------------------------------------------------------
154  static vector<string> splitExcludeEnclosedDelimiters (const string& str, const char delim = ',',
155  const string& encloser = "([{\"")
156  {
157  if(str.find_first_of(encloser) == string::npos) return split(str, delim);
158 
159  string::size_type open, delim_pos, next_index = 0;
160  string pair, block, tail(str);
161  vector<string> tokens;
162 
163  while (tail.find(delim) != string::npos) {
164 
165  delim_pos = tail.find_first_of(delim);
166 
167  //check if delim is within any enclosed block
168  open = tail.find_first_of(encloser);
169 
170  //read and remove blocks
171  while (open < delim_pos) {
172 
173  block = getBlock(tail.substr(open, string::npos), tail.at(open));
174 
175  next_index = open + block.size();
176 
177  pair += tail.substr(0, next_index);
178 
179  tail = tail.substr(next_index);
180 
181  delim_pos = tail.find_first_of(delim);
182 
183  open = tail.find_first_of(encloser);
184  }
185 
186  pair += tail.substr(0, delim_pos);
187 
188  pair = removeEnclosingChar(pair, ' ', ' ', true);
189 
190  tokens.push_back(pair);
191  pair.clear();
192 
193  if(delim_pos != string::npos) tail = tail.substr(delim_pos + 1);
194  else break;
195  }
196 
197  if(tail.size() != 0) { //check for trailing space characters before adding relevant characters
198  if(removeEnclosingChar(tail, ' ', ' ', true).size() != 0)
199  tokens.push_back( removeEnclosingChar(tail, ' ', ' ', true) );
200  }
201 
202 
203  return tokens;
204  }
205 
206 
207  //----------------------------------------------------------------------------------------
208  // getBlock
209  //----------------------------------------------------------------------------------------
212  static string getBlock(const string& str, const char start_c, const char end_c = '\0',
213  bool includeEnclosing = true)
214  {
215  string out;
216  char c, end = 0;
217  bool closed = false;
218 
219  istringstream IN( removeEnclosingChar(str, ' ', ' ', true) );
220 
221  IN.get(c);
222 
223  if(end_c == 0) {
224  switch (c) {
225  case '(':
226  end = ')';
227  break;
228  case '{':
229  end = '}';
230  break;
231  case '[':
232  end = ']';
233  break;
234  case '"':
235  end = '"';
236  break;
237  default:
238  error("tstring::getBlock: unknown start of block \'%c\'\n", c);
239  break;
240  }
241  } else end = end_c;
242 
243  out += c;
244 
245  while (IN.get(c) && IN.good() && !IN.eof() ) {
246 
247  if(c == end) {
248 
249  out += c;
250  closed = true;
251  break;
252 
253  } else if(c == start_c) {//nested blocks
254 
255  out += __get_block(IN, start_c, end);
256 
257  } else out += c;
258 
259  } //__end_while__
260 
261 
262  if(!closed) fatal("missing closing character '%c' in %s.\n", end, str.c_str());
263 
264  return out;
265  }
266  //----------------------------------------------------------------------------------------
267  // getBlock
268  //----------------------------------------------------------------------------------------
270  static string __get_block(istringstream& IN, const char start_c, const char end_c)
271  {
272  string out;
273  char c;
274  bool closed = false;
275 
276  out += start_c;
277 // cout << "start block : "<<start_c<<endl;
278  while (IN.get(c) && IN.good() && !IN.eof() ) {
279 
280  if(c == end_c) {
281 
282  out += c;
283  closed = true;
284  break;
285 
286  } else if(c == start_c) {//nested blocks
287 
288  out += __get_block(IN, start_c, end_c);
289 
290  } else out += c;
291 
292  } //__end_while__
293 // cout<<out<<endl;
294 
295  if(!closed) fatal("missing closing character '%c' in %s.\n", end_c, IN.str().c_str());
296 
297  // cout << "close block : "<< c << endl;
298 
299  return out;
300  }
301  // ----------------------------------------------------------------------------------------
302  // removeChar
303  // ----------------------------------------------------------------------------------------
305  static string removeChar(const string& str, const char c)
306  {
307  string s(str);
308 
309  for (unsigned int i = 0; i < s.size(); i++) {
310  if (s[i] == c) {
311  s.erase(s.begin() + i);
312  i--;
313  }
314  }
315 
316  return s;
317  }
318  // ----------------------------------------------------------------------------------------
319  // removeFirstCharOf
320  // ----------------------------------------------------------------------------------------
322  static string removeFirstCharOf(const string& str, const char c)
323  {
324  string out(str);
325  size_t pos;
326 
327  if( (pos = out.find_first_of(c)) != string::npos )
328  out.erase(out.begin() + pos);
329 
330  return out;
331  }
332  // ----------------------------------------------------------------------------------------
333  // removeLastCharOf
334  // ----------------------------------------------------------------------------------------
336  static string removeLastCharOf(const string& str, const char c)
337  {
338  string out(str);
339  size_t pos;
340 
341  if( (pos = out.find_last_of(c)) != string::npos )
342  out.erase(out.begin() + pos);
343 
344  return out;
345  }
346  // ----------------------------------------------------------------------------------------
347  // removeEnclosingChar
348  // ----------------------------------------------------------------------------------------
355  static string removeEnclosingChar (const string& str, const char o, const char c, bool allowMissing = false)
356  {
357  string s(str);
358  size_t first, last;
359 
360  first = s.find_first_of(o, 0);
361  if(first != 0) {
362  if(!allowMissing) {
363  error("tstring::removeEnclosingChar:: string \"%s\" not starting with \"%c\"\n.", s.c_str(), o);
364  return s;
365  }
366  } else
367  s.erase(s.begin());
368 
369  last = s.find_last_of(c);
370  if(last != s.length()-1 || last == string::npos) {
371  if(!allowMissing) {
372  error("tstring::removeEnclosingChar:: string \"%s\" not ending with \"%c\"\n.", s.c_str(), c);
373  return s;
374  }
375  } else
376  s.erase(s.end()-1);
377 
378  return s;
379  }
380  // ----------------------------------------------------------------------------------------
381  // removeLeadingWhiteSpace
382  // ----------------------------------------------------------------------------------------
383  static string removeLeadingWhiteSpace (const string& str)
384  {
385  string out(str);
386 
387  while(isspace(out[0]))
388  out.erase(out.begin());
389 
390  return out;
391  }
392 
393  static string replaceChar (const string& str, const char target, const char replacement)
394  {
395  string out(str);
396  size_t pos = out.find_first_of(target, 0);
397 
398  while(pos != string::npos) {
399  out[pos] = replacement;
400  pos = out.find_first_of(target, pos);
401  }
402 
403  return out;
404  }
405  // ----------------------------------------------------------------------------------------
406  // isanumber
407  // ----------------------------------------------------------------------------------------
409  static bool isanumber(const string& str)
410  {
411  unsigned int i = 0;
412 
413  while(i < str.size()) {
414  if(!isdigit(str[i]))
415  if(str[i] != '.' && str[i] != 'e' && str[i] != '-' && str[i] != '+')
416  return false;
417  i++;
418  }
419 
420  return true;
421  }
422 
423 
425  static bool isNA(const string& str)
426  {
427  if("NA" == str) return true;
428 
429  if("na" == str) return true;
430 
431  if("NaN" == str) return true;
432 
433  if("NAN" == str) return true;
434 
435  if("nan" == str) return true;
436 
437  return false;
438  }
439 };
440 
441 
442 
443 #endif
A global class to handle string conversions and operations.
Definition: tstring.h:40
static double str2dble(const string &str)
Converts a string into a double.
Definition: tstring.h:83
static string removeEnclosingChar(const string &str, const char o, const char c, bool allowMissing=false)
Removes characters enclosing a string.
Definition: tstring.h:355
static int str2int(const string &str)
Converts a string into an integer.
Definition: tstring.h:72
static unsigned long str2ulong(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:61
static vector< string > splitExcludeEnclosedDelimiters(const string &str, const char delim=',', const string &encloser="([{\"")
Splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:154
static vector< string > split(const string &str, const char delim, bool splitOnce=false)
splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:127
static string removeLeadingWhiteSpace(const string &str)
Definition: tstring.h:383
static bool isNA(const string &str)
Check whether the string is NA | NaN.
Definition: tstring.h:425
static string __get_block(istringstream &IN, const char start_c, const char end_c)
Internal function used by getBlock to recursively read nested blocks.
Definition: tstring.h:270
static bool isanumber(const string &str)
Check whether the string is a number.
Definition: tstring.h:409
static string removeChar(const string &str, const char c)
Removes a given character from a string.
Definition: tstring.h:305
tstring()
Definition: tstring.h:43
static string getBlock(const string &str, const char start_c, const char end_c='\0', bool includeEnclosing=true)
Reads a substring delimited by two enclosing character from a string.
Definition: tstring.h:212
static string int2str(const int i)
Writes an integer value into a string.
Definition: tstring.h:94
static string removeFirstCharOf(const string &str, const char c)
Removes the first of a character found in a string.
Definition: tstring.h:322
static unsigned int str2uint(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:50
static string removeLastCharOf(const string &str, const char c)
Removes the last of a character found in a string.
Definition: tstring.h:336
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:114
static string ulong2str(const unsigned long i)
Writes an integer value into a string.
Definition: tstring.h:104
static string replaceChar(const string &str, const char target, const char replacement)
Definition: tstring.h:393
void fatal(const char *str,...)
Definition: output.cc:99
int error(const char *str,...)
Definition: output.cc:78
Nemo2.

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR