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