OpenMD 3.0
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 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#include "utils/StringUtils.hpp"
46
47#include <config.h>
48
49#include <algorithm>
50#include <cctype>
51#include <cstdlib>
52#include <string>
53
54#ifdef _MSC_VER
55#define strcasecmp _stricmp
56#define strdup _strdup
57#define strtoull _strtoui64
58#endif
59
60namespace OpenMD {
61 std::string UpperCase(const std::string& S) {
62 std::string uc = S;
63 unsigned int n = uc.size();
64 for (unsigned int j = 0; j < n; j++) {
65 char sj = uc[j];
66 if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A'));
67 }
68 return uc;
69 }
70
71 int findBegin(std::istream& theStream, const char* startText) {
72 const int MAXLEN = 1024;
73 char readLine[MAXLEN];
74 int foundText = 0;
75 int lineNum;
76 char* the_token;
77
78 // rewind the stream
79 theStream.seekg(0, std::ios::beg);
80 lineNum = 0;
81
82 if (!theStream.eof()) {
83 theStream.getline(readLine, MAXLEN);
84 lineNum++;
85 } else {
86 printf("Error fast forwarding stream: stream is empty.\n");
87 return -1;
88 }
89
90 while (!foundText) {
91 if (theStream.eof()) {
92 printf("Error fast forwarding stream at line %d: "
93 "stream ended unexpectedly.\n",
94 lineNum);
95 return -1;
96 }
97
98 the_token = strtok(readLine, " ,;\t");
99 if (the_token != NULL)
100 if (!strcasecmp("begin", the_token)) {
101 the_token = strtok(NULL, " ,;\t");
102 if (the_token != NULL) {
103 foundText = !strcasecmp(startText, the_token);
104 }
105 }
106
107 if (!foundText) {
108 if (!theStream.eof()) {
109 theStream.getline(readLine, MAXLEN);
110 lineNum++;
111 } else {
112 printf("Error fast forwarding stream at line %d: "
113 "stream ended unexpectedly.\n",
114 lineNum);
115 return -1;
116 }
117 }
118 }
119 return lineNum;
120 }
121
122 int isEndLine(char* line) {
123 char* working_line;
124 char* foo;
125
126 working_line = strdup(line);
127
128 foo = strtok(working_line, " ,;\t");
129
130 if (foo != NULL) {
131 if (!strcasecmp(foo, "end")) {
132 free(working_line);
133 return 1;
134 }
135 }
136
137 free(working_line);
138 return 0;
139 }
140
141 std::string OpenMD_itoa(int value, unsigned int base) {
142 const char digitMap[] = "0123456789abcdef";
143 std::string buf;
144
145 if (base == 0 || base > 16) { return buf; }
146
147 if (value == 0) {
148 buf = "0";
149 return buf;
150 }
151
152 // Take care negative int:
153
154 std::string sign;
155 int _value = value;
156 if (value < 0) {
157 _value = -value;
158 sign = "-";
159 }
160
161 // Translating number to string with base:
162 for (int i = 30; _value && i; --i) {
163 buf = digitMap[_value % base] + buf;
164 _value /= base;
165 }
166 return sign.append(buf);
167 }
168
169 std::string getPrefix(const std::string& str) {
170 return str.substr(0, str.rfind('.'));
171 }
172
173 bool isInteger(const std::string& str) {
174 bool result = false;
175
176 std::string::const_iterator i = str.begin();
177 if (i != str.end() && (*i == '+' || *i == '-' || std::isdigit(*i))) {
178 ++i;
179 while (i != str.end() && std::isdigit(*i))
180 ++i;
181 if (i == str.end()) result = true;
182 }
183
184 return result;
185 }
186
187 bool CaseInsensitiveEquals(const char ch1, const char ch2) {
188 return std::toupper((unsigned char)ch1) == std::toupper((unsigned char)ch2);
189 }
190
191 size_t CaseInsensitiveFind(const std::string& str1, const std::string& str2) {
192 std::string::const_iterator pos =
193 std::search(str1.begin(), str1.end(), str2.begin(), str2.end(),
194 CaseInsensitiveEquals);
195 if (pos == str1.end())
196 return std::string::npos;
197 else
198 return pos - str1.begin();
199 }
200} // 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