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