1 |
/* |
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 |
#include "utils/Trim.hpp" |
28 |
|
29 |
namespace oopse { |
30 |
|
31 |
SectionParserManager::~SectionParserManager() { |
32 |
SectionParserManager::iterator i; |
33 |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
34 |
delete (i->sectionParser); |
35 |
} |
36 |
sectionParsers_.clear(); |
37 |
} |
38 |
|
39 |
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
40 |
|
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 |
|
54 |
std::string line = trimLeftCopy(buffer); |
55 |
//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 |
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); |
72 |
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 |
i->offset = input.tellg(); |
79 |
} |
80 |
|
81 |
} |
82 |
} |
83 |
|
84 |
|
85 |
} |
86 |
|
87 |
//invoke parser |
88 |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
89 |
if (i->isActive) { |
90 |
//C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail |
91 |
//It is always a good idea to call clear() before seek |
92 |
input.clear(); |
93 |
input.seekg(i->offset); |
94 |
(i->sectionParser)->parse(input, ff, i->lineNo); |
95 |
} |
96 |
} |
97 |
|
98 |
} |
99 |
|
100 |
void SectionParserManager::push_front(SectionParser* sp) { |
101 |
SectionParserManager::iterator i; |
102 |
i = findSectionParser(sp->getSectionName()); |
103 |
if (i != sectionParsers_.end()) { |
104 |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
105 |
return; |
106 |
} |
107 |
|
108 |
SectionParserContext context; |
109 |
|
110 |
if (sectionParsers_.empty()) { |
111 |
context.priority = beginPriority_; |
112 |
} else { |
113 |
context.priority = sectionParsers_.front().priority - priorityDifference_; |
114 |
} |
115 |
|
116 |
context.sectionParser = sp; |
117 |
context.lineNo = 0; |
118 |
context.offset = 0; |
119 |
context.isActive = false; |
120 |
|
121 |
sectionParsers_.push_front(context); |
122 |
} |
123 |
|
124 |
void SectionParserManager::push_back(SectionParser* sp) { |
125 |
SectionParserManager::iterator i; |
126 |
i = findSectionParser(sp->getSectionName()); |
127 |
if (i != sectionParsers_.end()) { |
128 |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
129 |
return; |
130 |
} |
131 |
|
132 |
SectionParserContext context; |
133 |
if (sectionParsers_.empty()) { |
134 |
context.priority = beginPriority_; |
135 |
} else { |
136 |
context.priority = sectionParsers_.back().priority + priorityDifference_; |
137 |
} |
138 |
|
139 |
context.sectionParser = sp; |
140 |
context.lineNo = 0; |
141 |
context.offset = 0; |
142 |
context.isActive = false; |
143 |
|
144 |
sectionParsers_.push_back(context); |
145 |
|
146 |
} |
147 |
|
148 |
void SectionParserManager::insert(SectionParser* sp, int priority) { |
149 |
SectionParserManager::iterator i; |
150 |
i = findSectionParser(sp->getSectionName()); |
151 |
if (i != sectionParsers_.end()) { |
152 |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
153 |
} |
154 |
|
155 |
SectionParserContext context; |
156 |
context.priority = priority; |
157 |
context.sectionParser = sp; |
158 |
context.lineNo = 0; |
159 |
context.offset = 0; |
160 |
context.isActive = false; |
161 |
|
162 |
if (sectionParsers_.empty()) { |
163 |
sectionParsers_.push_back(context); |
164 |
} else { |
165 |
|
166 |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
167 |
if (i->priority == priority) { |
168 |
std::cerr << "Priority " << priority << " already used" << std::endl; |
169 |
return; |
170 |
} else if (i->priority > priority) { |
171 |
sectionParsers_.insert(i, context); |
172 |
break; |
173 |
} |
174 |
|
175 |
} |
176 |
} |
177 |
|
178 |
} |
179 |
|
180 |
|
181 |
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
182 |
SectionParserManager::iterator i; |
183 |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
184 |
if (i->sectionParser->getSectionName() == sectionName) { |
185 |
break; |
186 |
} |
187 |
} |
188 |
|
189 |
return i; |
190 |
} |
191 |
|
192 |
} |