ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/StringUtils.cpp
Revision: 1654
Committed: Wed Sep 28 19:40:45 2011 UTC (13 years, 7 months ago) by gezelter
File size: 7993 byte(s)
Log Message:
fixed a compile bug for PGI

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

Properties

Name Value
svn:keywords Author Id Revision Date