1 |
gezelter |
507 |
/* |
2 |
gezelter |
246 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
gezelter |
1850 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
gezelter |
1665 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
gezelter |
246 |
*/ |
42 |
|
|
#include <algorithm> |
43 |
tim |
413 |
#include <stack> |
44 |
jmarr |
1401 |
#include <cstdio> |
45 |
gezelter |
246 |
#include "io/SectionParserManager.hpp" |
46 |
|
|
#include "utils/Trim.hpp" |
47 |
tim |
413 |
#include "utils/simError.h" |
48 |
gezelter |
246 |
|
49 |
gezelter |
1390 |
namespace OpenMD { |
50 |
gezelter |
246 |
|
51 |
gezelter |
507 |
SectionParserManager::~SectionParserManager() { |
52 |
gezelter |
246 |
SectionParserManager::iterator i; |
53 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
54 |
gezelter |
507 |
delete (i->sectionParser); |
55 |
gezelter |
246 |
} |
56 |
|
|
sectionParsers_.clear(); |
57 |
gezelter |
507 |
} |
58 |
gezelter |
246 |
|
59 |
gezelter |
507 |
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
60 |
gezelter |
246 |
|
61 |
|
|
//reset active flags |
62 |
|
|
SectionParserManager::iterator i; |
63 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
64 |
gezelter |
507 |
i->isActive = false; |
65 |
gezelter |
246 |
} |
66 |
|
|
|
67 |
|
|
const int bufferSize = 65535; |
68 |
|
|
char buffer[bufferSize]; |
69 |
|
|
int lineNo = 0; |
70 |
tim |
413 |
std::stack<std::string> sectionNameStack; |
71 |
gezelter |
246 |
//scan through the input stream and find section names |
72 |
|
|
while(input.getline(buffer, bufferSize)) { |
73 |
gezelter |
507 |
++lineNo; |
74 |
gezelter |
246 |
|
75 |
gezelter |
507 |
std::string line = trimLeftCopy(buffer); |
76 |
|
|
//a line begins with "//" is a comment line |
77 |
chrisfen |
514 |
if ( line.empty() || (line.size() >= 2 && line[0] == '/' |
78 |
|
|
&& line[1] == '/') ) { |
79 |
gezelter |
507 |
continue; |
80 |
|
|
} else { |
81 |
|
|
StringTokenizer tokenizer(line); |
82 |
|
|
if (tokenizer.countTokens() < 2) { |
83 |
|
|
continue; |
84 |
|
|
} else { |
85 |
|
|
std::string keyword = tokenizer.nextToken(); |
86 |
gezelter |
246 |
|
87 |
gezelter |
507 |
if (keyword == "begin") { |
88 |
|
|
std::string section = tokenizer.nextToken(); |
89 |
|
|
sectionNameStack.push(section); |
90 |
chrisfen |
514 |
|
91 |
|
|
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), |
92 |
|
|
SameSectionParserFunctor(section)); |
93 |
gezelter |
507 |
if (i == sectionParsers_.end()){ |
94 |
chrisfen |
514 |
sprintf(painCave.errMsg, |
95 |
|
|
"SectionParserManager Error: Can not find corresponding " |
96 |
|
|
"section parser for %s\n", |
97 |
|
|
section.c_str()); |
98 |
gezelter |
507 |
painCave.isFatal = 1; |
99 |
|
|
simError(); |
100 |
|
|
} else { |
101 |
|
|
if (i->isActive) { |
102 |
chrisfen |
514 |
sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s " |
103 |
|
|
"section\n", |
104 |
|
|
section.c_str()); |
105 |
|
|
painCave.isFatal = 1; |
106 |
|
|
simError(); |
107 |
gezelter |
507 |
} else { |
108 |
chrisfen |
514 |
i->isActive = true; |
109 |
|
|
i->lineNo = lineNo; |
110 |
|
|
i->offset = input.tellg(); |
111 |
gezelter |
507 |
} |
112 |
|
|
} |
113 |
|
|
} else if (keyword == "end") { |
114 |
|
|
std::string section = tokenizer.nextToken(); |
115 |
|
|
if (sectionNameStack.top() == section) { |
116 |
|
|
sectionNameStack.pop(); |
117 |
|
|
} else { |
118 |
|
|
sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", |
119 |
chrisfen |
514 |
sectionNameStack.top().c_str(), section.c_str(), lineNo); |
120 |
gezelter |
507 |
painCave.isFatal = 1; |
121 |
|
|
simError(); |
122 |
|
|
} |
123 |
chrisfen |
514 |
|
124 |
gezelter |
507 |
} else { |
125 |
|
|
continue; |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
} |
129 |
chrisfen |
514 |
|
130 |
gezelter |
246 |
} |
131 |
chrisfen |
514 |
|
132 |
tim |
413 |
if (!sectionNameStack.empty()) { |
133 |
gezelter |
507 |
sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); |
134 |
|
|
painCave.isFatal = 1; |
135 |
|
|
simError(); |
136 |
tim |
413 |
} |
137 |
chrisfen |
514 |
|
138 |
gezelter |
246 |
//invoke parser |
139 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
140 |
gezelter |
507 |
if (i->isActive) { |
141 |
chrisfen |
514 |
//C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail |
142 |
|
|
//It is always a good idea to call clear() before seek |
143 |
|
|
input.clear(); |
144 |
|
|
input.seekg(i->offset); |
145 |
|
|
(i->sectionParser)->parse(input, ff, i->lineNo); |
146 |
tim |
749 |
(i->sectionParser)->validateSection(); |
147 |
gezelter |
507 |
} |
148 |
gezelter |
246 |
} |
149 |
|
|
|
150 |
gezelter |
507 |
} |
151 |
chrisfen |
514 |
|
152 |
gezelter |
507 |
void SectionParserManager::push_front(SectionParser* sp) { |
153 |
gezelter |
246 |
SectionParserManager::iterator i; |
154 |
|
|
i = findSectionParser(sp->getSectionName()); |
155 |
|
|
if (i != sectionParsers_.end()) { |
156 |
gezelter |
1277 |
std::cerr << sp->getSectionName() << " section parser already exists" |
157 |
chrisfen |
514 |
<< std::endl; |
158 |
gezelter |
507 |
return; |
159 |
gezelter |
246 |
} |
160 |
|
|
|
161 |
|
|
SectionParserContext context; |
162 |
|
|
|
163 |
|
|
if (sectionParsers_.empty()) { |
164 |
gezelter |
507 |
context.priority = beginPriority_; |
165 |
gezelter |
246 |
} else { |
166 |
gezelter |
507 |
context.priority = sectionParsers_.front().priority - priorityDifference_; |
167 |
gezelter |
246 |
} |
168 |
chrisfen |
514 |
|
169 |
gezelter |
246 |
context.sectionParser = sp; |
170 |
|
|
context.lineNo = 0; |
171 |
|
|
context.offset = 0; |
172 |
|
|
context.isActive = false; |
173 |
|
|
|
174 |
|
|
sectionParsers_.push_front(context); |
175 |
gezelter |
507 |
} |
176 |
gezelter |
246 |
|
177 |
gezelter |
507 |
void SectionParserManager::push_back(SectionParser* sp) { |
178 |
gezelter |
246 |
SectionParserManager::iterator i; |
179 |
|
|
i = findSectionParser(sp->getSectionName()); |
180 |
|
|
if (i != sectionParsers_.end()) { |
181 |
gezelter |
1277 |
std::cerr << sp->getSectionName() << " section parser already exists" |
182 |
chrisfen |
514 |
<< std::endl; |
183 |
gezelter |
507 |
return; |
184 |
gezelter |
246 |
} |
185 |
|
|
|
186 |
|
|
SectionParserContext context; |
187 |
|
|
if (sectionParsers_.empty()) { |
188 |
gezelter |
507 |
context.priority = beginPriority_; |
189 |
gezelter |
246 |
} else { |
190 |
gezelter |
507 |
context.priority = sectionParsers_.back().priority + priorityDifference_; |
191 |
gezelter |
246 |
} |
192 |
|
|
|
193 |
|
|
context.sectionParser = sp; |
194 |
|
|
context.lineNo = 0; |
195 |
|
|
context.offset = 0; |
196 |
|
|
context.isActive = false; |
197 |
|
|
|
198 |
|
|
sectionParsers_.push_back(context); |
199 |
|
|
|
200 |
gezelter |
507 |
} |
201 |
gezelter |
246 |
|
202 |
gezelter |
507 |
void SectionParserManager::insert(SectionParser* sp, int priority) { |
203 |
gezelter |
246 |
SectionParserManager::iterator i; |
204 |
|
|
i = findSectionParser(sp->getSectionName()); |
205 |
|
|
if (i != sectionParsers_.end()) { |
206 |
gezelter |
1277 |
std::cerr << sp->getSectionName() << " section parser already exists" |
207 |
chrisfen |
514 |
<< std::endl; |
208 |
gezelter |
246 |
} |
209 |
|
|
|
210 |
|
|
SectionParserContext context; |
211 |
|
|
context.priority = priority; |
212 |
|
|
context.sectionParser = sp; |
213 |
|
|
context.lineNo = 0; |
214 |
|
|
context.offset = 0; |
215 |
|
|
context.isActive = false; |
216 |
|
|
|
217 |
|
|
if (sectionParsers_.empty()) { |
218 |
gezelter |
507 |
sectionParsers_.push_back(context); |
219 |
gezelter |
246 |
} else { |
220 |
|
|
|
221 |
gezelter |
507 |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
222 |
|
|
if (i->priority == priority) { |
223 |
|
|
std::cerr << "Priority " << priority << " already used" << std::endl; |
224 |
|
|
return; |
225 |
|
|
} else if (i->priority > priority) { |
226 |
|
|
sectionParsers_.insert(i, context); |
227 |
|
|
break; |
228 |
|
|
} |
229 |
gezelter |
246 |
|
230 |
gezelter |
507 |
} |
231 |
gezelter |
246 |
} |
232 |
|
|
|
233 |
gezelter |
507 |
} |
234 |
gezelter |
246 |
|
235 |
|
|
|
236 |
gezelter |
507 |
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
237 |
gezelter |
246 |
SectionParserManager::iterator i; |
238 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
239 |
gezelter |
507 |
if (i->sectionParser->getSectionName() == sectionName) { |
240 |
chrisfen |
514 |
break; |
241 |
gezelter |
507 |
} |
242 |
gezelter |
246 |
} |
243 |
|
|
|
244 |
|
|
return i; |
245 |
gezelter |
507 |
} |
246 |
gezelter |
246 |
|
247 |
|
|
} |