1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** \file FormatParserStorage.cpp
|
---|
9 | *
|
---|
10 | * date: Jun, 22 2010
|
---|
11 | * author: heber
|
---|
12 | *
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 | #include <fstream>
|
---|
24 |
|
---|
25 | #include "Parser/FormatParserStorage.hpp"
|
---|
26 |
|
---|
27 | #include "Parser/FormatParser.hpp"
|
---|
28 | #include "Parser/MpqcParser.hpp"
|
---|
29 | #include "Parser/PcpParser.hpp"
|
---|
30 | #include "Parser/PdbParser.hpp"
|
---|
31 | #include "Parser/TremoloParser.hpp"
|
---|
32 | #include "Parser/XyzParser.hpp"
|
---|
33 |
|
---|
34 | #include "Helpers/Log.hpp"
|
---|
35 | #include "Helpers/Verbose.hpp"
|
---|
36 |
|
---|
37 | #include "Helpers/Assert.hpp"
|
---|
38 |
|
---|
39 | #include "Patterns/Singleton_impl.hpp"
|
---|
40 |
|
---|
41 | /** Increment operator for the enumeration ParserTypes to allow loops.
|
---|
42 | * \param &type value
|
---|
43 | * \return value incremented by one
|
---|
44 | */
|
---|
45 | ParserTypes &operator++(ParserTypes &type)
|
---|
46 | {
|
---|
47 | return type = ParserTypes(type+1);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** Constructor of class FormatParserStorage.
|
---|
51 | */
|
---|
52 | FormatParserStorage::FormatParserStorage()
|
---|
53 | {
|
---|
54 | ParserList.resize(ParserTypes_end, NULL);
|
---|
55 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
56 | ParserPresent.resize(ParserTypes_end, false);
|
---|
57 |
|
---|
58 | ParserNames[mpqc] = "mpqc";
|
---|
59 | ParserNames[pcp] = "pcp";
|
---|
60 | ParserNames[pdb] = "pdb";
|
---|
61 | ParserNames[tremolo] = "tremolo";
|
---|
62 | ParserNames[xyz] = "xyz";
|
---|
63 |
|
---|
64 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
|
---|
65 | ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
66 |
|
---|
67 | ParserSuffixes[mpqc] = "in";
|
---|
68 | ParserSuffixes[pcp] = "conf";
|
---|
69 | ParserSuffixes[pdb] = "pdb";
|
---|
70 | ParserSuffixes[tremolo] = "data";
|
---|
71 | ParserSuffixes[xyz] = "xyz";
|
---|
72 |
|
---|
73 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
|
---|
74 | ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
75 |
|
---|
76 | ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
|
---|
77 | ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
|
---|
78 | ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
|
---|
79 | ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
|
---|
80 | ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Destructor of class FormatParserStorage.
|
---|
84 | * Free all stored FormatParsers.
|
---|
85 | * Save on Exit.
|
---|
86 | */
|
---|
87 | FormatParserStorage::~FormatParserStorage()
|
---|
88 | {
|
---|
89 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
90 | if (ParserPresent[iter]) {
|
---|
91 | if (ParserStream[iter]->is_open())
|
---|
92 | ParserStream[iter]->close();
|
---|
93 | delete ParserStream[iter];
|
---|
94 | delete ParserList[iter];
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
99 | * \param &prefix prefix to use.
|
---|
100 | */
|
---|
101 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
102 | {
|
---|
103 | prefix=_prefix;
|
---|
104 | };
|
---|
105 |
|
---|
106 |
|
---|
107 | void FormatParserStorage::SaveAll()
|
---|
108 | {
|
---|
109 | std::string filename;
|
---|
110 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
111 | if (ParserPresent[iter]) {
|
---|
112 | filename = prefix;
|
---|
113 | filename += ".";
|
---|
114 | filename += ParserSuffixes[iter];
|
---|
115 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
116 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /** Adds an MpqcParser to the storage.
|
---|
122 | */
|
---|
123 | void FormatParserStorage::addMpqc()
|
---|
124 | {
|
---|
125 | if (!ParserPresent[mpqc]) {
|
---|
126 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
|
---|
127 | ParserPresent[mpqc] = true;
|
---|
128 | }
|
---|
129 | else
|
---|
130 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /** Adds an PcpParser to the storage.
|
---|
135 | */
|
---|
136 | void FormatParserStorage::addPcp()
|
---|
137 | {
|
---|
138 | if (!ParserPresent[pcp]) {
|
---|
139 | ParserList[pcp] = new PcpParser();
|
---|
140 | ParserPresent[pcp] = true;
|
---|
141 | } else
|
---|
142 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /** Adds an PdbParser to the storage.
|
---|
147 | */
|
---|
148 | void FormatParserStorage::addPdb()
|
---|
149 | {
|
---|
150 | if (!ParserPresent[pdb]) {
|
---|
151 | ParserList[pdb] = new PdbParser();
|
---|
152 | ParserPresent[pdb] = true;
|
---|
153 | } else
|
---|
154 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pdb is already present." << endl);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /** Adds an TremoloParser to the storage.
|
---|
159 | */
|
---|
160 | void FormatParserStorage::addTremolo()
|
---|
161 | {
|
---|
162 | if (!ParserPresent[tremolo]) {
|
---|
163 | ParserList[tremolo] = new TremoloParser();
|
---|
164 | ParserPresent[tremolo] = true;
|
---|
165 | } else
|
---|
166 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /** Adds an XyzParser to the storage.
|
---|
171 | */
|
---|
172 | void FormatParserStorage::addXyz()
|
---|
173 | {
|
---|
174 | if (!ParserPresent[xyz]) {
|
---|
175 | ParserList[xyz] = new XyzParser();
|
---|
176 | ParserPresent[xyz] = true;
|
---|
177 | } else
|
---|
178 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
|
---|
179 | }
|
---|
180 |
|
---|
181 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
182 | {
|
---|
183 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
184 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
185 | return ParserTypes_end;
|
---|
186 | } else
|
---|
187 | return ParserLookupNames[type];
|
---|
188 | }
|
---|
189 |
|
---|
190 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
191 | {
|
---|
192 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
193 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
194 | return ParserTypes_end;
|
---|
195 | } else
|
---|
196 | return ParserLookupSuffixes[type];
|
---|
197 | }
|
---|
198 |
|
---|
199 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
200 | {
|
---|
201 | if (ptype != ParserTypes_end) {
|
---|
202 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
203 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
204 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
205 | return true;
|
---|
206 | } else {
|
---|
207 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 | } else {
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | bool FormatParserStorage::add(std::string type)
|
---|
216 | {
|
---|
217 | return add(getTypeFromName(type));
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /** Parses an istream depending on its suffix
|
---|
222 | * \param &input input stream
|
---|
223 | * \param suffix
|
---|
224 | * \return true - parsing ok, false - suffix unknown
|
---|
225 | */
|
---|
226 | bool FormatParserStorage::get(std::istream &input, std::string suffix)
|
---|
227 | {
|
---|
228 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
229 | getMpqc().load(&input);
|
---|
230 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
231 | getPcp().load(&input);
|
---|
232 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
233 | getPdb().load(&input);
|
---|
234 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
235 | getTremolo().load(&input);
|
---|
236 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
237 | getXyz().load(&input);
|
---|
238 | } else {
|
---|
239 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
240 | return false;
|
---|
241 | }
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /** Stores world in an ostream depending on its suffix
|
---|
247 | * \param &output output stream
|
---|
248 | * \param suffix
|
---|
249 | * \return true - storing ok, false - suffix unknown
|
---|
250 | */
|
---|
251 | bool FormatParserStorage::put(std::ostream &output, std::string suffix)
|
---|
252 | {
|
---|
253 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
254 | getMpqc().save(&output);
|
---|
255 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
256 | getPcp().save(&output);
|
---|
257 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
258 | getPdb().save(&output);
|
---|
259 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
260 | getTremolo().save(&output);
|
---|
261 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
262 | getXyz().save(&output);
|
---|
263 | } else {
|
---|
264 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
|
---|
265 | return false;
|
---|
266 | }
|
---|
267 | return true;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** Returns reference to the output MpqcParser, adds if not present.
|
---|
271 | * \return reference to the output MpqcParser
|
---|
272 | */
|
---|
273 | MpqcParser &FormatParserStorage::getMpqc()
|
---|
274 | {
|
---|
275 | if (!ParserPresent[mpqc])
|
---|
276 | addMpqc();
|
---|
277 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** Returns reference to the output PcpParser, adds if not present.
|
---|
281 | * \return reference to the output PcpParser
|
---|
282 | */
|
---|
283 | PcpParser &FormatParserStorage::getPcp()
|
---|
284 | {
|
---|
285 | if (!ParserPresent[pcp])
|
---|
286 | addPcp();
|
---|
287 | return dynamic_cast<PcpParser &>(*ParserList[pcp]);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Returns reference to the output PdbParser, adds if not present.
|
---|
291 | * \return reference to the output PdbParser
|
---|
292 | */
|
---|
293 | PdbParser &FormatParserStorage::getPdb()
|
---|
294 | {
|
---|
295 | if (!ParserPresent[pdb])
|
---|
296 | addPdb();
|
---|
297 | return dynamic_cast<PdbParser &>(*ParserList[pdb]);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Returns reference to the output TremoloParser, adds if not present.
|
---|
301 | * \return reference to the output TremoloParser
|
---|
302 | */
|
---|
303 | TremoloParser &FormatParserStorage::getTremolo()
|
---|
304 | {
|
---|
305 | if (!ParserPresent[tremolo])
|
---|
306 | addTremolo();
|
---|
307 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Returns reference to the output XyzParser, adds if not present.
|
---|
311 | * \return reference to the output XyzParser
|
---|
312 | */
|
---|
313 | XyzParser &FormatParserStorage::getXyz()
|
---|
314 | {
|
---|
315 | if (!ParserPresent[xyz])
|
---|
316 | addXyz();
|
---|
317 | return dynamic_cast<XyzParser &>(*ParserList[xyz]);
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 |
|
---|
322 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|