ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/StringUtils.cpp
(Generate patch)

Comparing:
trunk/src/utils/StringUtils.cpp (file contents), Revision 107 by gezelter, Tue Oct 19 20:40:30 2004 UTC vs.
branches/development/src/utils/StringUtils.cpp (file contents), Revision 1767 by gezelter, Fri Jul 6 22:01:58 2012 UTC

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

Comparing:
trunk/src/utils/StringUtils.cpp (property svn:keywords), Revision 107 by gezelter, Tue Oct 19 20:40:30 2004 UTC vs.
branches/development/src/utils/StringUtils.cpp (property svn:keywords), Revision 1767 by gezelter, Fri Jul 6 22:01:58 2012 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines