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