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

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR