ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/io/SectionParserManager.cpp
Revision: 1765
Committed: Mon Nov 22 20:55:52 2004 UTC (19 years, 7 months ago) by tim
File size: 5803 byte(s)
Log Message:
adding section parsers

File Contents

# Content
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
28 namespace oopse {
29
30 SectionParserManager::~SectionParserManager() {
31 SectionParserManager::iterator i;
32 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
33 delete (i->sp);
34 }
35 sectionParsers_.clear();
36 }
37
38 void SectionParserManager::parse(ifstrstream& input, ForceField& ff) {
39
40 //reset active flags
41 SectionParserManager::iterator i;
42 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
43 i->isActive = false;
44 }
45
46 const int bufferSize = 65535;
47 char buffer[bufferSize];
48 int lineNo = 0;
49 //scan through the input stream and find section names
50 while(input.getline(buffer, bufferSize)) {
51 ++lineNo;
52 line = LeftTrim(buffer);
53
54 //a line begins with "//" is comment
55 if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) {
56 continue;
57 } else {
58 StringTokenizer tokenizer(line);
59 if (tokenizer.countTokens() < 2) {
60 continue;
61 } else {
62 std::string keyword = tokenizer.nextToken();
63
64 if (keyword != "begin") {
65 continue;
66 }
67
68 std::string section = tokenizer.nextToken();
69
70 i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), predict);
71 if (i == sectionParsers_.end()){
72 //can not find corresponding section parser
73 std::cerr << "Can not find corresponding section parser for section: " << section << std::endl;
74 } else {
75 i->isActive = true;
76 i->lineNo = lineNo;
77 i->offset = input->tellg();
78 }
79
80 }
81 }
82
83
84 }
85
86 //invoke parser
87 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
88 if (i->isActive) {
89 input->seekg(i->offset);
90 (i->sectionParser)->parse(input, ff, i->lineNo);
91 }
92 }
93
94 }
95
96 void SectionParserManager::push_front(SectionParser* sp) {
97 SectionParserManager::iterator i;
98 i = findSectionParser(sp->getSectionName());
99 if (i != sectionParsers_.end()) {
100 std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
101 return;
102 }
103
104 SectionParserContext context;
105
106 if (sectionParsers_.empty()) {
107 context.priority = beginPriority_;
108 } else {
109 context.priority = sectionParsers_.front().priority - priorityDifference_;
110 }
111
112 context.sectionParser = sp;
113 context.lineNo = 0;
114 context.offset = 0;
115 context.isActive = false;
116
117 }
118
119 void SectionParserManager::push_back(SectionParser* sp) {
120 SectionParserManager::iterator i;
121 i = findSectionParser(sp->getSectionName());
122 if (i != sectionParsers_.end()) {
123 std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
124 return;
125 }
126
127 SectionParserContext context;
128 if (sectionParsers_.empty()) {
129 context.priority = beginPriority_;
130 } else {
131 context.priority = sectionParsers_.back().priority + priorityDifference_;
132 }
133
134 context.sectionParser = sp;
135 context.lineNo = 0;
136 context.offset = 0;
137 context.isActive = false;
138
139 }
140
141 void SectionParserManager::insert(SectionParser* sp, int priority) {
142 SectionParserManager::iterator i;
143 i = findSectionParser(sp->getSectionName());
144 if (i != sectionParsers_.end()) {
145 std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
146 }
147
148 SectionParserContext context;
149 context.priority = priority;
150 context.sectionParser = sp;
151 context.lineNo = 0;
152 context.offset = 0;
153 context.isActive = false;
154
155 if (sectionParsers_.empty()) {
156 sectionParsers_.push_back(context);
157 } else {
158
159 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
160 if (i->priority == priority) {
161 std::cerr << "Priority " << priority << " already used" << std::endl;
162 return;
163 } else if (i->priority > priority) {
164 sectionParsers_.insert(i, context);
165 break;
166 }
167
168 }
169 }
170
171 }
172
173
174 iterator SectionParserManager::findSectionParser(const std::string& sectionName) {
175 SectionParserManager::iterator i;
176 for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
177 if (i->sectionParser->getSectionName() == sectionName) {
178 break;
179 }
180 }
181
182 return i;
183 }
184
185 }