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 246 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
branches/development/src/utils/StringUtils.cpp (file contents), Revision 1808 by gezelter, Mon Oct 22 20:42:10 2012 UTC

# Line 1 | Line 1
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
# Line 6 | Line 6
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.
# Line 37 | Line 28
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 "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 < namespace oopse {
52 < std::string UpperCase(const std::string& S) {
53 <  std::string uc = S;
54 <  unsigned int n = uc.size();
55 <  for (unsigned int j = 0; j < n; j++) {  
56 <    char sj = uc[j];
57 <    if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A'));
51 > #ifdef _MSC_VER
52 > #define strcasecmp _stricmp
53 > #define strdup _strdup
54 > #define strtoull _strtoui64
55 > #endif
56 >
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    }
52  return uc;
53 }
68  
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'));
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    }
62  return lc;
63 }
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(std::istream &theStream, char* startText ){
108 <  const int MAXLEN = 1024;
109 <  char readLine[MAXLEN];  
110 <  int foundText = 0;
111 <  int lineNum;
112 <  char* the_token;
99 <  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  
114 <  // rewind the stream
115 <  theStream.seekg (0, std::ios::beg);
116 <  lineNum = 0;
114 >    // rewind the stream
115 >    theStream.seekg (0, std::ios::beg);
116 >    lineNum = 0;
117    
118 <  if (!theStream.eof()) {
119 <    theStream.getline(readLine, MAXLEN);
120 <    lineNum++;
121 <  } else {
122 <    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);
118 >    if (!theStream.eof()) {
119 >      theStream.getline(readLine, MAXLEN);
120 >      lineNum++;
121 >    } else {
122 >      printf( "Error fast forwarding stream: stream is empty.\n");
123        return -1;
124      }
125 +  
126 +    while ( !foundText ) {
127      
128 <    the_token = strtok( readLine, " ,;\t" );
129 <    if ( the_token != NULL)
130 <      if (!strcasecmp("begin", the_token)) {
131 <        the_token = strtok( NULL, " ,;\t" );
125 <        if ( the_token != NULL){
126 <          foundText = !strcasecmp( startText, the_token );
127 <        }
128 >      if (theStream.eof()) {
129 >        printf("Error fast forwarding stream at line %d: "
130 >               "stream ended unexpectedly.\n", lineNum);
131 >        return -1;
132        }
133      
134 <    if (!foundText) {
135 <      if (!theStream.eof()) {
136 <        theStream.getline(readLine, MAXLEN);
137 <        lineNum++;
138 <      } else {
139 <        printf( "Error fast forwarding stream at line %d: "
140 <                "stream ended unexpectedly.\n", lineNum);
141 <        return -1;
134 >      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        }
153      }
154 +    return lineNum;
155    }
141  return lineNum;
142 }
156  
157 < int countTokens(char *line, char *delimiters) {
158 <  /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */
157 >  int countTokens(char *line, char *delimiters) {
158 >    /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */
159    
160 <  char *working_line;   /* WORKING COPY OF LINE. */
161 <  int ntokens;          /* NUMBER OF TOKENS FOUND IN LINE. */
162 <  char *strtok_ptr;     /* POINTER FOR STRTOK. */
160 >    char *working_line;   /* WORKING COPY OF LINE. */
161 >    int ntokens;          /* NUMBER OF TOKENS FOUND IN LINE. */
162 >    char *strtok_ptr;     /* POINTER FOR STRTOK. */
163  
164 <  strtok_ptr= working_line= strdup(line);
164 >    strtok_ptr= working_line= strdup(line);
165  
166 <  ntokens=0;
167 <  while (strtok(strtok_ptr,delimiters)!=NULL)
168 <    {
169 <      ntokens++;
170 <      strtok_ptr=NULL;
171 <    }
166 >    ntokens=0;
167 >    while (strtok(strtok_ptr,delimiters)!=NULL)
168 >      {
169 >        ntokens++;
170 >        strtok_ptr=NULL;
171 >      }
172  
173 <  free(working_line);
174 <  return(ntokens);
175 < }
173 >    free(working_line);
174 >    return(ntokens);
175 >  }
176  
177 < int isEndLine(char *line) {
178 <  char *working_line;
179 <  char *foo;
177 >  int isEndLine(char *line) {
178 >    char *working_line;
179 >    char *foo;
180    
181 <  working_line = strdup(line);
181 >    working_line = strdup(line);
182    
183 <  foo = strtok(working_line, " ,;\t");
183 >    foo = strtok(working_line, " ,;\t");
184  
185 <  if (foo != NULL) {
185 >    if (foo != NULL) {
186  
187 <    if (!strcasecmp(foo, "end")) return 1;
187 >      if (!strcasecmp(foo, "end")) return 1;
188  
189 <  }
189 >    }
190  
191 < return 0;
192 < }
191 >    return 0;
192 >  }
193 >  
194 >  std::string OpenMD_itoa(int value, unsigned int base) {    
195 >    const char digitMap[] = "0123456789abcdef";
196 >    std::string buf;
197  
198 +    if (base == 0 || base > 16) {      
199 +      return buf;
200 +    }
201  
202 < std::string getPrefix(const std::string& str) {
203 <  return str.substr(0, str.rfind('.'));
204 < }
202 >    if (value == 0) {
203 >      buf = "0";
204 >      return buf;
205 >    }
206 >    
207 >    // Take care negative int:
208 >        
209 >    std::string sign;    
210 >    int _value = value;
211 >    if (value < 0) {
212 >      _value = -value;
213 >      sign = "-";
214 >    }
215  
216 < std::string getSuffix(const std::string& str) {
216 >    // Translating number to string with base:
217 >    for (int i = 30; _value && i ; --i) {
218 >      buf = digitMap[ _value % base ] + buf;
219 >      _value /= base;
220 >    }
221 >    return sign.append(buf);
222 >  }
223 >
224 >
225 >  std::string getPrefix(const std::string& str) {
226 >    return str.substr(0, str.rfind('.'));
227 >  }
228 >
229 >  std::string getSuffix(const std::string& str) {
230      return str.substr(0, str.find('.'));
231 < }
231 >  }
232 >  
233 >  bool isInteger(const std::string& str) {
234 >    
235 >    bool result = false;
236 >    
237 >    std::string::const_iterator i = str.begin();    
238 >    if (i != str.end() && (*i == '+' || *i == '-' || std::isdigit(*i) )) {
239 >      ++i;        
240 >      while (i != str.end() && std::isdigit(*i))
241 >        ++i;
242 >      if (i == str.end())
243 >        result = true;
244 >    }
245 >    
246 >    return result;
247 >  }
248  
249 +  bool CaseInsensitiveEquals(const char ch1, const char ch2) {
250 +    return std::toupper((unsigned char)ch1) == std::toupper((unsigned char)ch2);
251 +  }
252 +  
253 +  size_t CaseInsensitiveFind(const std::string& str1, const std::string& str2) {
254 +    std::string::const_iterator pos = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(), CaseInsensitiveEquals);
255 +    if (pos == str1.end())
256 +      return std::string::npos;
257 +    else
258 +      return pos - str1.begin();
259 +  }
260 +  
261 +  /**
262 +   *    memparse - parse a string with mem suffixes into a number
263 +   *    @param ptr: Where parse begins
264 +   *    @param retptr: (output) Pointer to next char after parse completes
265 +   *
266 +   *    Parses a string into a number.  The number stored at @param ptr is
267 +   *    potentially suffixed with %K (for kilobytes, or 1024 bytes),
268 +   *    %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
269 +   *    1073741824).  If the number is suffixed with K, M, or G, then
270 +   *    the return value is the number multiplied by one kilobyte, one
271 +   *    megabyte, or one gigabyte, respectively.
272 +   */  
273 +  unsigned long long memparse (char *ptr,  char **retptr) {
274 +    unsigned long long ret = strtoull (ptr, retptr, 0);
275 +    
276 +    switch (**retptr) {
277 +    case 'G':
278 +    case 'g':
279 +      ret <<= 10;
280 +    case 'M':
281 +    case 'm':
282 +      ret <<= 10;
283 +    case 'K':
284 +    case 'k':
285 +      ret <<= 10;
286 +      (*retptr)++;
287 +    default:
288 +      break;
289 +    }
290 +    return ret;
291 +  }
292 +  
293   }

Comparing:
trunk/src/utils/StringUtils.cpp (property svn:keywords), Revision 246 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
branches/development/src/utils/StringUtils.cpp (property svn:keywords), Revision 1808 by gezelter, Mon Oct 22 20:42:10 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines