1 |
gezelter |
246 |
/* |
2 |
|
|
* 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 |
413 |
#include <stack> |
43 |
gezelter |
246 |
#include "io/SectionParserManager.hpp" |
44 |
|
|
#include "utils/Trim.hpp" |
45 |
tim |
413 |
#include "utils/simError.h" |
46 |
gezelter |
246 |
|
47 |
|
|
namespace oopse { |
48 |
|
|
|
49 |
|
|
SectionParserManager::~SectionParserManager() { |
50 |
|
|
SectionParserManager::iterator i; |
51 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
52 |
|
|
delete (i->sectionParser); |
53 |
|
|
} |
54 |
|
|
sectionParsers_.clear(); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
58 |
|
|
|
59 |
|
|
//reset active flags |
60 |
|
|
SectionParserManager::iterator i; |
61 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
62 |
|
|
i->isActive = false; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
const int bufferSize = 65535; |
66 |
|
|
char buffer[bufferSize]; |
67 |
|
|
int lineNo = 0; |
68 |
tim |
413 |
std::stack<std::string> sectionNameStack; |
69 |
gezelter |
246 |
//scan through the input stream and find section names |
70 |
|
|
while(input.getline(buffer, bufferSize)) { |
71 |
|
|
++lineNo; |
72 |
|
|
|
73 |
|
|
std::string line = trimLeftCopy(buffer); |
74 |
tim |
413 |
//a line begins with "//" is a comment line |
75 |
gezelter |
246 |
if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { |
76 |
|
|
continue; |
77 |
|
|
} else { |
78 |
|
|
StringTokenizer tokenizer(line); |
79 |
|
|
if (tokenizer.countTokens() < 2) { |
80 |
|
|
continue; |
81 |
|
|
} else { |
82 |
|
|
std::string keyword = tokenizer.nextToken(); |
83 |
|
|
|
84 |
tim |
413 |
if (keyword == "begin") { |
85 |
|
|
std::string section = tokenizer.nextToken(); |
86 |
|
|
sectionNameStack.push(section); |
87 |
gezelter |
246 |
|
88 |
tim |
413 |
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); |
89 |
|
|
if (i == sectionParsers_.end()){ |
90 |
|
|
sprintf(painCave.errMsg, "SectionParserManager Error: Can not find corresponding section parser for %s\n", |
91 |
|
|
section.c_str()); |
92 |
|
|
painCave.isFatal = 1; |
93 |
|
|
simError(); |
94 |
|
|
} else { |
95 |
|
|
if (i->isActive) { |
96 |
|
|
sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s section\n", |
97 |
|
|
section.c_str()); |
98 |
|
|
painCave.isFatal = 1; |
99 |
|
|
simError(); |
100 |
|
|
} else { |
101 |
|
|
i->isActive = true; |
102 |
|
|
i->lineNo = lineNo; |
103 |
|
|
i->offset = input.tellg(); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
} else if (keyword == "end") { |
107 |
|
|
std::string section = tokenizer.nextToken(); |
108 |
|
|
if (sectionNameStack.top() == section) { |
109 |
|
|
sectionNameStack.pop(); |
110 |
|
|
} else { |
111 |
|
|
sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", |
112 |
|
|
sectionNameStack.top().c_str(), section.c_str(), lineNo); |
113 |
|
|
painCave.isFatal = 1; |
114 |
|
|
simError(); |
115 |
|
|
} |
116 |
|
|
|
117 |
gezelter |
246 |
} else { |
118 |
tim |
413 |
continue; |
119 |
gezelter |
246 |
} |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
} |
124 |
|
|
|
125 |
tim |
413 |
if (!sectionNameStack.empty()) { |
126 |
|
|
sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); |
127 |
|
|
painCave.isFatal = 1; |
128 |
|
|
simError(); |
129 |
|
|
} |
130 |
|
|
|
131 |
gezelter |
246 |
//invoke parser |
132 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
133 |
|
|
if (i->isActive) { |
134 |
|
|
//C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail |
135 |
|
|
//It is always a good idea to call clear() before seek |
136 |
|
|
input.clear(); |
137 |
|
|
input.seekg(i->offset); |
138 |
|
|
(i->sectionParser)->parse(input, ff, i->lineNo); |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
void SectionParserManager::push_front(SectionParser* sp) { |
145 |
|
|
SectionParserManager::iterator i; |
146 |
|
|
i = findSectionParser(sp->getSectionName()); |
147 |
|
|
if (i != sectionParsers_.end()) { |
148 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
149 |
|
|
return; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
SectionParserContext context; |
153 |
|
|
|
154 |
|
|
if (sectionParsers_.empty()) { |
155 |
|
|
context.priority = beginPriority_; |
156 |
|
|
} else { |
157 |
|
|
context.priority = sectionParsers_.front().priority - priorityDifference_; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
context.sectionParser = sp; |
161 |
|
|
context.lineNo = 0; |
162 |
|
|
context.offset = 0; |
163 |
|
|
context.isActive = false; |
164 |
|
|
|
165 |
|
|
sectionParsers_.push_front(context); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void SectionParserManager::push_back(SectionParser* sp) { |
169 |
|
|
SectionParserManager::iterator i; |
170 |
|
|
i = findSectionParser(sp->getSectionName()); |
171 |
|
|
if (i != sectionParsers_.end()) { |
172 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
173 |
|
|
return; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
SectionParserContext context; |
177 |
|
|
if (sectionParsers_.empty()) { |
178 |
|
|
context.priority = beginPriority_; |
179 |
|
|
} else { |
180 |
|
|
context.priority = sectionParsers_.back().priority + priorityDifference_; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
context.sectionParser = sp; |
184 |
|
|
context.lineNo = 0; |
185 |
|
|
context.offset = 0; |
186 |
|
|
context.isActive = false; |
187 |
|
|
|
188 |
|
|
sectionParsers_.push_back(context); |
189 |
|
|
|
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
void SectionParserManager::insert(SectionParser* sp, int priority) { |
193 |
|
|
SectionParserManager::iterator i; |
194 |
|
|
i = findSectionParser(sp->getSectionName()); |
195 |
|
|
if (i != sectionParsers_.end()) { |
196 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
SectionParserContext context; |
200 |
|
|
context.priority = priority; |
201 |
|
|
context.sectionParser = sp; |
202 |
|
|
context.lineNo = 0; |
203 |
|
|
context.offset = 0; |
204 |
|
|
context.isActive = false; |
205 |
|
|
|
206 |
|
|
if (sectionParsers_.empty()) { |
207 |
|
|
sectionParsers_.push_back(context); |
208 |
|
|
} else { |
209 |
|
|
|
210 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
211 |
|
|
if (i->priority == priority) { |
212 |
|
|
std::cerr << "Priority " << priority << " already used" << std::endl; |
213 |
|
|
return; |
214 |
|
|
} else if (i->priority > priority) { |
215 |
|
|
sectionParsers_.insert(i, context); |
216 |
|
|
break; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
226 |
|
|
SectionParserManager::iterator i; |
227 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
228 |
|
|
if (i->sectionParser->getSectionName() == sectionName) { |
229 |
|
|
break; |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
return i; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
} |