OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
StringTokenizer.cpp
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
46
47#include <iostream>
48#include <iterator>
49#include <sstream>
50
51namespace OpenMD {
52
53 StringTokenizer::StringTokenizer(const std::string& str,
54 const std::string& delim) :
55 tokenString_(str),
56 delim_(delim), returnTokens_(false), currentPos_(tokenString_.begin()),
57 end_(tokenString_.end()) {}
58
59 StringTokenizer::StringTokenizer(std::string::const_iterator& first,
60 std::string::const_iterator& last,
61 const std::string& delim) :
62 tokenString_(first, last),
63 delim_(delim), returnTokens_(false), currentPos_(tokenString_.begin()),
64 end_(tokenString_.end()) {}
65
66 StringTokenizer::StringTokenizer(const std::string& str,
67 const std::string& delim,
68 bool returnTokens) :
69 tokenString_(str),
70 delim_(delim), returnTokens_(returnTokens),
71 currentPos_(tokenString_.begin()), end_(tokenString_.end()) {}
72
73 bool StringTokenizer::isDelimiter(const char c) {
74 return delim_.find(c) == std::string::npos ? false : true;
75 }
76
78 std::string::const_iterator tmpIter = currentPos_;
79 int numToken = 0;
80
81 while (true) {
82 // skip delimiter first
83 while (tmpIter != end_ && isDelimiter(*tmpIter)) {
84 ++tmpIter;
85
86 if (returnTokens_) {
87 // if delimiter is consider as token
88 ++numToken;
89 }
90 }
91
92 if (tmpIter == end_) { break; }
93
94 // encount a token here
95 while (tmpIter != end_ && !isDelimiter(*tmpIter)) {
96 ++tmpIter;
97 }
98
99 ++numToken;
100 }
101
102 return numToken;
103 }
104
106 if (currentPos_ == end_) {
107 return false;
108 } else if (returnTokens_) {
109 return true;
110 } else {
111 std::string::const_iterator i = currentPos_;
112
113 // walk through the remaining string to check whether it contains
114 // non-delimeter or not
115 while (i != end_ && isDelimiter(*i)) {
116 ++i;
117 }
118
119 return i != end_ ? true : false;
120 }
121 }
122
124 std::string result;
125
126 if (currentPos_ != end_) {
127 std::insert_iterator<std::string> insertIter(result, result.begin());
128
129 while (currentPos_ != end_ && isDelimiter(*currentPos_)) {
130 if (returnTokens_) {
131 *insertIter++ = *currentPos_++;
132 return result;
133 }
134
135 ++currentPos_;
136 }
137
138 while (currentPos_ != end_ && !isDelimiter(*currentPos_)) {
139 *insertIter++ = *currentPos_++;
140 }
141 }
142
143 return result;
144 }
145
147 if (currentPos_ != end_) {
148 while (currentPos_ != end_ && isDelimiter(*currentPos_)) {
149 if (returnTokens_) {
150 *currentPos_++;
151 return;
152 }
153
154 ++currentPos_;
155 }
156
157 while (currentPos_ != end_ && !isDelimiter(*currentPos_)) {
158 *currentPos_++;
159 }
160 }
161 }
162
164 std::string token = nextToken();
165 std::istringstream iss(token);
166 bool result;
167
168 if (iss >> result) {
169 return result;
170 } else {
171 std::cerr << "unable to convert " << token << " to a bool" << std::endl;
172 return false;
173 }
174 }
175
176 // Since libstdc++(GCC 3.2) has an i/ostream::operator>>/<<(streambuf*) bug
177 // (Bug 9318) Instead of using iostream facility, we use C library
179 std::string token = nextToken();
180
181 return atoi(token.c_str());
182 }
183
185 std::string token = nextToken();
186 convertFortranNumber(token);
187 return (float)(atof(token.c_str()));
188 }
189
191 std::string token = nextToken();
192 convertFortranNumber(token);
193 return atof(token.c_str());
194 }
195
197 std::string result;
198 std::string::const_iterator tmpIter = currentPos_;
199
200 if (tmpIter != end_) {
201 std::insert_iterator<std::string> insertIter(result, result.begin());
202
203 while (tmpIter != end_ && isDelimiter(*tmpIter)) {
204 if (returnTokens_) {
205 *insertIter++ = *tmpIter++;
206 return result;
207 }
208
209 ++tmpIter;
210 }
211
212 while (tmpIter != end_ && !isDelimiter(*tmpIter)) {
213 *insertIter++ = *tmpIter++;
214 }
215 }
216
217 return result;
218 }
219
220 std::vector<std::string> StringTokenizer::getAllTokens() {
221 std::vector<std::string> tokens;
222 while (hasMoreTokens()) {
223 tokens.push_back(nextToken());
224 }
225 return tokens;
226 }
227
228 void StringTokenizer::convertFortranNumber(std::string& fortranNumber) {
229 std::string::iterator i;
230 for (i = fortranNumber.begin(); i != fortranNumber.end(); ++i) {
231 if (*i == 'd' || *i == 'D') { *i = 'E'; }
232 }
233 }
234
236 std::string result;
237 std::string::const_iterator tmpIter = currentPos_;
238 if (tmpIter != end_) {
239 std::insert_iterator<std::string> insertIter(result, result.begin());
240
241 while (tmpIter != end_) {
242 *insertIter++ = *tmpIter++;
243 }
244 }
245
246 return result;
247 }
248} // namespace OpenMD
std::vector< std::string > getAllTokens()
Returns all of the tokens.
std::string peekNextToken()
Returns the next token without advancing the position of the StringTokenizer.
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.
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.