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. |
23 |
* |
24 |
*/ |
25 |
|
26 |
#include <iostream> |
27 |
#include <sstream> |
28 |
#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 |
: tokenString_(first, last) , delim_(delim), returnTokens_(false), |
42 |
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 |
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::const_iterator tmpIter = currentPos_; |
60 |
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 |
|
74 |
if (tmpIter == end_) { |
75 |
break; |
76 |
} |
77 |
|
78 |
//encount a token here |
79 |
while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) { |
80 |
++tmpIter; |
81 |
} |
82 |
|
83 |
++numToken; |
84 |
|
85 |
} |
86 |
|
87 |
return numToken; |
88 |
} |
89 |
|
90 |
bool StringTokenizer::hasMoreTokens() { |
91 |
|
92 |
if (currentPos_ == end_) { |
93 |
return false; |
94 |
} else if (returnTokens_) { |
95 |
return true; |
96 |
} else { |
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)) { |
101 |
++i; |
102 |
} |
103 |
|
104 |
return i != end_ ? true : false; |
105 |
} |
106 |
} |
107 |
|
108 |
std::string StringTokenizer::nextToken() { |
109 |
std::string result; |
110 |
|
111 |
if(currentPos_ != end_) { |
112 |
std::insert_iterator<std::string> insertIter(result, result.begin()); |
113 |
|
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 |
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); |
149 |
int result; |
150 |
|
151 |
if (iss >> result) { |
152 |
return result; |
153 |
} else { |
154 |
std::cerr << "unable to convert " << token << "to an integer" << std::endl; |
155 |
return 0; |
156 |
} |
157 |
} |
158 |
|
159 |
float StringTokenizer::nextTokenAsFloat() { |
160 |
std::string token = nextToken(); |
161 |
std::istringstream iss(token); |
162 |
float result; |
163 |
|
164 |
if (iss >> result) { |
165 |
return result; |
166 |
} else { |
167 |
std::cerr << "unable to convert " << token << "to a float" << std::endl; |
168 |
return 0.0; |
169 |
} |
170 |
} |
171 |
|
172 |
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 |
std::cerr << "unable to convert " << token << "to a double" << std::endl; |
181 |
return 0.0; |
182 |
} |
183 |
} |
184 |
|
185 |
std::string StringTokenizer::peekNextToken() { |
186 |
std::string result; |
187 |
std::string::const_iterator tmpIter = currentPos_; |
188 |
|
189 |
if(tmpIter != end_) { |
190 |
std::insert_iterator<std::string> insertIter(result, result.begin()); |
191 |
|
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 |
}//end namespace oopse |
211 |
|