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