-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstringtools.cpp
400 lines (335 loc) · 10.1 KB
/
stringtools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/***************************************************************************
stringtools.cpp - description
-------------------
begin : Mon Dec 10 2001
copyright : (C) 2001 by André Simon
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "stringtools.h"
using namespace std;
// Make a lowercase copy of s:
// (C) Bruce Eckel, Thinking in C++ Vol 2
// If pref="movie", suff="xyz" nxyz=235, and nfields=6 then
// returns "movie000235.xyz"
// nfields in the total number of digits in teh string
string StringTools::genfilename(const string& pref, const string &suff,
int nfields, int nxyz)
{
string nstr="";
string spacer="0";
double limit=0.9999;
for (int i=1; i<=nfields; i++) { limit=limit*10; nstr=nstr+"0";}
if (nxyz>limit) {
printf("\n Now in file %s at line %d \n ",__FILE__,__LINE__);
cout << "ERROR in genfilename" << endl;
return nstr;
}
nstr=StringTools::int2str(nxyz,nfields,spacer);
string fname=(pref+nstr)+("."+suff);
return (fname);
}
string StringTools::lowerCase(const string& s)
{
char* buf = new char[s.length()];
s.copy(buf, s.length());
for(unsigned int i = 0; i < s.length(); i++)
buf[i] = tolower(buf[i]);
string r(buf, s.length());
delete buf;
return r;
}
// converts atof correctly
double StringTools::atod(const string& s)
{
char* buf = new char[s.length()];
s.copy(buf, s.length());
for(unsigned int i = 0; i < s.length(); i++){
if (s[i]=='D') buf[i]='E';
if (s[i]=='d') buf[i]='e';
}
string r(buf, s.length());
delete buf;
double d=atof(r.c_str());
return d;
}
/** wandelt String s in Integer um */
// Needs removal of all blanks etc.
int StringTools::str2int(string &s)
{
int intVal=0;
bool neg = false;
for (unsigned int i=0; i< s.length(); i++)
{
if (s[i]=='-')
neg= ! neg;
else
intVal= (intVal*10)+(s[i]-48);
}
return (neg)? -intVal: intVal;
}
/** wandelt char* s in Integer um */
int StringTools::str2int(unsigned char *s)
{
int intVal=0;
bool neg = false;
unsigned int i=0;
while ( *(s+i) != '\0')
{
if (*(s+i)=='-')
neg= ! neg;
else
intVal= (intVal*10)+(*(s+i)-48);
i++;
}
return (neg)? -intVal: intVal;
}
/** gibt integer >=0 als String zurueck,
size=Mindestlaenge des ergebnisstrings,
Rest wird mit Leerzeichen (spaceStr) aufgefuellt
*/
string StringTools::int2str(int integer, int size, const string spaceStr)
{
int d;
string s, z;
do
{
d= integer % 10;
z=d+'0';
s.insert(0, z);
integer/=10;
}
while (integer);
for (int i=s.length(); i< size; i++)
s.insert( 0 , spaceStr);
return s;
}
string StringTools::double2str(double val, int precision)
{
char *buffer;
int decimal, sign;
string tempstr;
if (abs(val)<1.000e-12) {
tempstr="0.000000e00";
return(tempstr);
}
if (precision>12) {
printf("\n Now in file %s at line %d \n ",__FILE__,__LINE__);
printf("\n Now in file %s at line %d \n ",__FILE__,__LINE__);
}
char temp[25];
int expn=lrint(log10(abs(val)));
int decadd=0;
double fac=1;
if (expn<-5) {
decadd=-5-expn;
for (int i=1; i<=decadd; i++) {fac=fac*10;}
val=val*fac;
} else if( expn>5) {
decadd=expn-5;
for (int i=1; i<=decadd; i++) {fac=fac*10;}
val=val/fac;
}
for (int i=0; i<25; i++){temp[i]=' ';}
buffer = fcvt(val, precision, &decimal, &sign);
// OLDSTYLE
// sprintf (temp,"%c%c.%s x 10^%d ",sign?'-':'+',
// buffer[0],buffer+1,decimal-1);
if (expn<-5) decimal=decimal-decadd;
if (expn>5) decimal=decimal+decadd;
sprintf (temp," %c%c.%se%d ",sign?'-':'+',buffer[0],buffer+1,decimal-1);
tempstr=temp;
return(tempstr);
}
string StringTools::double2str1(double val)
{
char *buffer;
int decimal, sign;
string tempstr;
int precision = 0;
if (abs(val)<1.000e-12) {
tempstr="0.00000000";
return(tempstr);
}
char temp[25];
int expn=lrint(log10(abs(val)));
int decadd=0;
double fac=1;
if (expn<-5) {
decadd=-5-expn;
for (int i=1; i<=decadd; i++) {fac=fac*10;}
val=val*fac;
} else if( expn>5) {
decadd=expn-5;
for (int i=1; i<=decadd; i++) {fac=fac*10;}
val=val/fac;
}
for (int i=0; i<25; i++){temp[i]=' ';}
buffer = fcvt(val, precision, &decimal, &sign);
// OLDSTYLE
// sprintf (temp,"%c%c.%s x 10^%d ",sign?'-':'+',
// buffer[0],buffer+1,decimal-1);
if (expn<-5) decimal=decimal-decadd;
if (expn>5) decimal=decimal+decadd;
sprintf (temp," %c%c%s ",sign?'-':'+',buffer[0],buffer+1,decimal-1);
tempstr=temp;
return(tempstr);
}
/** gibt true zurueck, falls c eine Ziffer ist*/
// bool StringTools::isDigit(unsigned char c)
// {
// return ((c>='0') && (c<='9'));
// }
/** gibt True zurueck, falls c ein Buchstabe/Underscore ist */
bool StringTools::isAlpha(unsigned char c)
{
return (isalpha(c) || c == '_');
}
// /** gibt TRUE zurueck, wenn c Space oder Tab ist */
// bool StringTools::isWhitespace (unsigned char c)
// {
// return ((c=='\t') || (c==' '));
// }
string StringTools::trimRight(const string &value)
{
string::size_type where = value.find_last_not_of(" \t");
if (where == string::npos)
// string has nothing but space
return string();
if (where == (value.length() - 1))
// string has no trailing space, don't copy its contents
return value;
return value.substr(0, where + 1);
}
/** gibt naechsten Character der Zeile zurueck, der kein Whitespace ist */
unsigned char StringTools::getNextNonWs(const string &line, int index)
{
unsigned char c;
do
{
c=line[index++];
}
while (isspace(c));
return c;
}
unsigned int StringTools::getNextNonWsPos(const string &line, int index){
return line.find_first_not_of(WS_CHARS, index);
}
string StringTools::validateDirPath(const string & path){
return (path[path.length()-1] !=PATH_SEPARATOR_CHAR)?
path+PATH_SEPARATOR_CHAR : path;
}
// return a new clean string
string StringTools::newCleanString(string line){
// remove initial whitespace blank tab comma etc find where actaul
// string starts
// then find end "look for end of line or #", if end of line > 150 anyway
// stop
// erase 0 to start erase end to end
string::size_type sbegin, send;
const string delims(" \t,.;");
const char *commentchar("#");
int istart, iend, length;
// find size of string
length=line.length();
if (length==0) return NULL;
sbegin=line.find_first_not_of(delims);
istart=sbegin;
send=line.find_first_of(commentchar);
if (send>length) {iend=length;} else {iend=send;} // no commentchar
// clear from end of string
line.erase((iend));
line.erase(0,istart);
string r=line;
return r;
}
int StringTools::cleanstring(string& line){
string::size_type sbegin, send;
const string delims(" \t,.;");
const char *commentchar("#");
int istart, iend, length;
// find size of string
length=line.length();
if (length==0) return length;
sbegin=line.find_first_not_of(delims);
istart=sbegin;
send=line.find_first_of(commentchar);
if (send>length) {iend=length;} else {iend=send;} // no commentchar
line.erase((iend));
line.erase(0,istart);
return length;
}
// Function: tokenize, taken from web
//
// A simple and robust string tokenizer, extracts a vector of
// tokens from a string (str) delimited by delims
//
vector<string> StringTools::tokenize(string str, string delims)
{
string::size_type start_index, end_index;
vector<string> ret;
// Skip leading delimiters, to get to the first token
start_index = str.find_first_not_of(delims);
// While found a beginning of a new token
//
while (start_index != string::npos)
{
// Find the end of this token
end_index = str.find_first_of(delims, start_index);
// If this is the end of the string
if (end_index == string::npos)
end_index = str.length();
ret.push_back(str.substr(start_index, end_index - start_index));
// Find beginning of the next token
start_index = str.find_first_not_of(delims, end_index);
}
return ret;
}
// checks whether s1 contains s2 as a substring
bool StringTools::contains(string s1, string s2){
int s2pos=s1.find(s2);
bool contains_string=true;
if (s2pos==string::npos) contains_string=false;
return contains_string;
}
// If an input line starts with # or % then it is a comment
bool StringTools::iscomment(string s){
const char *blank(" ");
// go to beginning of line
int index=s.find_first_not_of(blank);
if ( (s[index]=='#')||(s[index]=='%') ) {return true;} else {return false;}
}
// finds the string containing tag in the file
// useful for locating the beginning of a particular section
// of input in a file
bool StringTools::findstr(ifstream &fstr, string tag){
bool found=false;
int linecount=0; // for debug purpose only
string line;
while(getline(fstr, line)){
linecount++;
found=contains(line, tag);
if (found) break;
}
return found;
}
// finds the string containing tag in the file
// useful for locating the beginning of a particular section
// of input in a file AND RTEURS THE FOUND STRING
bool StringTools::findstr(ifstream &fstr, string tag, string & outline){
bool found=false;
string line;
while(getline(fstr, line)){
// linecount++;
found=contains(line, tag);
if (found) {outline=line;break;}
}
return found;
}