ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/utils/StringTokenizer.cpp
Revision: 1854
Committed: Sun Dec 5 22:02:42 2004 UTC (19 years, 7 months ago) by tim
File size: 5916 byte(s)
Log Message:
fix a bug in filling MolMembership

File Contents

# Content
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 <iterator>
28 #include <sstream>
29 #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 : tokenString_(first, last) , delim_(delim), returnTokens_(false),
43 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 bool StringTokenizer::isDelimiter(const char c) {
55 return delim_.find(c) == std::string::npos ? false : true;
56 }
57
58 int StringTokenizer::countTokens() {
59
60 std::string::const_iterator tmpIter = currentPos_;
61 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
75 if (tmpIter == end_) {
76 break;
77 }
78
79 //encount a token here
80 while ( tmpIter != end_ && !isDelimiter(*tmpIter) ) {
81 ++tmpIter;
82 }
83
84 ++numToken;
85
86 }
87
88 return numToken;
89 }
90
91 bool StringTokenizer::hasMoreTokens() {
92
93 if (currentPos_ == end_) {
94 return false;
95 } else if (returnTokens_) {
96 return true;
97 } else {
98 std::string::const_iterator i = currentPos_;
99
100 //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 }
108
109 std::string StringTokenizer::nextToken() {
110 std::string result;
111
112 if(currentPos_ != end_) {
113 std::insert_iterator<std::string> insertIter(result, result.begin());
114
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 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 std::cerr << "unable to convert " << token << "to a bool" << std::endl;
143 return false;
144 }
145 }
146
147 int StringTokenizer::nextTokenAsInt() {
148 std::string token = nextToken();
149 std::istringstream iss(token);
150 int result;
151
152 if (iss >> result) {
153 return result;
154 } else {
155 std::cerr << "unable to convert " << token << "to an integer" << std::endl;
156 return 0;
157 }
158 }
159
160 float StringTokenizer::nextTokenAsFloat() {
161 std::string token = nextToken();
162 std::istringstream iss(token);
163 float result;
164
165 if (iss >> result) {
166 return result;
167 } else {
168 std::cerr << "unable to convert " << token << "to a float" << std::endl;
169 return 0.0;
170 }
171 }
172
173 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 std::cerr << "unable to convert " << token << "to a double" << std::endl;
182 return 0.0;
183 }
184 }
185
186 std::string StringTokenizer::peekNextToken() {
187 std::string result;
188 std::string::const_iterator tmpIter = currentPos_;
189
190 if(tmpIter != end_) {
191 std::insert_iterator<std::string> insertIter(result, result.begin());
192
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 }//end namespace oopse
212