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 1854 by tim, Sun Dec 5 22:02:42 2004 UTC

# Line 23 | Line 23
23   *
24   */
25  
26 + #include <iostream>
27 + #include <iterator>
28 + #include <sstream>
29   #include "utils/StringTokenizer.hpp"
30  
31   namespace oopse {
32  
30 StringTokenizer::defaultDelim = " \t\n\r";
33  
34   StringTokenizer::StringTokenizer(const std::string & str, const std::string & delim)
35                          : tokenString_(str), delim_(delim), returnTokens_(false),
# Line 37 | Line 39 | StringTokenizer::StringTokenizer(std::string::const_it
39  
40   StringTokenizer::StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last,
41                                                                 const std::string & delim)  
42 <                        : (tokenString_(first, last) , delim_(delim), returnTokens_(false),
42 >                        : tokenString_(first, last) , delim_(delim), returnTokens_(false),
43                             currentPos_(tokenString_.begin()), end_(tokenString_.end()) {
44  
45   }
# Line 49 | Line 51 | StringTokenizer::StringTokenizer(const std::string&str
51  
52   }
53  
54 + bool StringTokenizer::isDelimiter(const char c) {
55 +    return delim_.find(c) == std::string::npos ? false : true;
56 + }
57 +
58   int StringTokenizer::countTokens() {
59      
60 <    std::string::iterator tmpIter = currentPos_;    
60 >    std::string::const_iterator tmpIter = currentPos_;    
61      int numToken = 0;
62  
63      while (true) {
# Line 65 | Line 71 | int StringTokenizer::countTokens() {
71                  ++numToken;
72              }
73          }
74 <
74 >        
75 >        if (tmpIter == end_) {
76 >            break;
77 >        }
78 >        
79          //encount a token here
80          while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) {
81              ++tmpIter;
82          }
83  
84 <         if (tmpIter != end_) {
75 <            ++numToken;
76 <        } else {
77 <            break;
78 <        }
84 >        ++numToken;
85  
86      }
87  
# Line 89 | Line 95 | bool StringTokenizer::hasMoreTokens() {
95      } else if (returnTokens_) {
96          return true;
97      } else {
98 <        std::string::iterator i = currentPos_;
98 >        std::string::const_iterator i = currentPos_;
99  
100          //walk through the remaining string to check whether it contains non-delimeter or not
101          while(i != end_ && isDelimiter(*i)) {
# Line 104 | Line 110 | std::string StringTokenizer::nextToken() {
110      std::string result;
111      
112      if(currentPos_ != end_) {
113 <        std::insert_iterator<string> insertIter(result, result.begin());
113 >        std::insert_iterator<std::string> insertIter(result, result.begin());
114  
115          while( currentPos_ != end_ && isDelimiter(*currentPos_)) {
116  
# Line 125 | Line 131 | std::string StringTokenizer::nextToken() {
131      return result;
132   }
133  
134 + bool StringTokenizer::nextTokenAsBool() {
135 +    std::string token = nextToken();
136 +    std::istringstream iss(token);
137 +    bool result;
138 +    
139 +    if (iss >> result) {
140 +        return result;
141 +    } else {
142 +        std::cerr << "unable to convert " << token << "to a bool" << std::endl;
143 +        return false;
144 +    }
145 + }
146 +
147   int StringTokenizer::nextTokenAsInt() {
148      std::string token = nextToken();
149      std::istringstream iss(token);
# Line 133 | Line 152 | int StringTokenizer::nextTokenAsInt() {
152      if (iss >> result) {
153          return result;
154      } else {
155 <        std::err << "unable to convert " << token << "to an integer" << std::endl;
155 >        std::cerr << "unable to convert " << token << "to an integer" << std::endl;
156          return 0;
157      }
158   }
# Line 146 | Line 165 | float StringTokenizer::nextTokenAsFloat() {
165      if (iss >> result) {
166          return result;
167      } else {
168 <        std::err << "unable to convert " << token << "to a float" << std::endl;
168 >        std::cerr << "unable to convert " << token << "to a float" << std::endl;
169          return 0.0;
170      }
171   }
# Line 159 | Line 178 | double StringTokenizer::nextTokenAsDouble() {
178      if (iss >> result) {
179          return result;
180      } else {
181 <        std::err << "unable to convert " << token << "to a double" << std::endl;
181 >        std::cerr << "unable to convert " << token << "to a double" << std::endl;
182          return 0.0;
183      }
184   }
185  
186   std::string  StringTokenizer::peekNextToken() {
187 <    string result;
187 >    std::string result;
188      std::string::const_iterator tmpIter = currentPos_;
189      
190      if(tmpIter != end_) {
191 <        std::insert_iterator<string> insertIter(result, result.begin());
191 >        std::insert_iterator<std::string> insertIter(result, result.begin());
192  
193          while(tmpIter != end_ && isDelimiter(*tmpIter)) {
194  
# Line 189 | Line 208 | std::string  StringTokenizer::peekNextToken() {
208      return result;    
209   }
210  
211 < }
211 > }//end namespace oopse
212  
194 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines