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 |
|
|
/** |
27 |
|
|
* @file StringTokenizer.hpp |
28 |
|
|
* @author tlin |
29 |
|
|
* @date 09/20/2004 |
30 |
|
|
* @time 11:30am |
31 |
|
|
* @version 1.0 |
32 |
|
|
*/ |
33 |
|
|
|
34 |
|
|
#ifndef UTIL_STRINGTOKENIZER_HPP |
35 |
|
|
#define UTIL_STRINGTOKENIZER_HPP |
36 |
|
|
|
37 |
|
|
#include <string> |
38 |
|
|
|
39 |
|
|
namespace oopse { |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* @class StringTokenizer.hpp "util/StringTokenizer.hpp" |
43 |
|
|
* @brief The string tokenizer class allows an application to break a string into tokens |
44 |
|
|
* The set of delimiters (the characters that separate tokens) may be specified either |
45 |
|
|
* at creation time or on a per-token basis. |
46 |
|
|
* An instance of StringTokenizer behaves in one of two ways, depending on whether it was |
47 |
|
|
* created with the returnTokens flag having the value true or false. |
48 |
|
|
*/ |
49 |
|
|
class StringTokenizer { |
50 |
|
|
public: |
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* Constructs a string tokenizer for the specified string. The characters in the delim argument |
54 |
|
|
* are the delimiters for separating tokens. characters are skipped and only serve as |
55 |
|
|
* separators between tokens. |
56 |
|
|
* @param str a string to be parsed. |
57 |
tim |
1864 |
* @param delim the delimiters, default value is " ;\t\n\r". |
58 |
tim |
1737 |
* @note this is still a little bit java like implementation. Pure c++ one should use TokenIterator. |
59 |
|
|
* Boost's tokenizer class is one of them |
60 |
tim |
1736 |
*/ |
61 |
|
|
StringTokenizer(const std::string & str, |
62 |
tim |
1808 |
const std::string & delim = " ;\t\n\r"); |
63 |
tim |
1736 |
|
64 |
tim |
1739 |
/** |
65 |
|
|
* Constructs a string tokenizer for an iterator range [first, last). The characters in the delim argument |
66 |
|
|
* are the delimiters for separating tokens. characters are skipped and only serve as |
67 |
|
|
* separators between tokens. |
68 |
|
|
* @param first begin iterator |
69 |
|
|
* @param last end iterator |
70 |
tim |
1864 |
* @param delim the delimiters, default value is " ;\t\n\r". |
71 |
tim |
1739 |
* @note this is still a little bit java like implementation. Pure c++ one should use TokenIterator. |
72 |
|
|
* Boost's tokenizer class is one of them |
73 |
|
|
*/ |
74 |
tim |
1736 |
StringTokenizer(std::string::const_iterator& first, std::string::const_iterator& last, |
75 |
tim |
1808 |
const std::string & delim = " ;\t\n\r"); |
76 |
tim |
1736 |
|
77 |
|
|
/** |
78 |
|
|
* Constructs a string tokenizer for the specified string. The characters in the delim argument |
79 |
|
|
* are the delimiters for separating tokens. |
80 |
|
|
* If the returnTokens flag is true, then the delimiter characters are also returned as tokens. |
81 |
|
|
* Each delimiter is returned as a string of length one. If the flag is false, the delimiter |
82 |
|
|
* characters are skipped and only serve as separators between tokens. |
83 |
|
|
* @param str a string to be parsed. |
84 |
|
|
* @param delim the delimiters. |
85 |
|
|
* @param returnTokens flag indicating whether to return the delimiters as tokens. |
86 |
|
|
*/ |
87 |
|
|
StringTokenizer(const std::string&str, const std::string&delim, |
88 |
|
|
bool returnTokens); |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Calculates the number of times that this tokenizer's nextToken method can be called |
92 |
|
|
* before it generates an exception. |
93 |
|
|
* @return the number of tokens remaining in the string using the current delimiter set. |
94 |
|
|
*/ |
95 |
|
|
int countTokens(); |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Tests if there are more tokens available from this tokenizer's string. |
99 |
|
|
* @return true if there are more tokens available from this tokenizer's string, false otherwise |
100 |
|
|
*/ |
101 |
|
|
bool hasMoreTokens(); |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Returns the next token from this string tokenizer. |
105 |
|
|
* @return the next token from this string tokenizer. |
106 |
|
|
* @exception NoSuchElementException if there are no more tokens in this tokenizer's string |
107 |
|
|
*/ |
108 |
|
|
std::string nextToken(); |
109 |
|
|
|
110 |
tim |
1780 |
//actually, nextToken Can be template function |
111 |
|
|
//template <typename ReturnType> |
112 |
|
|
//ReturnType nextToken(); |
113 |
|
|
|
114 |
tim |
1736 |
/** |
115 |
tim |
1780 |
* Returns the next token from this string tokenizer as a bool. |
116 |
|
|
* @return the next token from this string tokenizer as a bool. |
117 |
|
|
*/ |
118 |
|
|
bool nextTokenAsBool(); |
119 |
|
|
|
120 |
|
|
/** |
121 |
tim |
1736 |
* Returns the next token from this string tokenizer as an integer. |
122 |
|
|
* @return the next token from this string tokenizer as an integer. |
123 |
|
|
*/ |
124 |
|
|
int nextTokenAsInt(); |
125 |
|
|
|
126 |
|
|
/** |
127 |
|
|
* Returns the next token from this string tokenizer as a float. |
128 |
|
|
* @return the next token from this string tokenizer as a float. |
129 |
|
|
*/ |
130 |
|
|
float nextTokenAsFloat(); |
131 |
|
|
|
132 |
|
|
/** |
133 |
tim |
1746 |
* Returns the next token from this string tokenizer as a double. |
134 |
|
|
* @return the next token from this string tokenizer as a double. |
135 |
|
|
*/ |
136 |
|
|
double nextTokenAsDouble(); |
137 |
|
|
|
138 |
|
|
/** |
139 |
tim |
1736 |
* Returns the next token without advancing the position of the StringTokenizer. |
140 |
|
|
* @return the next token |
141 |
|
|
*/ |
142 |
|
|
std::string peekNextToken(); |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* Returns the current delimiter set of this string tokenizer |
146 |
|
|
* @return the current delimiter set |
147 |
|
|
*/ |
148 |
|
|
const std::string& getDelimiters() { |
149 |
|
|
return delim_; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
/** |
153 |
|
|
* Returns the original string before tokenizing. |
154 |
|
|
* @return the original string before tokenizing |
155 |
|
|
*/ |
156 |
|
|
const std::string& getOriginal() { |
157 |
|
|
return tokenString_; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
private: |
161 |
|
|
|
162 |
|
|
/** |
163 |
|
|
* Test if character is in current delimiter set. |
164 |
|
|
* @param c character to be tested |
165 |
|
|
* @return true if character is in current delimiter set, flase otherwise. |
166 |
|
|
*/ |
167 |
tim |
1780 |
bool isDelimiter(const char c); |
168 |
tim |
1736 |
|
169 |
|
|
std::string tokenString_; |
170 |
|
|
|
171 |
|
|
std::string delim_; /**< current delimiter set of this string tokenizer */ |
172 |
|
|
|
173 |
|
|
bool returnTokens_; /**< flag indicating whether to return the delimiters as tokens */ |
174 |
|
|
|
175 |
|
|
std::string::const_iterator currentPos_; |
176 |
|
|
std::string::const_iterator end_; |
177 |
|
|
}; |
178 |
|
|
|
179 |
|
|
} //namespace oopse |
180 |
|
|
|
181 |
|
|
#endif //UTIL_STRINGTOKENIZER_HPP |