1 |
< |
/* |
1 |
> |
/* |
2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
|
* The University of Notre Dame grants you ("Licensee") a |
6 |
|
* redistribute this software in source and binary code form, provided |
7 |
|
* that the following conditions are met: |
8 |
|
* |
9 |
< |
* 1. Acknowledgement of the program authors must be made in any |
10 |
< |
* publication of scientific results based in part on use of the |
11 |
< |
* program. An acceptable form of acknowledgement is citation of |
12 |
< |
* the article in which the program was described (Matthew |
13 |
< |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
< |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
< |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
< |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
< |
* |
18 |
< |
* 2. Redistributions of source code must retain the above copyright |
9 |
> |
* 1. Redistributions of source code must retain the above copyright |
10 |
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
* |
12 |
< |
* 3. Redistributions in binary form must reproduce the above copyright |
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. |
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] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
+ |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
|
*/ |
42 |
< |
|
42 |
> |
|
43 |
> |
#include <algorithm> |
44 |
> |
#include <stdlib.h> |
45 |
|
#include "utils/StringUtils.hpp" |
46 |
|
|
47 |
< |
namespace oopse { |
48 |
< |
std::string UpperCase(const std::string& S) { |
49 |
< |
std::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')); |
47 |
> |
namespace OpenMD { |
48 |
> |
std::string UpperCase(const std::string& S) { |
49 |
> |
std::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')); |
54 |
> |
} |
55 |
> |
return uc; |
56 |
|
} |
52 |
– |
return uc; |
53 |
– |
} |
57 |
|
|
58 |
< |
std::string LowerCase(const std::string& S) { |
59 |
< |
std::string lc = S; |
60 |
< |
unsigned int n = lc.size(); |
61 |
< |
for (unsigned int j = 0; j < n; j++) { |
62 |
< |
char sj = lc[j]; |
63 |
< |
if (sj >= 'A' && sj <= 'Z') lc[j] = (char)(sj + ('a' - 'A')); |
58 |
> |
std::string LowerCase(const std::string& S) { |
59 |
> |
std::string lc = S; |
60 |
> |
unsigned int n = lc.size(); |
61 |
> |
for (unsigned int j = 0; j < n; j++) { |
62 |
> |
char sj = lc[j]; |
63 |
> |
if (sj >= 'A' && sj <= 'Z') lc[j] = (char)(sj + ('a' - 'A')); |
64 |
> |
} |
65 |
> |
return lc; |
66 |
|
} |
62 |
– |
return lc; |
63 |
– |
} |
67 |
|
|
68 |
< |
char* trimSpaces(char *str) { |
69 |
< |
size_t len; |
70 |
< |
char *right, *left; |
68 |
> |
char* trimSpaces(char *str) { |
69 |
> |
size_t len; |
70 |
> |
char *right, *left; |
71 |
|
|
72 |
< |
if (strlen(str) == 0) return(str); |
72 |
> |
if (strlen(str) == 0) return(str); |
73 |
|
|
74 |
< |
/* Trim whitespace from left side */ |
75 |
< |
for (left = str; isspace(*left); left++); |
74 |
> |
/* Trim whitespace from left side */ |
75 |
> |
for (left = str; isspace(*left); left++); |
76 |
|
|
77 |
< |
/* Trim whitespace from right side */ |
78 |
< |
if ((len = strlen(left))) |
79 |
< |
{ |
80 |
< |
right = left + (len - 1); |
77 |
> |
/* Trim whitespace from right side */ |
78 |
> |
if ((len = strlen(left))) |
79 |
> |
{ |
80 |
> |
right = left + (len - 1); |
81 |
|
|
82 |
< |
while (isspace(*right)) |
83 |
< |
{ |
84 |
< |
*right = '\0'; |
85 |
< |
right--; |
86 |
< |
} |
87 |
< |
} |
82 |
> |
while (isspace(*right)) |
83 |
> |
{ |
84 |
> |
*right = '\0'; |
85 |
> |
right--; |
86 |
> |
} |
87 |
> |
} |
88 |
|
|
89 |
< |
/* Only do the str copy if there were spaces to the left */ |
90 |
< |
if (left != str) |
91 |
< |
strcpy(str, left); |
89 |
> |
/* Only do the str copy if there were spaces to the left */ |
90 |
> |
if (left != str) |
91 |
> |
strcpy(str, left); |
92 |
|
|
93 |
< |
return (str); |
94 |
< |
} |
93 |
> |
return (str); |
94 |
> |
} |
95 |
|
|
96 |
< |
int findBegin(std::istream &theStream, char* startText ){ |
97 |
< |
const int MAXLEN = 1024; |
98 |
< |
char readLine[MAXLEN]; |
99 |
< |
int foundText = 0; |
100 |
< |
int lineNum; |
101 |
< |
char* the_token; |
102 |
< |
char* eof_test; |
96 |
> |
int findBegin(std::istream &theStream, const char* startText ){ |
97 |
> |
const int MAXLEN = 1024; |
98 |
> |
char readLine[MAXLEN]; |
99 |
> |
int foundText = 0; |
100 |
> |
int lineNum; |
101 |
> |
char* the_token; |
102 |
> |
char* eof_test; |
103 |
|
|
104 |
< |
// rewind the stream |
105 |
< |
theStream.seekg (0, std::ios::beg); |
106 |
< |
lineNum = 0; |
104 |
> |
// rewind the stream |
105 |
> |
theStream.seekg (0, std::ios::beg); |
106 |
> |
lineNum = 0; |
107 |
|
|
108 |
< |
if (!theStream.eof()) { |
109 |
< |
theStream.getline(readLine, MAXLEN); |
110 |
< |
lineNum++; |
111 |
< |
} else { |
112 |
< |
printf( "Error fast forwarding stream: stream is empty.\n"); |
110 |
< |
return -1; |
111 |
< |
} |
112 |
< |
|
113 |
< |
while ( !foundText ) { |
114 |
< |
|
115 |
< |
if (theStream.eof()) { |
116 |
< |
printf("Error fast forwarding stream at line %d: " |
117 |
< |
"stream ended unexpectedly.\n", lineNum); |
108 |
> |
if (!theStream.eof()) { |
109 |
> |
theStream.getline(readLine, MAXLEN); |
110 |
> |
lineNum++; |
111 |
> |
} else { |
112 |
> |
printf( "Error fast forwarding stream: stream is empty.\n"); |
113 |
|
return -1; |
114 |
|
} |
115 |
+ |
|
116 |
+ |
while ( !foundText ) { |
117 |
|
|
118 |
< |
the_token = strtok( readLine, " ,;\t" ); |
119 |
< |
if ( the_token != NULL) |
120 |
< |
if (!strcasecmp("begin", the_token)) { |
121 |
< |
the_token = strtok( NULL, " ,;\t" ); |
125 |
< |
if ( the_token != NULL){ |
126 |
< |
foundText = !strcasecmp( startText, the_token ); |
127 |
< |
} |
118 |
> |
if (theStream.eof()) { |
119 |
> |
printf("Error fast forwarding stream at line %d: " |
120 |
> |
"stream ended unexpectedly.\n", lineNum); |
121 |
> |
return -1; |
122 |
|
} |
123 |
|
|
124 |
< |
if (!foundText) { |
125 |
< |
if (!theStream.eof()) { |
126 |
< |
theStream.getline(readLine, MAXLEN); |
127 |
< |
lineNum++; |
128 |
< |
} else { |
129 |
< |
printf( "Error fast forwarding stream at line %d: " |
130 |
< |
"stream ended unexpectedly.\n", lineNum); |
131 |
< |
return -1; |
124 |
> |
the_token = strtok( readLine, " ,;\t" ); |
125 |
> |
if ( the_token != NULL) |
126 |
> |
if (!strcasecmp("begin", the_token)) { |
127 |
> |
the_token = strtok( NULL, " ,;\t" ); |
128 |
> |
if ( the_token != NULL){ |
129 |
> |
foundText = !strcasecmp( startText, the_token ); |
130 |
> |
} |
131 |
> |
} |
132 |
> |
|
133 |
> |
if (!foundText) { |
134 |
> |
if (!theStream.eof()) { |
135 |
> |
theStream.getline(readLine, MAXLEN); |
136 |
> |
lineNum++; |
137 |
> |
} else { |
138 |
> |
printf( "Error fast forwarding stream at line %d: " |
139 |
> |
"stream ended unexpectedly.\n", lineNum); |
140 |
> |
return -1; |
141 |
> |
} |
142 |
|
} |
143 |
|
} |
144 |
+ |
return lineNum; |
145 |
|
} |
141 |
– |
return lineNum; |
142 |
– |
} |
146 |
|
|
147 |
< |
int countTokens(char *line, char *delimiters) { |
148 |
< |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
147 |
> |
int countTokens(char *line, char *delimiters) { |
148 |
> |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
149 |
|
|
150 |
< |
char *working_line; /* WORKING COPY OF LINE. */ |
151 |
< |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
152 |
< |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
150 |
> |
char *working_line; /* WORKING COPY OF LINE. */ |
151 |
> |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
152 |
> |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
153 |
|
|
154 |
< |
strtok_ptr= working_line= strdup(line); |
154 |
> |
strtok_ptr= working_line= strdup(line); |
155 |
|
|
156 |
< |
ntokens=0; |
157 |
< |
while (strtok(strtok_ptr,delimiters)!=NULL) |
158 |
< |
{ |
159 |
< |
ntokens++; |
160 |
< |
strtok_ptr=NULL; |
161 |
< |
} |
156 |
> |
ntokens=0; |
157 |
> |
while (strtok(strtok_ptr,delimiters)!=NULL) |
158 |
> |
{ |
159 |
> |
ntokens++; |
160 |
> |
strtok_ptr=NULL; |
161 |
> |
} |
162 |
|
|
163 |
< |
free(working_line); |
164 |
< |
return(ntokens); |
165 |
< |
} |
163 |
> |
free(working_line); |
164 |
> |
return(ntokens); |
165 |
> |
} |
166 |
|
|
167 |
< |
int isEndLine(char *line) { |
168 |
< |
char *working_line; |
169 |
< |
char *foo; |
167 |
> |
int isEndLine(char *line) { |
168 |
> |
char *working_line; |
169 |
> |
char *foo; |
170 |
|
|
171 |
< |
working_line = strdup(line); |
171 |
> |
working_line = strdup(line); |
172 |
|
|
173 |
< |
foo = strtok(working_line, " ,;\t"); |
173 |
> |
foo = strtok(working_line, " ,;\t"); |
174 |
|
|
175 |
< |
if (foo != NULL) { |
175 |
> |
if (foo != NULL) { |
176 |
|
|
177 |
< |
if (!strcasecmp(foo, "end")) return 1; |
177 |
> |
if (!strcasecmp(foo, "end")) return 1; |
178 |
|
|
179 |
< |
} |
179 |
> |
} |
180 |
|
|
181 |
< |
return 0; |
182 |
< |
} |
181 |
> |
return 0; |
182 |
> |
} |
183 |
> |
|
184 |
> |
std::string OpenMD_itoa(int value, unsigned int base) { |
185 |
> |
const char digitMap[] = "0123456789abcdef"; |
186 |
> |
std::string buf; |
187 |
|
|
188 |
+ |
if (base == 0 || base > 16) { |
189 |
+ |
return buf; |
190 |
+ |
} |
191 |
|
|
192 |
< |
std::string getPrefix(const std::string& str) { |
193 |
< |
return str.substr(0, str.rfind('.')); |
194 |
< |
} |
192 |
> |
if (value == 0) { |
193 |
> |
buf = "0"; |
194 |
> |
return buf; |
195 |
> |
} |
196 |
> |
|
197 |
> |
// Take care negative int: |
198 |
> |
|
199 |
> |
std::string sign; |
200 |
> |
int _value = value; |
201 |
> |
if (value < 0) { |
202 |
> |
_value = -value; |
203 |
> |
sign = "-"; |
204 |
> |
} |
205 |
|
|
206 |
< |
std::string getSuffix(const std::string& str) { |
206 |
> |
// Translating number to string with base: |
207 |
> |
for (int i = 30; _value && i ; --i) { |
208 |
> |
buf = digitMap[ _value % base ] + buf; |
209 |
> |
_value /= base; |
210 |
> |
} |
211 |
> |
return sign.append(buf); |
212 |
> |
} |
213 |
> |
|
214 |
> |
|
215 |
> |
std::string getPrefix(const std::string& str) { |
216 |
> |
return str.substr(0, str.rfind('.')); |
217 |
> |
} |
218 |
> |
|
219 |
> |
std::string getSuffix(const std::string& str) { |
220 |
|
return str.substr(0, str.find('.')); |
221 |
< |
} |
221 |
> |
} |
222 |
> |
|
223 |
> |
bool isInteger(const std::string& str) { |
224 |
> |
|
225 |
> |
bool result = false; |
226 |
> |
|
227 |
> |
std::string::const_iterator i = str.begin(); |
228 |
> |
if (i != str.end() && (*i == '+' || *i == '-' || std::isdigit(*i) )) { |
229 |
> |
++i; |
230 |
> |
while (i != str.end() && std::isdigit(*i)) |
231 |
> |
++i; |
232 |
> |
if (i == str.end()) |
233 |
> |
result = true; |
234 |
> |
} |
235 |
> |
|
236 |
> |
return result; |
237 |
> |
} |
238 |
|
|
239 |
+ |
bool CaseInsensitiveEquals(const char ch1, const char ch2) { |
240 |
+ |
return std::toupper((unsigned char)ch1) == std::toupper((unsigned char)ch2); |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
size_t CaseInsensitiveFind(const std::string& str1, const std::string& str2) { |
244 |
+ |
std::string::const_iterator pos = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), CaseInsensitiveEquals); |
245 |
+ |
if (pos == str1.end()) |
246 |
+ |
return std::string::npos; |
247 |
+ |
else |
248 |
+ |
return pos - str1.begin(); |
249 |
+ |
} |
250 |
+ |
|
251 |
+ |
/** |
252 |
+ |
* memparse - parse a string with mem suffixes into a number |
253 |
+ |
* @ptr: Where parse begins |
254 |
+ |
* @retptr: (output) Pointer to next char after parse completes |
255 |
+ |
* |
256 |
+ |
* Parses a string into a number. The number stored at @ptr is |
257 |
+ |
* potentially suffixed with %K (for kilobytes, or 1024 bytes), |
258 |
+ |
* %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or |
259 |
+ |
* 1073741824). If the number is suffixed with K, M, or G, then |
260 |
+ |
* the return value is the number multiplied by one kilobyte, one |
261 |
+ |
* megabyte, or one gigabyte, respectively. |
262 |
+ |
*/ |
263 |
+ |
unsigned long long memparse (char *ptr, char **retptr) { |
264 |
+ |
unsigned long long ret = strtoull (ptr, retptr, 0); |
265 |
+ |
|
266 |
+ |
switch (**retptr) { |
267 |
+ |
case 'G': |
268 |
+ |
case 'g': |
269 |
+ |
ret <<= 10; |
270 |
+ |
case 'M': |
271 |
+ |
case 'm': |
272 |
+ |
ret <<= 10; |
273 |
+ |
case 'K': |
274 |
+ |
case 'k': |
275 |
+ |
ret <<= 10; |
276 |
+ |
(*retptr)++; |
277 |
+ |
default: |
278 |
+ |
break; |
279 |
+ |
} |
280 |
+ |
return ret; |
281 |
+ |
} |
282 |
+ |
|
283 |
|
} |