1 |
tim |
1765 |
/* |
2 |
|
|
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
|
|
* |
4 |
|
|
* Contact: oopse@oopse.org |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2.1 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* All we ask is that proper credit is given for our work, which includes |
11 |
|
|
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
|
|
* of your source code files, and to any copyright notice that you may distribute |
13 |
|
|
* with programs based on this work. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU Lesser General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
21 |
|
|
* along with this program; if not, write to the Free Software |
22 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
|
* |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
#include "io/SectionParserManager.hpp" |
27 |
tim |
1840 |
#include "utils/Trim.hpp" |
28 |
tim |
1765 |
|
29 |
|
|
namespace oopse { |
30 |
|
|
|
31 |
|
|
SectionParserManager::~SectionParserManager() { |
32 |
|
|
SectionParserManager::iterator i; |
33 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
34 |
tim |
1789 |
delete (i->sectionParser); |
35 |
tim |
1765 |
} |
36 |
|
|
sectionParsers_.clear(); |
37 |
|
|
} |
38 |
|
|
|
39 |
tim |
1789 |
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
40 |
tim |
1765 |
|
41 |
|
|
//reset active flags |
42 |
|
|
SectionParserManager::iterator i; |
43 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
44 |
|
|
i->isActive = false; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
const int bufferSize = 65535; |
48 |
|
|
char buffer[bufferSize]; |
49 |
|
|
int lineNo = 0; |
50 |
|
|
//scan through the input stream and find section names |
51 |
|
|
while(input.getline(buffer, bufferSize)) { |
52 |
|
|
++lineNo; |
53 |
tim |
1840 |
|
54 |
|
|
std::string line = trimLeftCopy(buffer); |
55 |
tim |
1765 |
//a line begins with "//" is comment |
56 |
|
|
if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { |
57 |
|
|
continue; |
58 |
|
|
} else { |
59 |
|
|
StringTokenizer tokenizer(line); |
60 |
|
|
if (tokenizer.countTokens() < 2) { |
61 |
|
|
continue; |
62 |
|
|
} else { |
63 |
|
|
std::string keyword = tokenizer.nextToken(); |
64 |
|
|
|
65 |
|
|
if (keyword != "begin") { |
66 |
|
|
continue; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
std::string section = tokenizer.nextToken(); |
70 |
|
|
|
71 |
tim |
1801 |
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); |
72 |
tim |
1765 |
if (i == sectionParsers_.end()){ |
73 |
|
|
//can not find corresponding section parser |
74 |
|
|
std::cerr << "Can not find corresponding section parser for section: " << section << std::endl; |
75 |
|
|
} else { |
76 |
|
|
i->isActive = true; |
77 |
|
|
i->lineNo = lineNo; |
78 |
tim |
1790 |
i->offset = input.tellg(); |
79 |
tim |
1765 |
} |
80 |
|
|
|
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
//invoke parser |
88 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
89 |
|
|
if (i->isActive) { |
90 |
tim |
1790 |
input.seekg(i->offset); |
91 |
tim |
1765 |
(i->sectionParser)->parse(input, ff, i->lineNo); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
void SectionParserManager::push_front(SectionParser* sp) { |
98 |
|
|
SectionParserManager::iterator i; |
99 |
|
|
i = findSectionParser(sp->getSectionName()); |
100 |
|
|
if (i != sectionParsers_.end()) { |
101 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
102 |
|
|
return; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
SectionParserContext context; |
106 |
|
|
|
107 |
|
|
if (sectionParsers_.empty()) { |
108 |
|
|
context.priority = beginPriority_; |
109 |
|
|
} else { |
110 |
|
|
context.priority = sectionParsers_.front().priority - priorityDifference_; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
context.sectionParser = sp; |
114 |
|
|
context.lineNo = 0; |
115 |
|
|
context.offset = 0; |
116 |
|
|
context.isActive = false; |
117 |
|
|
|
118 |
tim |
1840 |
sectionParsers_.push_front(context); |
119 |
tim |
1765 |
} |
120 |
|
|
|
121 |
|
|
void SectionParserManager::push_back(SectionParser* sp) { |
122 |
|
|
SectionParserManager::iterator i; |
123 |
|
|
i = findSectionParser(sp->getSectionName()); |
124 |
|
|
if (i != sectionParsers_.end()) { |
125 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
126 |
|
|
return; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
SectionParserContext context; |
130 |
|
|
if (sectionParsers_.empty()) { |
131 |
|
|
context.priority = beginPriority_; |
132 |
|
|
} else { |
133 |
|
|
context.priority = sectionParsers_.back().priority + priorityDifference_; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
context.sectionParser = sp; |
137 |
|
|
context.lineNo = 0; |
138 |
|
|
context.offset = 0; |
139 |
|
|
context.isActive = false; |
140 |
|
|
|
141 |
tim |
1840 |
sectionParsers_.push_back(context); |
142 |
|
|
|
143 |
tim |
1765 |
} |
144 |
|
|
|
145 |
|
|
void SectionParserManager::insert(SectionParser* sp, int priority) { |
146 |
|
|
SectionParserManager::iterator i; |
147 |
|
|
i = findSectionParser(sp->getSectionName()); |
148 |
|
|
if (i != sectionParsers_.end()) { |
149 |
|
|
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
SectionParserContext context; |
153 |
|
|
context.priority = priority; |
154 |
|
|
context.sectionParser = sp; |
155 |
|
|
context.lineNo = 0; |
156 |
|
|
context.offset = 0; |
157 |
|
|
context.isActive = false; |
158 |
|
|
|
159 |
|
|
if (sectionParsers_.empty()) { |
160 |
|
|
sectionParsers_.push_back(context); |
161 |
|
|
} else { |
162 |
|
|
|
163 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
164 |
|
|
if (i->priority == priority) { |
165 |
|
|
std::cerr << "Priority " << priority << " already used" << std::endl; |
166 |
|
|
return; |
167 |
|
|
} else if (i->priority > priority) { |
168 |
|
|
sectionParsers_.insert(i, context); |
169 |
|
|
break; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
} |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
|
178 |
tim |
1789 |
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
179 |
tim |
1765 |
SectionParserManager::iterator i; |
180 |
|
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
181 |
|
|
if (i->sectionParser->getSectionName() == sectionName) { |
182 |
|
|
break; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
return i; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
} |