OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
StringUtils.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/StringUtils.hpp"
49
50#include <config.h>
51
52#include <algorithm>
53#include <cctype>
54#include <cstdlib>
55#include <string>
56
57#ifdef _MSC_VER
58#define strcasecmp _stricmp
59#define strdup _strdup
60#define strtoull _strtoui64
61#endif
62
63namespace OpenMD {
64 std::string stripComments(const std::string& S) {
65 unsigned int n = S.length();
66 std::string res;
67
68 // Flags to indicate that single line and multpile line comments
69 // have started or not.
70 bool s_cmt = false;
71 bool m_cmt = false;
72
73 // Traverse the line
74 for (unsigned int i = 0; i < n; i++) {
75 // If single line comment flag is on, then check for end of it
76 if (s_cmt == true && S[i] == '\n') s_cmt = false;
77
78 // If multiple line comment is on, then check for end of it
79 else if (m_cmt == true && S[i] == '*' && S[i + 1] == '/')
80 m_cmt = false, i++;
81
82 // If this character is in a comment, ignore it
83 else if (s_cmt || m_cmt)
84 continue;
85
86 // Check for beginning of comments and set the approproate flags
87 else if ((S[i] == '/' && S[i + 1] == '/') || (S[i] == '#'))
88 s_cmt = true, i++;
89 else if (S[i] == '/' && S[i + 1] == '*')
90 m_cmt = true, i++;
91
92 // If current character is a non-comment character, append it to res
93 else
94 res += S[i];
95 }
96 return res;
97 }
98
99 std::string UpperCase(const std::string& S) {
100 std::string uc = S;
101 unsigned int n = uc.size();
102 for (unsigned int j = 0; j < n; j++) {
103 char sj = uc[j];
104 if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A'));
105 }
106 return uc;
107 }
108
109 int findBegin(std::istream& theStream, const char* startText) {
110 const int MAXLEN = 1024;
111 char readLine[MAXLEN];
112 int foundText = 0;
113 int lineNum;
114 char* the_token;
115
116 // rewind the stream
117 theStream.seekg(0, std::ios::beg);
118 lineNum = 0;
119
120 if (!theStream.eof()) {
121 theStream.getline(readLine, MAXLEN);
122 lineNum++;
123 } else {
124 printf("Error fast forwarding stream: stream is empty.\n");
125 return -1;
126 }
127
128 while (!foundText) {
129 if (theStream.eof()) {
130 printf("Error fast forwarding stream at line %d: "
131 "stream ended unexpectedly.\n",
132 lineNum);
133 return -1;
134 }
135
136 the_token = strtok(readLine, " ,;\t");
137 if (the_token != NULL)
138 if (!strcasecmp("begin", the_token)) {
139 the_token = strtok(NULL, " ,;\t");
140 if (the_token != NULL) {
141 foundText = !strcasecmp(startText, the_token);
142 }
143 }
144
145 if (!foundText) {
146 if (!theStream.eof()) {
147 theStream.getline(readLine, MAXLEN);
148 lineNum++;
149 } else {
150 printf("Error fast forwarding stream at line %d: "
151 "stream ended unexpectedly.\n",
152 lineNum);
153 return -1;
154 }
155 }
156 }
157 return lineNum;
158 }
159
160 int isEndLine(char* line) {
161 char* working_line;
162 char* foo;
163
164 working_line = strdup(line);
165
166 foo = strtok(working_line, " ,;\t");
167
168 if (foo != NULL) {
169 if (!strcasecmp(foo, "end")) {
170 free(working_line);
171 return 1;
172 }
173 }
174
175 free(working_line);
176 return 0;
177 }
178
179 std::string OpenMD_itoa(int value, unsigned int base) {
180 const char digitMap[] = "0123456789abcdef";
181 std::string buf;
182
183 if (base == 0 || base > 16) { return buf; }
184
185 if (value == 0) {
186 buf = "0";
187 return buf;
188 }
189
190 // Take care negative int:
191
192 std::string sign;
193 int _value = value;
194 if (value < 0) {
195 _value = -value;
196 sign = "-";
197 }
198
199 // Translating number to string with base:
200 for (int i = 30; _value && i; --i) {
201 buf = digitMap[_value % base] + buf;
202 _value /= base;
203 }
204 return sign.append(buf);
205 }
206
207 std::string getPrefix(const std::string& str) {
208 return str.substr(0, str.rfind('.'));
209 }
210
211 bool isInteger(const std::string& str) {
212 bool result = false;
213
214 std::string::const_iterator i = str.begin();
215 if (i != str.end() && (*i == '+' || *i == '-' || std::isdigit(*i))) {
216 ++i;
217 while (i != str.end() && std::isdigit(*i))
218 ++i;
219 if (i == str.end()) result = true;
220 }
221
222 return result;
223 }
224
225 bool CaseInsensitiveEquals(const char ch1, const char ch2) {
226 return std::toupper((unsigned char)ch1) == std::toupper((unsigned char)ch2);
227 }
228
229 size_t CaseInsensitiveFind(const std::string& str1, const std::string& str2) {
230 std::string::const_iterator pos =
231 std::search(str1.begin(), str1.end(), str2.begin(), str2.end(),
232 CaseInsensitiveEquals);
233 if (pos == str1.end())
234 return std::string::npos;
235 else
236 return pos - str1.begin();
237 }
238} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string UpperCase(const std::string &S)
Converts a string to UPPER CASE.
int findBegin(std::istream &theStream, const char *startText)
Finds the location of the string "begin <startText>" in an input stream.
std::string getPrefix(const std::string &str)
int isEndLine(char *line)
discovers whether or not the line contains the "end" token
std::string stripComments(const std::string &S)
Strip comments from a string.