ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/utils/StringTokenizer.cpp
Revision: 1736
Committed: Fri Nov 12 22:28:35 2004 UTC (19 years, 9 months ago) by tim
File size: 4328 byte(s)
Log Message:
adding StringUtils, a c-style strtok like class, need unit test

File Contents

# User Rev Content
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     #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     return currentPos_ != end_ ? true : false;
88     }
89    
90     std::string StringTokenizer::nextToken() {
91     string result;
92    
93     if(currentPos_ != end_) {
94     std::insert_iterator<string> insertIter(result, result.begin());
95    
96     while( currentPos_ != end_ && isDelimiter(*currentPos_)) {
97    
98     if (returnTokens_) {
99     *insertIter++ = *currentPos_++;
100     return result;
101     }
102    
103     ++currentPos_;
104     }
105    
106     while (currentPos_ != end_ && !isDelimiter(*currentPos_)) {
107     *insertIter++ = *currentPos_++;
108     }
109    
110     }
111    
112     return result;
113     }
114    
115     int StringTokenizer::nextTokenAsInt() {
116     return atoi(nextToken().c_str());
117     }
118    
119     float StringTokenizer::nextTokenAsFloat() {
120     return atof(nextToken().c_str());
121     }
122    
123     std::string StringTokenizer::peekNextToken() {
124     string result;
125     std::string::const_iterator tmpIter = currentPos_;
126    
127     if(tmpIter != end_) {
128     std::insert_iterator<string> insertIter(result, result.begin());
129    
130     while(tmpIter != end_ && isDelimiter(*tmpIter)) {
131    
132     if (returnTokens_) {
133     *insertIter++ = *tmpIter++;
134     return result;
135     }
136    
137     ++tmpIter;
138     }
139    
140     while (tmpIter != end_ && !isDelimiter(*tmpIter)) {
141     *insertIter++ = *tmpIter++;
142     }
143     }
144    
145     return result;
146     }
147    
148     }
149    
150     }