OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
StringTokenizer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45/**
46 * @file StringTokenizer.hpp
47 * @author tlin
48 * @date 09/20/2004
49 * @version 1.0
50 */
51
52#ifndef UTIL_STRINGTOKENIZER_HPP
53#define UTIL_STRINGTOKENIZER_HPP
54
55#include <config.h>
56
57#include <cstdlib>
58#include <string>
59#include <vector>
60
61namespace OpenMD {
62
63 /**
64 * @class StringTokenizer
65 * @brief The string tokenizer class allows an application to break a string
66 * into tokens The set of delimiters (the characters that separate tokens) may
67 * be specified either at creation time or on a per-token basis. An instance
68 * of StringTokenizer behaves in one of two ways, depending on whether it was
69 * created with the returnTokens flag having the value true or false.
70 */
72 public:
73 /**
74 * Constructs a string tokenizer for the specified string. The characters in
75 * the delim argument are the delimiters for separating tokens. characters
76 * are skipped and only serve as separators between tokens.
77 * @param str a string to be parsed.
78 * @param delim the delimiters, default value is " ;\t\n\r".
79 * @note this is still a little bit java like implementation. Pure c++ one
80 * should use TokenIterator. Boost's tokenizer class is one of them
81 */
82 StringTokenizer(const std::string& str,
83 const std::string& delim = " ;\t\n\r");
84
85 /**
86 * Constructs a string tokenizer for an iterator range [first, last). The
87 * characters in the delim argument are the delimiters for separating
88 * tokens. characters are skipped and only serve as separators between
89 * tokens.
90 * @param first begin iterator
91 * @param last end iterator
92 * @param delim the delimiters, default value is " ;\t\n\r".
93 * @note this is still a little bit java like implementation. Pure c++ one
94 * should use TokenIterator. Boost's tokenizer class is one of them
95 */
96 StringTokenizer(std::string::const_iterator& first,
97 std::string::const_iterator& last,
98 const std::string& delim = " ;\t\n\r");
99
100 /**
101 * Constructs a string tokenizer for the specified string. The characters in
102 * the delim argument are the delimiters for separating tokens. If the
103 * returnTokens flag is true, then the delimiter characters are also
104 * returned as tokens. Each delimiter is returned as a string of length one.
105 * If the flag is false, the delimiter characters are skipped and only serve
106 * as separators between tokens.
107 * @param str a string to be parsed.
108 * @param delim the delimiters.
109 * @param returnTokens flag indicating whether to return the delimiters as
110 * tokens.
111 */
112 StringTokenizer(const std::string& str, const std::string& delim,
113 bool returnTokens);
114
115 /**
116 * Calculates the number of times that this tokenizer's nextToken method can
117 * be called before it generates an exception.
118 * @return the number of tokens remaining in the string using the current
119 * delimiter set.
120 */
121 int countTokens();
122
123 /**
124 * Tests if there are more tokens available from this tokenizer's string.
125 * @return true if there are more tokens available from this tokenizer's
126 * string, false otherwise
127 */
128 bool hasMoreTokens();
129
130 /**
131 * Returns the next token from this string tokenizer.
132 * @return the next token from this string tokenizer.
133 * @exception NoSuchElementException if there are no more tokens in this
134 * tokenizer's string
135 */
136 std::string nextToken();
137
138 /**
139 * Skips the next token from this string tokenizer.
140 * @exception NoSuchElementException if there are no more tokens in this
141 * tokenizer's string
142 */
143 void skipToken();
144
145 // actually, nextToken Can be template function
146 // template <typename ReturnType>
147 // ReturnType nextToken();
148
149 /**
150 * Returns the next token from this string tokenizer as a bool.
151 * @return the next token from this string tokenizer as a bool.
152 */
153 bool nextTokenAsBool();
154
155 /**
156 * Returns the next token from this string tokenizer as an integer.
157 * @return the next token from this string tokenizer as an integer.
158 */
159 int nextTokenAsInt();
160
161 /**
162 * Returns the next token from this string tokenizer as a float.
163 * @return the next token from this string tokenizer as a float.
164 */
165 float nextTokenAsFloat();
166
167 /**
168 * Returns the next token from this string tokenizer as a RealType.
169 * @return the next token from this string tokenizer as a RealType.
170 */
171 RealType nextTokenAsDouble();
172
173 /**
174 * Returns the next token without advancing the position of the
175 * StringTokenizer.
176 * @return the next token
177 */
178 std::string peekNextToken();
179
180 /**
181 * Returns the current delimiter set of this string tokenizer
182 * @return the current delimiter set
183 */
184 const std::string& getDelimiters() { return delim_; }
185
186 /**
187 * Returns the original string before tokenizing.
188 * @return the original string before tokenizing
189 */
190 const std::string& getOriginal() { return tokenString_; }
191
192 /**
193 * Returns all of the tokens
194 * @return all of the tokens
195 */
196 std::vector<std::string> getAllTokens();
197
198 /**
199 * Returns the remaining unparsed string
200 * @return the remaining unparsed string
201 */
202 std::string getRemainingString();
203
204 private:
205 /**
206 * Test if character is in current delimiter set.
207 * @param c character to be tested
208 * @return true if character is in current delimiter set, flase otherwise.
209 */
210 bool isDelimiter(const char c);
211
212 /** convert a fortran number to a c/c++ number */
213 void convertFortranNumber(std::string& fortranNumber);
214
215 std::string tokenString_;
216
217 std::string delim_; /**< current delimiter set of this string tokenizer */
218
219 bool returnTokens_; /**< flag indicating whether to return the delimiters as
220 tokens */
221
222 std::string::const_iterator currentPos_;
223 std::string::const_iterator end_;
224 };
225} // namespace OpenMD
226
227#endif // UTIL_STRINGTOKENIZER_HPP
The string tokenizer class allows an application to break a string into tokens The set of delimiters ...
std::vector< std::string > getAllTokens()
Returns all of the tokens.
std::string peekNextToken()
Returns the next token without advancing the position of the StringTokenizer.
const std::string & getDelimiters()
Returns the current delimiter set of this string tokenizer.
std::string nextToken()
Returns the next token from this string tokenizer.
void skipToken()
Skips the next token from this string tokenizer.
int countTokens()
Calculates the number of times that this tokenizer's nextToken method can be called before it generat...
int nextTokenAsInt()
Returns the next token from this string tokenizer as an integer.
float nextTokenAsFloat()
Returns the next token from this string tokenizer as a float.
bool hasMoreTokens()
Tests if there are more tokens available from this tokenizer's string.
const std::string & getOriginal()
Returns the original string before tokenizing.
StringTokenizer(const std::string &str, const std::string &delim=" ;\t\n\r")
Constructs a string tokenizer for the specified string.
std::string getRemainingString()
Returns the remaining unparsed string.
bool nextTokenAsBool()
Returns the next token from this string tokenizer as a bool.
RealType nextTokenAsDouble()
Returns the next token from this string tokenizer as a RealType.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.