ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/StringUtils.cpp
Revision: 1874
Committed: Wed May 15 15:09:35 2013 UTC (11 years, 11 months ago) by gezelter
File size: 8313 byte(s)
Log Message:
Fixed a bunch of cppcheck warnings.

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date