| 1 | tim | 1736 | /* | 
| 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. | 
| 23 |  |  | * | 
| 24 |  |  | */ | 
| 25 |  |  |  | 
| 26 | tim | 1780 | #include <iostream> | 
| 27 |  |  | #include <sstream> | 
| 28 | tim | 1736 | #include "utils/StringTokenizer.hpp" | 
| 29 |  |  |  | 
| 30 |  |  | namespace oopse { | 
| 31 |  |  |  | 
| 32 |  |  |  | 
| 33 |  |  | StringTokenizer::StringTokenizer(const std::string & str, const std::string & delim) | 
| 34 |  |  | : tokenString_(str), delim_(delim), returnTokens_(false), | 
| 35 |  |  | currentPos_(tokenString_.begin()), end_(tokenString_.end()){ | 
| 36 |  |  |  | 
| 37 |  |  | } | 
| 38 |  |  |  | 
| 39 |  |  | StringTokenizer::StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last, | 
| 40 |  |  | const std::string & delim) | 
| 41 | tim | 1780 | : tokenString_(first, last) , delim_(delim), returnTokens_(false), | 
| 42 | tim | 1736 | currentPos_(tokenString_.begin()), end_(tokenString_.end()) { | 
| 43 |  |  |  | 
| 44 |  |  | } | 
| 45 |  |  |  | 
| 46 |  |  | StringTokenizer::StringTokenizer(const std::string&str, const std::string&delim, | 
| 47 |  |  | bool returnTokens) | 
| 48 |  |  | : tokenString_(str), delim_(delim), returnTokens_(returnTokens), | 
| 49 |  |  | currentPos_(tokenString_.begin()), end_(tokenString_.end()) { | 
| 50 |  |  |  | 
| 51 |  |  | } | 
| 52 |  |  |  | 
| 53 | tim | 1780 | bool StringTokenizer::isDelimiter(const char c) { | 
| 54 |  |  | return delim_.find(c) == std::string::npos ? false : true; | 
| 55 |  |  | } | 
| 56 |  |  |  | 
| 57 | tim | 1736 | int StringTokenizer::countTokens() { | 
| 58 |  |  |  | 
| 59 | tim | 1780 | std::string::const_iterator tmpIter = currentPos_; | 
| 60 | tim | 1736 | int numToken = 0; | 
| 61 |  |  |  | 
| 62 |  |  | while (true) { | 
| 63 |  |  |  | 
| 64 |  |  | //skip delimiter first | 
| 65 |  |  | while( tmpIter != end_ && isDelimiter(*tmpIter)) { | 
| 66 |  |  | ++tmpIter; | 
| 67 |  |  |  | 
| 68 |  |  | if (returnTokens_) { | 
| 69 |  |  | //if delimiter is consider as token | 
| 70 |  |  | ++numToken; | 
| 71 |  |  | } | 
| 72 |  |  | } | 
| 73 | tim | 1840 |  | 
| 74 |  |  | if (tmpIter == end_) { | 
| 75 |  |  | break; | 
| 76 |  |  | } | 
| 77 |  |  |  | 
| 78 | tim | 1736 | //encount a token here | 
| 79 |  |  | while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) { | 
| 80 |  |  | ++tmpIter; | 
| 81 |  |  | } | 
| 82 |  |  |  | 
| 83 | tim | 1840 | ++numToken; | 
| 84 | tim | 1736 |  | 
| 85 |  |  | } | 
| 86 |  |  |  | 
| 87 |  |  | return numToken; | 
| 88 |  |  | } | 
| 89 |  |  |  | 
| 90 |  |  | bool StringTokenizer::hasMoreTokens() { | 
| 91 | tim | 1737 |  | 
| 92 |  |  | if (currentPos_ == end_) { | 
| 93 |  |  | return false; | 
| 94 |  |  | } else if (returnTokens_) { | 
| 95 |  |  | return true; | 
| 96 |  |  | } else { | 
| 97 | tim | 1780 | std::string::const_iterator i = currentPos_; | 
| 98 | tim | 1736 |  | 
| 99 | tim | 1737 | //walk through the remaining string to check whether it contains non-delimeter or not | 
| 100 |  |  | while(i != end_ && isDelimiter(*i)) { | 
| 101 |  |  | ++i; | 
| 102 |  |  | } | 
| 103 |  |  |  | 
| 104 |  |  | return i != end_ ? true : false; | 
| 105 |  |  | } | 
| 106 | tim | 1736 | } | 
| 107 |  |  |  | 
| 108 |  |  | std::string StringTokenizer::nextToken() { | 
| 109 | tim | 1741 | std::string result; | 
| 110 | tim | 1736 |  | 
| 111 |  |  | if(currentPos_ != end_) { | 
| 112 | tim | 1780 | std::insert_iterator<std::string> insertIter(result, result.begin()); | 
| 113 | tim | 1736 |  | 
| 114 |  |  | while( currentPos_ != end_ && isDelimiter(*currentPos_)) { | 
| 115 |  |  |  | 
| 116 |  |  | if (returnTokens_) { | 
| 117 |  |  | *insertIter++ = *currentPos_++; | 
| 118 |  |  | return result; | 
| 119 |  |  | } | 
| 120 |  |  |  | 
| 121 |  |  | ++currentPos_; | 
| 122 |  |  | } | 
| 123 |  |  |  | 
| 124 |  |  | while (currentPos_ != end_ && !isDelimiter(*currentPos_)) { | 
| 125 |  |  | *insertIter++ = *currentPos_++; | 
| 126 |  |  | } | 
| 127 |  |  |  | 
| 128 |  |  | } | 
| 129 |  |  |  | 
| 130 |  |  | return result; | 
| 131 |  |  | } | 
| 132 |  |  |  | 
| 133 | tim | 1758 | 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 | tim | 1780 | std::cerr << "unable to convert " << token << "to a bool" << std::endl; | 
| 142 | tim | 1758 | return false; | 
| 143 |  |  | } | 
| 144 |  |  | } | 
| 145 |  |  |  | 
| 146 | tim | 1736 | int StringTokenizer::nextTokenAsInt() { | 
| 147 | tim | 1741 | std::string token = nextToken(); | 
| 148 |  |  | std::istringstream iss(token); | 
| 149 |  |  | int result; | 
| 150 |  |  |  | 
| 151 |  |  | if (iss >> result) { | 
| 152 |  |  | return result; | 
| 153 |  |  | } else { | 
| 154 | tim | 1780 | std::cerr << "unable to convert " << token << "to an integer" << std::endl; | 
| 155 | tim | 1741 | return 0; | 
| 156 |  |  | } | 
| 157 | tim | 1736 | } | 
| 158 |  |  |  | 
| 159 |  |  | float StringTokenizer::nextTokenAsFloat() { | 
| 160 | tim | 1741 | std::string token = nextToken(); | 
| 161 |  |  | std::istringstream iss(token); | 
| 162 |  |  | float result; | 
| 163 |  |  |  | 
| 164 |  |  | if (iss >> result) { | 
| 165 |  |  | return result; | 
| 166 |  |  | } else { | 
| 167 | tim | 1780 | std::cerr << "unable to convert " << token << "to a float" << std::endl; | 
| 168 | tim | 1741 | return 0.0; | 
| 169 |  |  | } | 
| 170 | tim | 1736 | } | 
| 171 |  |  |  | 
| 172 | tim | 1741 | double StringTokenizer::nextTokenAsDouble() { | 
| 173 |  |  | std::string token = nextToken(); | 
| 174 |  |  | std::istringstream iss(token); | 
| 175 |  |  | double result; | 
| 176 |  |  |  | 
| 177 |  |  | if (iss >> result) { | 
| 178 |  |  | return result; | 
| 179 |  |  | } else { | 
| 180 | tim | 1780 | std::cerr << "unable to convert " << token << "to a double" << std::endl; | 
| 181 | tim | 1741 | return 0.0; | 
| 182 |  |  | } | 
| 183 |  |  | } | 
| 184 |  |  |  | 
| 185 | tim | 1736 | std::string  StringTokenizer::peekNextToken() { | 
| 186 | tim | 1780 | std::string result; | 
| 187 | tim | 1736 | std::string::const_iterator tmpIter = currentPos_; | 
| 188 |  |  |  | 
| 189 |  |  | if(tmpIter != end_) { | 
| 190 | tim | 1780 | std::insert_iterator<std::string> insertIter(result, result.begin()); | 
| 191 | tim | 1736 |  | 
| 192 |  |  | while(tmpIter != end_ && isDelimiter(*tmpIter)) { | 
| 193 |  |  |  | 
| 194 |  |  | if (returnTokens_) { | 
| 195 |  |  | *insertIter++ = *tmpIter++; | 
| 196 |  |  | return result; | 
| 197 |  |  | } | 
| 198 |  |  |  | 
| 199 |  |  | ++tmpIter; | 
| 200 |  |  | } | 
| 201 |  |  |  | 
| 202 |  |  | while (tmpIter != end_ && !isDelimiter(*tmpIter)) { | 
| 203 |  |  | *insertIter++ = *tmpIter++; | 
| 204 |  |  | } | 
| 205 |  |  | } | 
| 206 |  |  |  | 
| 207 |  |  | return result; | 
| 208 |  |  | } | 
| 209 |  |  |  | 
| 210 | tim | 1780 | }//end namespace oopse | 
| 211 | tim | 1736 |  |