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