OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48/**
49 * @file StringTokenizer.hpp
50 * @author tlin
51 * @date 09/20/2004
52 * @version 1.0
53 */
54
55#ifndef UTIL_STRINGTOKENIZER_HPP
56#define UTIL_STRINGTOKENIZER_HPP
57
58#include <config.h>
59
60#include <cstdlib>
61#include <string>
62#include <vector>
63
64namespace OpenMD {
65
66 /**
67 * @class StringTokenizer
68 * @brief The string tokenizer class allows an application to break a string
69 * into tokens The set of delimiters (the characters that separate tokens) may
70 * be specified either at creation time or on a per-token basis. An instance
71 * of StringTokenizer behaves in one of two ways, depending on whether it was
72 * created with the returnTokens flag having the value true or false.
73 */
75 public:
76 /**
77 * Constructs a string tokenizer for the specified string. The characters in
78 * the delim argument are the delimiters for separating tokens. characters
79 * are skipped and only serve as separators between tokens.
80 * @param str a string to be parsed.
81 * @param delim the delimiters, default value is " ;\t\n\r".
82 * @note this is still a little bit java like implementation. Pure c++ one
83 * should use TokenIterator. Boost's tokenizer class is one of them
84 */
85 StringTokenizer(const std::string& str,
86 const std::string& delim = " ;\t\n\r");
87
88 /**
89 * Constructs a string tokenizer for an iterator range [first, last). The
90 * characters in the delim argument are the delimiters for separating
91 * tokens. characters are skipped and only serve as separators between
92 * tokens.
93 * @param first begin iterator
94 * @param last end iterator
95 * @param delim the delimiters, default value is " ;\t\n\r".
96 * @note this is still a little bit java like implementation. Pure c++ one
97 * should use TokenIterator. Boost's tokenizer class is one of them
98 */
99 StringTokenizer(std::string::const_iterator& first,
100 std::string::const_iterator& last,
101 const std::string& delim = " ;\t\n\r");
102
103 /**
104 * Constructs a string tokenizer for the specified string. The characters in
105 * the delim argument are the delimiters for separating tokens. If the
106 * returnTokens flag is true, then the delimiter characters are also
107 * returned as tokens. Each delimiter is returned as a string of length one.
108 * If the flag is false, the delimiter characters are skipped and only serve
109 * as separators between tokens.
110 * @param str a string to be parsed.
111 * @param delim the delimiters.
112 * @param returnTokens flag indicating whether to return the delimiters as
113 * tokens.
114 */
115 StringTokenizer(const std::string& str, const std::string& delim,
116 bool returnTokens);
117
118 /**
119 * Calculates the number of times that this tokenizer's nextToken method can
120 * be called before it generates an exception.
121 * @return the number of tokens remaining in the string using the current
122 * delimiter set.
123 */
124 int countTokens();
125
126 /**
127 * Tests if there are more tokens available from this tokenizer's string.
128 * @return true if there are more tokens available from this tokenizer's
129 * string, false otherwise
130 */
131 bool hasMoreTokens();
132
133 /**
134 * Returns the next token from this string tokenizer.
135 * @return the next token from this string tokenizer.
136 * @exception NoSuchElementException if there are no more tokens in this
137 * tokenizer's string
138 */
139 std::string nextToken();
140
141 /**
142 * Skips the next token from this string tokenizer.
143 * @exception NoSuchElementException if there are no more tokens in this
144 * tokenizer's string
145 */
146 void skipToken();
147
148 // actually, nextToken Can be template function
149 // template <typename ReturnType>
150 // ReturnType nextToken();
151
152 /**
153 * Returns the next token from this string tokenizer as a bool.
154 * @return the next token from this string tokenizer as a bool.
155 */
156 bool nextTokenAsBool();
157
158 /**
159 * Returns the next token from this string tokenizer as an integer.
160 * @return the next token from this string tokenizer as an integer.
161 */
162 int nextTokenAsInt();
163
164 /**
165 * Returns the next token from this string tokenizer as a float.
166 * @return the next token from this string tokenizer as a float.
167 */
168 float nextTokenAsFloat();
169
170 /**
171 * Returns the next token from this string tokenizer as a RealType.
172 * @return the next token from this string tokenizer as a RealType.
173 */
174 RealType nextTokenAsDouble();
175
176 /**
177 * Returns the next token without advancing the position of the
178 * StringTokenizer.
179 * @return the next token
180 */
181 std::string peekNextToken();
182
183 /**
184 * Returns the current delimiter set of this string tokenizer
185 * @return the current delimiter set
186 */
187 const std::string& getDelimiters() { return delim_; }
188
189 /**
190 * Returns the original string before tokenizing.
191 * @return the original string before tokenizing
192 */
193 const std::string& getOriginal() { return tokenString_; }
194
195 /**
196 * Returns all of the tokens
197 * @return all of the tokens
198 */
199 std::vector<std::string> getAllTokens();
200
201 /**
202 * Returns the remaining unparsed string
203 * @return the remaining unparsed string
204 */
205 std::string getRemainingString() const;
206
207 private:
208 /**
209 * Test if character is in current delimiter set.
210 * @param c character to be tested
211 * @return true if character is in current delimiter set, flase otherwise.
212 */
213 bool isDelimiter(const char c);
214
215 /** convert a fortran number to a c/c++ number */
216 void convertFortranNumber(std::string& fortranNumber);
217
218 std::string tokenString_;
219
220 std::string delim_; /**< current delimiter set of this string tokenizer */
221
222 bool returnTokens_; /**< flag indicating whether to return the delimiters as
223 tokens */
224
225 std::string::const_iterator currentPos_;
226 std::string::const_iterator end_;
227 };
228} // namespace OpenMD
229
230#endif // UTIL_STRINGTOKENIZER_HPP
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.
std::string getRemainingString() const
Returns the remaining unparsed string.
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.
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.