ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/utils/StringTokenizer.cpp
Revision: 1737
Committed: Fri Nov 12 22:44:03 2004 UTC (19 years, 8 months ago) by tim
File size: 4668 byte(s)
Log Message:
bug fix in hasMoreTokens

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 "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 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 return atoi(nextToken().c_str());
130 }
131
132 float StringTokenizer::nextTokenAsFloat() {
133 return atof(nextToken().c_str());
134 }
135
136 std::string StringTokenizer::peekNextToken() {
137 string result;
138 std::string::const_iterator tmpIter = currentPos_;
139
140 if(tmpIter != end_) {
141 std::insert_iterator<string> insertIter(result, result.begin());
142
143 while(tmpIter != end_ && isDelimiter(*tmpIter)) {
144
145 if (returnTokens_) {
146 *insertIter++ = *tmpIter++;
147 return result;
148 }
149
150 ++tmpIter;
151 }
152
153 while (tmpIter != end_ && !isDelimiter(*tmpIter)) {
154 *insertIter++ = *tmpIter++;
155 }
156 }
157
158 return result;
159 }
160
161 }
162
163 }