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