ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/SectionParserManager.cpp
Revision: 1850
Committed: Wed Feb 20 15:39:39 2013 UTC (12 years, 2 months ago) by gezelter
File size: 8158 byte(s)
Log Message:
Fixed a widespread typo in the license 

File Contents

# Content
1 /*
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. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * 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 *
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 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 */
42 #include <algorithm>
43 #include <stack>
44 #include <cstdio>
45 #include "io/SectionParserManager.hpp"
46 #include "utils/Trim.hpp"
47 #include "utils/simError.h"
48
49 namespace OpenMD {
50
51 SectionParserManager::~SectionParserManager() {
52 SectionParserManager::iterator i;
53 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
54 delete (i->sectionParser);
55 }
56 sectionParsers_.clear();
57 }
58
59 void SectionParserManager::parse(std::istream& input, ForceField& ff) {
60
61 //reset active flags
62 SectionParserManager::iterator i;
63 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
64 i->isActive = false;
65 }
66
67 const int bufferSize = 65535;
68 char buffer[bufferSize];
69 int lineNo = 0;
70 std::stack<std::string> sectionNameStack;
71 //scan through the input stream and find section names
72 while(input.getline(buffer, bufferSize)) {
73 ++lineNo;
74
75 std::string line = trimLeftCopy(buffer);
76 //a line begins with "//" is a comment line
77 if ( line.empty() || (line.size() >= 2 && line[0] == '/'
78 && line[1] == '/') ) {
79 continue;
80 } else {
81 StringTokenizer tokenizer(line);
82 if (tokenizer.countTokens() < 2) {
83 continue;
84 } else {
85 std::string keyword = tokenizer.nextToken();
86
87 if (keyword == "begin") {
88 std::string section = tokenizer.nextToken();
89 sectionNameStack.push(section);
90
91 i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(),
92 SameSectionParserFunctor(section));
93 if (i == sectionParsers_.end()){
94 sprintf(painCave.errMsg,
95 "SectionParserManager Error: Can not find corresponding "
96 "section parser for %s\n",
97 section.c_str());
98 painCave.isFatal = 1;
99 simError();
100 } else {
101 if (i->isActive) {
102 sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s "
103 "section\n",
104 section.c_str());
105 painCave.isFatal = 1;
106 simError();
107 } else {
108 i->isActive = true;
109 i->lineNo = lineNo;
110 i->offset = input.tellg();
111 }
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 sectionNameStack.top().c_str(), section.c_str(), lineNo);
120 painCave.isFatal = 1;
121 simError();
122 }
123
124 } else {
125 continue;
126 }
127 }
128 }
129
130 }
131
132 if (!sectionNameStack.empty()) {
133 sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n");
134 painCave.isFatal = 1;
135 simError();
136 }
137
138 //invoke parser
139 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
140 if (i->isActive) {
141 //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 (i->sectionParser)->validateSection();
147 }
148 }
149
150 }
151
152 void SectionParserManager::push_front(SectionParser* sp) {
153 SectionParserManager::iterator i;
154 i = findSectionParser(sp->getSectionName());
155 if (i != sectionParsers_.end()) {
156 std::cerr << sp->getSectionName() << " section parser already exists"
157 << std::endl;
158 return;
159 }
160
161 SectionParserContext context;
162
163 if (sectionParsers_.empty()) {
164 context.priority = beginPriority_;
165 } else {
166 context.priority = sectionParsers_.front().priority - priorityDifference_;
167 }
168
169 context.sectionParser = sp;
170 context.lineNo = 0;
171 context.offset = 0;
172 context.isActive = false;
173
174 sectionParsers_.push_front(context);
175 }
176
177 void SectionParserManager::push_back(SectionParser* sp) {
178 SectionParserManager::iterator i;
179 i = findSectionParser(sp->getSectionName());
180 if (i != sectionParsers_.end()) {
181 std::cerr << sp->getSectionName() << " section parser already exists"
182 << std::endl;
183 return;
184 }
185
186 SectionParserContext context;
187 if (sectionParsers_.empty()) {
188 context.priority = beginPriority_;
189 } else {
190 context.priority = sectionParsers_.back().priority + priorityDifference_;
191 }
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 }
201
202 void SectionParserManager::insert(SectionParser* sp, int priority) {
203 SectionParserManager::iterator i;
204 i = findSectionParser(sp->getSectionName());
205 if (i != sectionParsers_.end()) {
206 std::cerr << sp->getSectionName() << " section parser already exists"
207 << std::endl;
208 }
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 sectionParsers_.push_back(context);
219 } else {
220
221 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
230 }
231 }
232
233 }
234
235
236 SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) {
237 SectionParserManager::iterator i;
238 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
239 if (i->sectionParser->getSectionName() == sectionName) {
240 break;
241 }
242 }
243
244 return i;
245 }
246
247 }

Properties

Name Value
svn:keywords Author Id Revision Date