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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR