ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/utils/StringTokenizer.cpp
(Generate patch)

Comparing branches/new_design/OOPSE-3.0/src/utils/StringTokenizer.cpp (file contents):
Revision 1741 by tim, Tue Nov 16 02:07:14 2004 UTC vs.
Revision 1927 by tim, Wed Jan 12 17:14:35 2005 UTC

# Line 1 | Line 1
1 < /*
2 < * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project
3 < *
4 < * Contact: oopse@oopse.org
5 < *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU Lesser General Public License
8 < * as published by the Free Software Foundation; either version 2.1
9 < * of the License, or (at your option) any later version.
10 < * All we ask is that proper credit is given for our work, which includes
11 < * - but is not limited to - adding the above copyright notice to the beginning
12 < * of your source code files, and to any copyright notice that you may distribute
13 < * with programs based on this work.
14 < *
15 < * This program is distributed in the hope that it will be useful,
16 < * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 < * GNU Lesser General Public License for more details.
19 < *
20 < * You should have received a copy of the GNU Lesser General Public License
21 < * along with this program; if not, write to the Free Software
22 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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. 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
19 + *    notice, this list of conditions and the following disclaimer.
20 + *
21 + * 3. Redistributions in binary form must reproduce the above copyright
22 + *    notice, this list of conditions and the following disclaimer in the
23 + *    documentation and/or other materials provided with the
24 + *    distribution.
25 + *
26 + * This software is provided "AS IS," without a warranty of any
27 + * kind. All express or implied conditions, representations and
28 + * warranties, including any implied warranty of merchantability,
29 + * fitness for a particular purpose or non-infringement, are hereby
30 + * excluded.  The University of Notre Dame and its licensors shall not
31 + * be liable for any damages suffered by licensee as a result of
32 + * using, modifying or distributing the software or its
33 + * derivatives. In no event will the University of Notre Dame or its
34 + * licensors be liable for any lost revenue, profit or data, or for
35 + * direct, indirect, special, consequential, incidental or punitive
36 + * damages, however caused and regardless of the theory of liability,
37 + * arising out of the use of or inability to use software, even if the
38 + * University of Notre Dame has been advised of the possibility of
39 + * such damages.
40   */
41 <
41 >
42 > #include <iostream>
43 > #include <iterator>
44 > #include <sstream>
45   #include "utils/StringTokenizer.hpp"
46  
47   namespace oopse {
48  
30 StringTokenizer::defaultDelim = " \t\n\r";
49  
50   StringTokenizer::StringTokenizer(const std::string & str, const std::string & delim)
51                          : tokenString_(str), delim_(delim), returnTokens_(false),
# Line 37 | Line 55 | StringTokenizer::StringTokenizer(std::string::const_it
55  
56   StringTokenizer::StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last,
57                                                                 const std::string & delim)  
58 <                        : (tokenString_(first, last) , delim_(delim), returnTokens_(false),
58 >                        : tokenString_(first, last) , delim_(delim), returnTokens_(false),
59                             currentPos_(tokenString_.begin()), end_(tokenString_.end()) {
60  
61   }
# Line 49 | Line 67 | StringTokenizer::StringTokenizer(const std::string&str
67  
68   }
69  
70 + bool StringTokenizer::isDelimiter(const char c) {
71 +    return delim_.find(c) == std::string::npos ? false : true;
72 + }
73 +
74   int StringTokenizer::countTokens() {
75      
76 <    std::string::iterator tmpIter = currentPos_;    
76 >    std::string::const_iterator tmpIter = currentPos_;    
77      int numToken = 0;
78  
79      while (true) {
# Line 65 | Line 87 | int StringTokenizer::countTokens() {
87                  ++numToken;
88              }
89          }
90 <
90 >        
91 >        if (tmpIter == end_) {
92 >            break;
93 >        }
94 >        
95          //encount a token here
96          while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) {
97              ++tmpIter;
98          }
99  
100 <         if (tmpIter != end_) {
75 <            ++numToken;
76 <        } else {
77 <            break;
78 <        }
100 >        ++numToken;
101  
102      }
103  
# Line 89 | Line 111 | bool StringTokenizer::hasMoreTokens() {
111      } else if (returnTokens_) {
112          return true;
113      } else {
114 <        std::string::iterator i = currentPos_;
114 >        std::string::const_iterator i = currentPos_;
115  
116          //walk through the remaining string to check whether it contains non-delimeter or not
117          while(i != end_ && isDelimiter(*i)) {
# Line 104 | Line 126 | std::string StringTokenizer::nextToken() {
126      std::string result;
127      
128      if(currentPos_ != end_) {
129 <        std::insert_iterator<string> insertIter(result, result.begin());
129 >        std::insert_iterator<std::string> insertIter(result, result.begin());
130  
131          while( currentPos_ != end_ && isDelimiter(*currentPos_)) {
132  
# Line 125 | Line 147 | std::string StringTokenizer::nextToken() {
147      return result;
148   }
149  
150 + bool StringTokenizer::nextTokenAsBool() {
151 +    std::string token = nextToken();
152 +    std::istringstream iss(token);
153 +    bool result;
154 +    
155 +    if (iss >> result) {
156 +        return result;
157 +    } else {
158 +        std::cerr << "unable to convert " << token << " to a bool" << std::endl;
159 +        return false;
160 +    }
161 + }
162 +
163   int StringTokenizer::nextTokenAsInt() {
164      std::string token = nextToken();
165      std::istringstream iss(token);
# Line 133 | Line 168 | int StringTokenizer::nextTokenAsInt() {
168      if (iss >> result) {
169          return result;
170      } else {
171 <        std::err << "unable to convert " << token << "to an integer" << std::endl;
171 >        std::cerr << "unable to convert " << token << " to an integer" << std::endl;
172          return 0;
173      }
174   }
# Line 146 | Line 181 | float StringTokenizer::nextTokenAsFloat() {
181      if (iss >> result) {
182          return result;
183      } else {
184 <        std::err << "unable to convert " << token << "to a float" << std::endl;
184 >        std::cerr << "unable to convert " << token << " to a float" << std::endl;
185          return 0.0;
186      }
187   }
# Line 159 | Line 194 | double StringTokenizer::nextTokenAsDouble() {
194      if (iss >> result) {
195          return result;
196      } else {
197 <        std::err << "unable to convert " << token << "to a double" << std::endl;
197 >        std::cerr << "unable to convert " << token << " to a double" << std::endl;
198          return 0.0;
199      }
200   }
201  
202   std::string  StringTokenizer::peekNextToken() {
203 <    string result;
203 >    std::string result;
204      std::string::const_iterator tmpIter = currentPos_;
205      
206      if(tmpIter != end_) {
207 <        std::insert_iterator<string> insertIter(result, result.begin());
207 >        std::insert_iterator<std::string> insertIter(result, result.begin());
208  
209          while(tmpIter != end_ && isDelimiter(*tmpIter)) {
210  
# Line 189 | Line 224 | std::string  StringTokenizer::peekNextToken() {
224      return result;    
225   }
226  
227 < }
227 > }//end namespace oopse
228  
194 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines