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 | ParserSuffix.resize(ParserTypes_end, "");
|
---|
58 |
|
---|
59 | ParserNames[mpqc] = "mpqc";
|
---|
60 | ParserNames[pcp] = "pcp";
|
---|
61 | ParserNames[pdb] = "pdb";
|
---|
62 | ParserNames[tremolo] = "tremolo";
|
---|
63 | ParserNames[xyz] = "xyz";
|
---|
64 |
|
---|
65 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
|
---|
66 | ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
67 |
|
---|
68 | ParserSuffix[mpqc] = "in";
|
---|
69 | ParserSuffix[pcp] = "conf";
|
---|
70 | ParserSuffix[pdb] = "pdb";
|
---|
71 | ParserSuffix[tremolo] = "data";
|
---|
72 | ParserSuffix[xyz] = "xyz";
|
---|
73 |
|
---|
74 | ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
|
---|
75 | ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
|
---|
76 | ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
|
---|
77 | ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
|
---|
78 | ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Destructor of class FormatParserStorage.
|
---|
82 | * Free all stored FormatParsers.
|
---|
83 | * Save on Exit.
|
---|
84 | */
|
---|
85 | FormatParserStorage::~FormatParserStorage()
|
---|
86 | {
|
---|
87 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
88 | if (ParserPresent[iter]) {
|
---|
89 | if (ParserStream[iter]->is_open())
|
---|
90 | ParserStream[iter]->close();
|
---|
91 | delete ParserStream[iter];
|
---|
92 | delete ParserList[iter];
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
97 | * \param &prefix prefix to use.
|
---|
98 | */
|
---|
99 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
100 | {
|
---|
101 | prefix=_prefix;
|
---|
102 | };
|
---|
103 |
|
---|
104 |
|
---|
105 | void FormatParserStorage::SaveAll()
|
---|
106 | {
|
---|
107 | std::string filename;
|
---|
108 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
109 | if (ParserPresent[iter]) {
|
---|
110 | filename = prefix;
|
---|
111 | filename += ".";
|
---|
112 | filename += ParserSuffix[iter];
|
---|
113 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
114 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /** Adds an MpqcParser to the storage.
|
---|
120 | */
|
---|
121 | void FormatParserStorage::addMpqc()
|
---|
122 | {
|
---|
123 | if (!ParserPresent[mpqc]) {
|
---|
124 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
|
---|
125 | ParserPresent[mpqc] = true;
|
---|
126 | }
|
---|
127 | else
|
---|
128 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** Adds an PcpParser to the storage.
|
---|
133 | */
|
---|
134 | void FormatParserStorage::addPcp()
|
---|
135 | {
|
---|
136 | if (!ParserPresent[pcp]) {
|
---|
137 | ParserList[pcp] = new PcpParser();
|
---|
138 | ParserPresent[pcp] = true;
|
---|
139 | } else
|
---|
140 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** Adds an PdbParser to the storage.
|
---|
145 | */
|
---|
146 | void FormatParserStorage::addPdb()
|
---|
147 | {
|
---|
148 | if (!ParserPresent[pdb]) {
|
---|
149 | ParserList[pdb] = new PdbParser();
|
---|
150 | ParserPresent[pdb] = true;
|
---|
151 | } else
|
---|
152 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pdb is already present." << endl);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /** Adds an TremoloParser to the storage.
|
---|
157 | */
|
---|
158 | void FormatParserStorage::addTremolo()
|
---|
159 | {
|
---|
160 | if (!ParserPresent[tremolo]) {
|
---|
161 | ParserList[tremolo] = new TremoloParser();
|
---|
162 | ParserPresent[tremolo] = true;
|
---|
163 | } else
|
---|
164 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /** Adds an XyzParser to the storage.
|
---|
169 | */
|
---|
170 | void FormatParserStorage::addXyz()
|
---|
171 | {
|
---|
172 | if (!ParserPresent[xyz]) {
|
---|
173 | ParserList[xyz] = new XyzParser();
|
---|
174 | ParserPresent[xyz] = true;
|
---|
175 | } else
|
---|
176 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
|
---|
177 | }
|
---|
178 |
|
---|
179 | ParserTypes FormatParserStorage::getType(std::string type)
|
---|
180 | {
|
---|
181 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
182 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
183 | return ParserTypes_end;
|
---|
184 | } else
|
---|
185 | return ParserLookupNames[type];
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
189 | {
|
---|
190 | if (ptype != ParserTypes_end) {
|
---|
191 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
192 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
193 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
194 | return true;
|
---|
195 | } else {
|
---|
196 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
197 | return false;
|
---|
198 | }
|
---|
199 | } else {
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | bool FormatParserStorage::add(std::string type)
|
---|
205 | {
|
---|
206 | return add(getType(type));
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /** Parses an istream depending on its suffix
|
---|
211 | * \param &input input stream
|
---|
212 | * \param suffix
|
---|
213 | * \return true - parsing ok, false - suffix unknown
|
---|
214 | */
|
---|
215 | bool FormatParserStorage::get(std::istream &input, std::string suffix)
|
---|
216 | {
|
---|
217 | if (suffix == ParserSuffix[mpqc]) {
|
---|
218 | getMpqc().load(&input);
|
---|
219 | } else if (suffix == ParserSuffix[pcp]) {
|
---|
220 | getPcp().load(&input);
|
---|
221 | } else if (suffix == ParserSuffix[pdb]) {
|
---|
222 | getPdb().load(&input);
|
---|
223 | } else if (suffix == ParserSuffix[tremolo]) {
|
---|
224 | getTremolo().load(&input);
|
---|
225 | } else if (suffix == ParserSuffix[xyz]) {
|
---|
226 | getXyz().load(&input);
|
---|
227 | } else {
|
---|
228 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | return true;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /** Returns reference to the output MpqcParser, adds if not present.
|
---|
235 | * \return reference to the output MpqcParser
|
---|
236 | */
|
---|
237 | MpqcParser &FormatParserStorage::getMpqc()
|
---|
238 | {
|
---|
239 | if (!ParserPresent[mpqc])
|
---|
240 | addMpqc();
|
---|
241 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Returns reference to the output PcpParser, adds if not present.
|
---|
245 | * \return reference to the output PcpParser
|
---|
246 | */
|
---|
247 | PcpParser &FormatParserStorage::getPcp()
|
---|
248 | {
|
---|
249 | if (!ParserPresent[pcp])
|
---|
250 | addPcp();
|
---|
251 | return dynamic_cast<PcpParser &>(*ParserList[pcp]);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** Returns reference to the output PdbParser, adds if not present.
|
---|
255 | * \return reference to the output PdbParser
|
---|
256 | */
|
---|
257 | PdbParser &FormatParserStorage::getPdb()
|
---|
258 | {
|
---|
259 | if (!ParserPresent[pdb])
|
---|
260 | addPdb();
|
---|
261 | return dynamic_cast<PdbParser &>(*ParserList[pdb]);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** Returns reference to the output TremoloParser, adds if not present.
|
---|
265 | * \return reference to the output TremoloParser
|
---|
266 | */
|
---|
267 | TremoloParser &FormatParserStorage::getTremolo()
|
---|
268 | {
|
---|
269 | if (!ParserPresent[tremolo])
|
---|
270 | addTremolo();
|
---|
271 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Returns reference to the output XyzParser, adds if not present.
|
---|
275 | * \return reference to the output XyzParser
|
---|
276 | */
|
---|
277 | XyzParser &FormatParserStorage::getXyz()
|
---|
278 | {
|
---|
279 | if (!ParserPresent[xyz])
|
---|
280 | addXyz();
|
---|
281 | return dynamic_cast<XyzParser &>(*ParserList[xyz]);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 |
|
---|
286 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|