| 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), |
| 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 |
|
} |
| 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) { |
| 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 |
|
|
| 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)) { |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
std::string StringTokenizer::nextToken() { |
| 110 |
< |
string result; |
| 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 |
|
|
| 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 |
< |
return atoi(nextToken().c_str()); |
| 148 |
> |
std::string token = nextToken(); |
| 149 |
> |
std::istringstream iss(token); |
| 150 |
> |
int result; |
| 151 |
> |
|
| 152 |
> |
if (iss >> result) { |
| 153 |
> |
return result; |
| 154 |
> |
} else { |
| 155 |
> |
std::cerr << "unable to convert " << token << " to an integer" << std::endl; |
| 156 |
> |
return 0; |
| 157 |
> |
} |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
float StringTokenizer::nextTokenAsFloat() { |
| 161 |
< |
return atof(nextToken().c_str()); |
| 161 |
> |
std::string token = nextToken(); |
| 162 |
> |
std::istringstream iss(token); |
| 163 |
> |
float result; |
| 164 |
> |
|
| 165 |
> |
if (iss >> result) { |
| 166 |
> |
return result; |
| 167 |
> |
} else { |
| 168 |
> |
std::cerr << "unable to convert " << token << " to a float" << std::endl; |
| 169 |
> |
return 0.0; |
| 170 |
> |
} |
| 171 |
|
} |
| 172 |
|
|
| 173 |
+ |
double StringTokenizer::nextTokenAsDouble() { |
| 174 |
+ |
std::string token = nextToken(); |
| 175 |
+ |
std::istringstream iss(token); |
| 176 |
+ |
double result; |
| 177 |
+ |
|
| 178 |
+ |
if (iss >> result) { |
| 179 |
+ |
return result; |
| 180 |
+ |
} else { |
| 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 |
|
|
| 208 |
|
return result; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
< |
} |
| 211 |
> |
}//end namespace oopse |
| 212 |
|
|
| 163 |
– |
} |