1 |
+ |
/* |
2 |
+ |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
+ |
* |
4 |
+ |
* The University of Notre Dame grants you ("Licensee") a |
5 |
+ |
* non-exclusive, royalty free, license to use, modify and |
6 |
+ |
* redistribute this software in source and binary code form, provided |
7 |
+ |
* that the following conditions are met: |
8 |
+ |
* |
9 |
+ |
* 1. Redistributions of source code must retain the above copyright |
10 |
+ |
* notice, this list of conditions and the following disclaimer. |
11 |
+ |
* |
12 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
+ |
* notice, this list of conditions and the following disclaimer in the |
14 |
+ |
* documentation and/or other materials provided with the |
15 |
+ |
* distribution. |
16 |
+ |
* |
17 |
+ |
* This software is provided "AS IS," without a warranty of any |
18 |
+ |
* kind. All express or implied conditions, representations and |
19 |
+ |
* warranties, including any implied warranty of merchantability, |
20 |
+ |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
+ |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
+ |
* be liable for any damages suffered by licensee as a result of |
23 |
+ |
* using, modifying or distributing the software or its |
24 |
+ |
* derivatives. In no event will the University of Notre Dame or its |
25 |
+ |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
+ |
* direct, indirect, special, consequential, incidental or punitive |
27 |
+ |
* damages, however caused and regardless of the theory of liability, |
28 |
+ |
* arising out of the use of or inability to use software, even if the |
29 |
+ |
* University of Notre Dame has been advised of the possibility of |
30 |
+ |
* such damages. |
31 |
+ |
* |
32 |
+ |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
+ |
* research, please cite the appropriate papers when you publish your |
34 |
+ |
* work. Good starting points are: |
35 |
+ |
* |
36 |
+ |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
+ |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
+ |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
+ |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
+ |
*/ |
41 |
+ |
|
42 |
+ |
#include <algorithm> |
43 |
|
#include "utils/StringUtils.hpp" |
44 |
|
|
45 |
< |
using namespace std; |
46 |
< |
using namespace oopse; |
47 |
< |
|
48 |
< |
string UpperCase(const string& S) { |
49 |
< |
string uc = S; |
50 |
< |
unsigned int n = uc.size(); |
51 |
< |
for (unsigned int j = 0; j < n; j++) { |
52 |
< |
char sj = uc[j]; |
53 |
< |
if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A')); |
45 |
> |
namespace OpenMD { |
46 |
> |
std::string UpperCase(const std::string& S) { |
47 |
> |
std::string uc = S; |
48 |
> |
unsigned int n = uc.size(); |
49 |
> |
for (unsigned int j = 0; j < n; j++) { |
50 |
> |
char sj = uc[j]; |
51 |
> |
if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A')); |
52 |
> |
} |
53 |
> |
return uc; |
54 |
|
} |
13 |
– |
return uc; |
14 |
– |
} |
55 |
|
|
56 |
< |
string LowerCase(const string& S) { |
57 |
< |
string lc = S; |
58 |
< |
unsigned int n = lc.size(); |
59 |
< |
for (unsigned int j = 0; j < n; j++) { |
60 |
< |
char sj = lc[j]; |
61 |
< |
if (sj >= 'A' && sj <= 'Z') lc[j] = (char)(sj + ('a' - 'A')); |
56 |
> |
std::string LowerCase(const std::string& S) { |
57 |
> |
std::string lc = S; |
58 |
> |
unsigned int n = lc.size(); |
59 |
> |
for (unsigned int j = 0; j < n; j++) { |
60 |
> |
char sj = lc[j]; |
61 |
> |
if (sj >= 'A' && sj <= 'Z') lc[j] = (char)(sj + ('a' - 'A')); |
62 |
> |
} |
63 |
> |
return lc; |
64 |
|
} |
23 |
– |
return lc; |
24 |
– |
} |
65 |
|
|
66 |
< |
int findBegin(istream theStream, char* startText ){ |
67 |
< |
const int MAXLEN = 1024; |
68 |
< |
char readLine[MAXLEN]; |
29 |
< |
int foundText = 0; |
30 |
< |
int lineNum; |
31 |
< |
char* the_token; |
32 |
< |
char* eof_test; |
66 |
> |
char* trimSpaces(char *str) { |
67 |
> |
size_t len; |
68 |
> |
char *right, *left; |
69 |
|
|
70 |
< |
// rewind the stream |
35 |
< |
theStream.seekg (0, ios::beg); |
36 |
< |
lineNum = 0; |
70 |
> |
if (strlen(str) == 0) return(str); |
71 |
|
|
72 |
< |
if (!theStream.eof()) { |
73 |
< |
theStream.getline(readLine, MAXLEN); |
74 |
< |
lineNum++; |
75 |
< |
} else { |
76 |
< |
printf( "Error fast forwarding stream: stream is empty.\n"); |
77 |
< |
return -1; |
72 |
> |
/* Trim whitespace from left side */ |
73 |
> |
for (left = str; isspace(*left); left++); |
74 |
> |
|
75 |
> |
/* Trim whitespace from right side */ |
76 |
> |
if ((len = strlen(left))) |
77 |
> |
{ |
78 |
> |
right = left + (len - 1); |
79 |
> |
|
80 |
> |
while (isspace(*right)) |
81 |
> |
{ |
82 |
> |
*right = '\0'; |
83 |
> |
right--; |
84 |
> |
} |
85 |
> |
} |
86 |
> |
|
87 |
> |
/* Only do the str copy if there were spaces to the left */ |
88 |
> |
if (left != str) |
89 |
> |
strcpy(str, left); |
90 |
> |
|
91 |
> |
return (str); |
92 |
|
} |
93 |
+ |
|
94 |
+ |
int findBegin(std::istream &theStream, const char* startText ){ |
95 |
+ |
const int MAXLEN = 1024; |
96 |
+ |
char readLine[MAXLEN]; |
97 |
+ |
int foundText = 0; |
98 |
+ |
int lineNum; |
99 |
+ |
char* the_token; |
100 |
+ |
char* eof_test; |
101 |
+ |
|
102 |
+ |
// rewind the stream |
103 |
+ |
theStream.seekg (0, std::ios::beg); |
104 |
+ |
lineNum = 0; |
105 |
|
|
106 |
< |
while ( !foundText ) { |
107 |
< |
|
108 |
< |
if (theStream.eof()) { |
109 |
< |
printf("Error fast forwarding stream at line %d: " |
110 |
< |
"stream ended unexpectedly.\n", lineNum); |
106 |
> |
if (!theStream.eof()) { |
107 |
> |
theStream.getline(readLine, MAXLEN); |
108 |
> |
lineNum++; |
109 |
> |
} else { |
110 |
> |
printf( "Error fast forwarding stream: stream is empty.\n"); |
111 |
|
return -1; |
112 |
|
} |
113 |
< |
|
114 |
< |
the_token = strtok( readLine, " ,;\t" ); |
115 |
< |
|
116 |
< |
if (!strcasecmp("begin", the_token)) { |
117 |
< |
the_token = TrimSpaces(strtok( NULL, " ,;\t" )); |
118 |
< |
foundText = !strcasecmp( startText, the_token ); |
59 |
< |
} |
60 |
< |
|
61 |
< |
if (!foundText) { |
62 |
< |
if (!theStream.eof()) { |
63 |
< |
theStream.getline(readLine, MAXLEN); |
64 |
< |
lineNum++; |
65 |
< |
} else { |
66 |
< |
printf( "Error fast forwarding stream at line %d: " |
67 |
< |
"stream ended unexpectedly.\n", lineNum); |
113 |
> |
|
114 |
> |
while ( !foundText ) { |
115 |
> |
|
116 |
> |
if (theStream.eof()) { |
117 |
> |
printf("Error fast forwarding stream at line %d: " |
118 |
> |
"stream ended unexpectedly.\n", lineNum); |
119 |
|
return -1; |
120 |
|
} |
121 |
+ |
|
122 |
+ |
the_token = strtok( readLine, " ,;\t" ); |
123 |
+ |
if ( the_token != NULL) |
124 |
+ |
if (!strcasecmp("begin", the_token)) { |
125 |
+ |
the_token = strtok( NULL, " ,;\t" ); |
126 |
+ |
if ( the_token != NULL){ |
127 |
+ |
foundText = !strcasecmp( startText, the_token ); |
128 |
+ |
} |
129 |
+ |
} |
130 |
+ |
|
131 |
+ |
if (!foundText) { |
132 |
+ |
if (!theStream.eof()) { |
133 |
+ |
theStream.getline(readLine, MAXLEN); |
134 |
+ |
lineNum++; |
135 |
+ |
} else { |
136 |
+ |
printf( "Error fast forwarding stream at line %d: " |
137 |
+ |
"stream ended unexpectedly.\n", lineNum); |
138 |
+ |
return -1; |
139 |
+ |
} |
140 |
+ |
} |
141 |
|
} |
142 |
+ |
return lineNum; |
143 |
|
} |
72 |
– |
return lineNum; |
73 |
– |
} |
144 |
|
|
145 |
< |
int countTokens(char *line, char *delimiters) { |
146 |
< |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
145 |
> |
int countTokens(char *line, char *delimiters) { |
146 |
> |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
147 |
|
|
148 |
< |
char *working_line; /* WORKING COPY OF LINE. */ |
149 |
< |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
150 |
< |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
148 |
> |
char *working_line; /* WORKING COPY OF LINE. */ |
149 |
> |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
150 |
> |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
151 |
|
|
152 |
< |
strtok_ptr= working_line= strdup(line); |
152 |
> |
strtok_ptr= working_line= strdup(line); |
153 |
|
|
154 |
< |
ntokens=0; |
155 |
< |
while (strtok(strtok_ptr,delimiters)!=NULL) |
156 |
< |
{ |
157 |
< |
ntokens++; |
158 |
< |
strtok_ptr=NULL; |
159 |
< |
} |
154 |
> |
ntokens=0; |
155 |
> |
while (strtok(strtok_ptr,delimiters)!=NULL) |
156 |
> |
{ |
157 |
> |
ntokens++; |
158 |
> |
strtok_ptr=NULL; |
159 |
> |
} |
160 |
|
|
161 |
< |
free(working_line); |
162 |
< |
return(ntokens); |
163 |
< |
} |
161 |
> |
free(working_line); |
162 |
> |
return(ntokens); |
163 |
> |
} |
164 |
|
|
165 |
< |
char* TrimSpaces(char *str) { |
166 |
< |
size_t len; |
167 |
< |
char *right, *left; |
165 |
> |
int isEndLine(char *line) { |
166 |
> |
char *working_line; |
167 |
> |
char *foo; |
168 |
|
|
169 |
< |
if (strlen(str) == 0) return(str); |
169 |
> |
working_line = strdup(line); |
170 |
|
|
171 |
< |
/* Trim whitespace from left side */ |
172 |
< |
for (left = str; isspace(*left); left++); |
171 |
> |
foo = strtok(working_line, " ,;\t"); |
172 |
> |
|
173 |
> |
if (foo != NULL) { |
174 |
> |
|
175 |
> |
if (!strcasecmp(foo, "end")) return 1; |
176 |
> |
|
177 |
> |
} |
178 |
> |
|
179 |
> |
return 0; |
180 |
> |
} |
181 |
|
|
182 |
< |
/* Trim whitespace from right side */ |
183 |
< |
if ((len = strlen(left))) |
184 |
< |
{ |
185 |
< |
right = left + (len - 1); |
186 |
< |
|
187 |
< |
while (isspace(*right)) |
110 |
< |
{ |
111 |
< |
*right = '\0'; |
112 |
< |
right--; |
113 |
< |
} |
182 |
> |
std::string OpenMD_itoa(int value, unsigned int base) { |
183 |
> |
const char digitMap[] = "0123456789abcdef"; |
184 |
> |
std::string buf; |
185 |
> |
|
186 |
> |
if (base == 0 || base > 16) { |
187 |
> |
return buf; |
188 |
|
} |
189 |
+ |
|
190 |
+ |
if (value == 0) { |
191 |
+ |
buf = "0"; |
192 |
+ |
return buf; |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
// Take care negative int: |
196 |
+ |
|
197 |
+ |
std::string sign; |
198 |
+ |
int _value = value; |
199 |
+ |
if (value < 0) { |
200 |
+ |
_value = -value; |
201 |
+ |
sign = "-"; |
202 |
+ |
} |
203 |
+ |
|
204 |
+ |
// Translating number to string with base: |
205 |
+ |
for (int i = 30; _value && i ; --i) { |
206 |
+ |
buf = digitMap[ _value % base ] + buf; |
207 |
+ |
_value /= base; |
208 |
+ |
} |
209 |
+ |
return sign.append(buf); |
210 |
+ |
} |
211 |
+ |
|
212 |
+ |
|
213 |
+ |
std::string getPrefix(const std::string& str) { |
214 |
+ |
return str.substr(0, str.rfind('.')); |
215 |
+ |
} |
216 |
+ |
|
217 |
+ |
std::string getSuffix(const std::string& str) { |
218 |
+ |
return str.substr(0, str.find('.')); |
219 |
+ |
} |
220 |
+ |
|
221 |
+ |
bool isInteger(const std::string& str) { |
222 |
+ |
|
223 |
+ |
bool result = false; |
224 |
+ |
|
225 |
+ |
std::string::const_iterator i = str.begin(); |
226 |
+ |
if (i != str.end() && (*i == '+' || *i == '-' || std::isdigit(*i) )) { |
227 |
+ |
++i; |
228 |
+ |
while (i != str.end() && std::isdigit(*i)) |
229 |
+ |
++i; |
230 |
+ |
if (i == str.end()) |
231 |
+ |
result = true; |
232 |
+ |
} |
233 |
+ |
|
234 |
+ |
return result; |
235 |
+ |
} |
236 |
|
|
237 |
< |
/* Only do the str copy if their was spaces to the left */ |
238 |
< |
if (left != str) |
118 |
< |
strcpy(str, left); |
119 |
< |
|
120 |
< |
return (str); |
237 |
> |
bool CaseInsensitiveEquals(const char ch1, const char ch2) { |
238 |
> |
return std::toupper((unsigned char)ch1) == std::toupper((unsigned char)ch2); |
239 |
|
} |
240 |
|
|
241 |
< |
int isEndLine(char *line) { |
242 |
< |
char *working_line; |
243 |
< |
char *foo; |
244 |
< |
|
245 |
< |
working_line = strdup(line); |
246 |
< |
|
247 |
< |
foo = strtok(working_line, " ,;\t"); |
241 |
> |
size_t CaseInsensitiveFind(const std::string& str1, const std::string& str2) { |
242 |
> |
std::string::const_iterator pos = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), CaseInsensitiveEquals); |
243 |
> |
if (pos == str1.end()) |
244 |
> |
return std::string::npos; |
245 |
> |
else |
246 |
> |
return pos - str1.begin(); |
247 |
> |
} |
248 |
|
|
249 |
< |
if (!strcasecmp(foo, "end")) return 1; |
250 |
< |
|
251 |
< |
return 0; |
249 |
> |
/** |
250 |
> |
* memparse - parse a string with mem suffixes into a number |
251 |
> |
* @ptr: Where parse begins |
252 |
> |
* @retptr: (output) Pointer to next char after parse completes |
253 |
> |
* |
254 |
> |
* Parses a string into a number. The number stored at @ptr is |
255 |
> |
* potentially suffixed with %K (for kilobytes, or 1024 bytes), |
256 |
> |
* %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or |
257 |
> |
* 1073741824). If the number is suffixed with K, M, or G, then |
258 |
> |
* the return value is the number multiplied by one kilobyte, one |
259 |
> |
* megabyte, or one gigabyte, respectively. |
260 |
> |
*/ |
261 |
> |
unsigned long long memparse (char *ptr, char **retptr) { |
262 |
> |
unsigned long long ret = strtoull (ptr, retptr, 0); |
263 |
> |
|
264 |
> |
switch (**retptr) { |
265 |
> |
case 'G': |
266 |
> |
case 'g': |
267 |
> |
ret <<= 10; |
268 |
> |
case 'M': |
269 |
> |
case 'm': |
270 |
> |
ret <<= 10; |
271 |
> |
case 'K': |
272 |
> |
case 'k': |
273 |
> |
ret <<= 10; |
274 |
> |
(*retptr)++; |
275 |
> |
default: |
276 |
> |
break; |
277 |
> |
} |
278 |
> |
return ret; |
279 |
> |
} |
280 |
> |
|
281 |
|
} |