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