1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 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 "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 | #include <fstream>
|
---|
24 |
|
---|
25 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
26 |
|
---|
27 | #include "CodePatterns/Assert.hpp"
|
---|
28 | #include "CodePatterns/Log.hpp"
|
---|
29 |
|
---|
30 | #include "molecule.hpp"
|
---|
31 | #include "FormatParserStorage.hpp"
|
---|
32 | #include "ParserTypes.hpp"
|
---|
33 |
|
---|
34 | #include "MpqcParser.hpp"
|
---|
35 | #include "PcpParser.hpp"
|
---|
36 | #include "PdbParser.hpp"
|
---|
37 | #include "Psi3Parser.hpp"
|
---|
38 | #include "TremoloParser.hpp"
|
---|
39 | #include "XmlParser.hpp"
|
---|
40 | #include "XyzParser.hpp"
|
---|
41 |
|
---|
42 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
43 |
|
---|
44 | const std::string FormatParserStorage::unknownTypeString("unknown");
|
---|
45 |
|
---|
46 | /** Constructor of class FormatParserStorage.
|
---|
47 | */
|
---|
48 | FormatParserStorage::FormatParserStorage()
|
---|
49 | {
|
---|
50 | ParserList.resize(ParserTypes_end, NULL);
|
---|
51 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
52 | ParserPresent.resize(ParserTypes_end, false);
|
---|
53 | ParserDesiredOutputFormat.resize(ParserTypes_end, false);
|
---|
54 |
|
---|
55 | #include "ParserTypes.def"
|
---|
56 |
|
---|
57 | #define insert_print(z,n,seq,map, before, after) \
|
---|
58 | map .insert( std::make_pair( \
|
---|
59 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
60 | , before < \
|
---|
61 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
62 | > after \
|
---|
63 | ) );
|
---|
64 |
|
---|
65 | #define insert_invert_print(z,n,seq,map, before, after) \
|
---|
66 | map .insert( std::make_pair( before < \
|
---|
67 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
68 | > after, \
|
---|
69 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
70 | ) );
|
---|
71 |
|
---|
72 | // fill ParserNames
|
---|
73 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
74 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserNames, FormatParserTrait, ::name)
|
---|
75 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
76 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | // fill ParserLookupNames
|
---|
80 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
81 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupNames, FormatParserTrait, ::name)
|
---|
82 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
83 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | // fill ParserSuffixes
|
---|
87 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
88 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserSuffixes, FormatParserTrait, ::suffix)
|
---|
89 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
90 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | // fill ParserLookupSuffixes
|
---|
94 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
95 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupSuffixes, FormatParserTrait, ::suffix)
|
---|
96 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
97 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | // fill ParserAddFunction
|
---|
101 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
102 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserAddFunction, &FormatParserStorage::addParser, )
|
---|
103 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
104 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | #undef insert_print
|
---|
108 | #undef insert_invert_print
|
---|
109 | #include "ParserTypes.undef"
|
---|
110 |
|
---|
111 | //std::cout << "ParserNames:" << std::endl << ParserNames << std::endl;
|
---|
112 | //std::cout << "ParserSuffixes:" << std::endl << ParserSuffixes << std::endl;
|
---|
113 | //std::cout << "ParserLookupNames:" << std::endl << ParserLookupNames << std::endl;
|
---|
114 | //std::cout << "ParserLookupSuffixes:" << std::endl << ParserLookupSuffixes << std::endl;
|
---|
115 | //std::cout << "ParserAddFunction:" << std::endl << ParserAddFunction << std::endl;
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Destructor of class FormatParserStorage.
|
---|
120 | * Free all stored FormatParsers.
|
---|
121 | * Save on Exit.
|
---|
122 | */
|
---|
123 | FormatParserStorage::~FormatParserStorage()
|
---|
124 | {
|
---|
125 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
126 | if (ParserPresent[iter]) {
|
---|
127 | if (ParserStream[iter] != NULL) {
|
---|
128 | if (ParserStream[iter]->is_open())
|
---|
129 | ParserStream[iter]->close();
|
---|
130 | delete ParserStream[iter];
|
---|
131 | }
|
---|
132 | delete ParserList[iter];
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /** Tests whether a file and parsers are selected for saving.
|
---|
138 | */
|
---|
139 | bool FormatParserStorage::isAbleToSave()
|
---|
140 | {
|
---|
141 | if (prefix.empty())
|
---|
142 | return false;
|
---|
143 |
|
---|
144 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
145 | if (ParserPresent[iter] && ParserDesiredOutputFormat[iter])
|
---|
146 | return true;
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
151 | * \param &prefix prefix to use.
|
---|
152 | */
|
---|
153 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
154 | {
|
---|
155 | prefix=_prefix;
|
---|
156 | };
|
---|
157 |
|
---|
158 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
159 | *
|
---|
160 | * @param type type to add to desired output formats
|
---|
161 | */
|
---|
162 | void FormatParserStorage::setOutputFormat(ParserTypes type)
|
---|
163 | {
|
---|
164 | LOG(0, "STATUS: Adding " << ParserNames[type] << " type to output.");
|
---|
165 | ParserDesiredOutputFormat[type] = true;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
169 | *
|
---|
170 | * @param type type to add to desired output formats
|
---|
171 | */
|
---|
172 | void FormatParserStorage::setOutputFormat(std::string type)
|
---|
173 | {
|
---|
174 | std::map<std::string, ParserTypes>::const_iterator iter = ParserLookupNames.find(type);
|
---|
175 | ASSERT(iter != ParserLookupNames.end(),
|
---|
176 | "FormatParserStorage::setOutputFormat() - output format "+type+" is unknown.");
|
---|
177 | setOutputFormat(iter->second);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Saves the world in the desired output formats.
|
---|
181 | *
|
---|
182 | */
|
---|
183 | void FormatParserStorage::SaveAll()
|
---|
184 | {
|
---|
185 | std::string filename;
|
---|
186 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
187 | if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
|
---|
188 | filename = prefix;
|
---|
189 | filename += ".";
|
---|
190 | filename += ParserSuffixes[iter];
|
---|
191 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
192 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
198 | {
|
---|
199 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
200 | ELOG(1, "Unknown type " << type << ".");
|
---|
201 | return ParserTypes_end;
|
---|
202 | } else
|
---|
203 | return ParserLookupNames[type];
|
---|
204 | }
|
---|
205 |
|
---|
206 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
207 | {
|
---|
208 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
209 | ELOG(1, "Unknown type " << type << ".");
|
---|
210 | return ParserTypes_end;
|
---|
211 | } else
|
---|
212 | return ParserLookupSuffixes[type];
|
---|
213 | }
|
---|
214 |
|
---|
215 | const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
|
---|
216 | {
|
---|
217 | if (ParserNames.find(type) == ParserNames.end()) {
|
---|
218 | ELOG(1, "Unknown type " << type << ".");
|
---|
219 | return unknownTypeString;
|
---|
220 | } else
|
---|
221 | return ParserNames[type];
|
---|
222 | }
|
---|
223 |
|
---|
224 | const std::string &FormatParserStorage::getSuffixFromType(ParserTypes type)
|
---|
225 | {
|
---|
226 | if (ParserSuffixes.find(type) == ParserSuffixes.end()) {
|
---|
227 | ELOG(1, "Unknown type " << type << ".");
|
---|
228 | return unknownTypeString;
|
---|
229 | } else
|
---|
230 | return ParserSuffixes[type];
|
---|
231 | }
|
---|
232 |
|
---|
233 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
234 | {
|
---|
235 | if (ptype != ParserTypes_end) {
|
---|
236 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
237 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
238 | return true;
|
---|
239 | } else {
|
---|
240 | ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 | } else {
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | bool FormatParserStorage::add(std::string type)
|
---|
249 | {
|
---|
250 | enum ParserTypes Ptype = getTypeFromName(type);
|
---|
251 | return add(Ptype);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
255 | * \param filename path and filename
|
---|
256 | * \return true - parsing ok, false - suffix unknown
|
---|
257 | */
|
---|
258 | bool FormatParserStorage::load(boost::filesystem::path filename)
|
---|
259 | {
|
---|
260 | return load(filename.string());
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
264 | * \param filename path and filename
|
---|
265 | * \return true - parsing ok, false - suffix unknown
|
---|
266 | */
|
---|
267 | bool FormatParserStorage::load(std::string &filename)
|
---|
268 | {
|
---|
269 | std::string FilenameSuffix = filename.substr(filename.find_last_of('.')+1, filename.length());
|
---|
270 | ifstream input;
|
---|
271 | LOG(0, "STATUS: Loading filler molecule " << filename
|
---|
272 | << " of suffix " << FilenameSuffix << ".");
|
---|
273 | input.open(filename.c_str());
|
---|
274 | const bool status = load(input, FilenameSuffix);
|
---|
275 | input.close();
|
---|
276 |
|
---|
277 | return status;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** Parses an istream depending on its suffix
|
---|
281 | * \param &input input stream
|
---|
282 | * \param suffix
|
---|
283 | * \return true - parsing ok, false - suffix unknown
|
---|
284 | */
|
---|
285 | bool FormatParserStorage::load(std::istream &input, std::string &suffix)
|
---|
286 | {
|
---|
287 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
288 | if (type != ParserTypes_end)
|
---|
289 | get(type).load(&input);
|
---|
290 | else
|
---|
291 | return false;
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
296 | * \param &output output stream
|
---|
297 | * \param suffix
|
---|
298 | * \return true - storing ok, false - suffix unknown
|
---|
299 | */
|
---|
300 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
301 | {
|
---|
302 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
303 | return save(output, suffix, atoms);
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
307 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
308 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
309 | *
|
---|
310 | * TODO: Change here atom * to const atom * when FormatParserStorage::save() uses vector<const atom *>
|
---|
311 | *
|
---|
312 | * \param &output output stream
|
---|
313 | * \param suffix
|
---|
314 | * \return true - storing ok, false - suffix unknown
|
---|
315 | */
|
---|
316 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
317 | {
|
---|
318 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
319 | std::map<size_t, atom *> IdAtoms;
|
---|
320 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
321 | MolIter != molecules.end();
|
---|
322 | ++MolIter) {
|
---|
323 | for(molecule::iterator AtomIter = (*MolIter)->begin();
|
---|
324 | AtomIter != (*MolIter)->end();
|
---|
325 | ++AtomIter) {
|
---|
326 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
327 | }
|
---|
328 | }
|
---|
329 | std::vector<atom *> atoms;
|
---|
330 | atoms.reserve(IdAtoms.size());
|
---|
331 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
332 | iter != IdAtoms.end();
|
---|
333 | ++iter) {
|
---|
334 | atoms.push_back(iter->second);
|
---|
335 | }
|
---|
336 | return save(output, suffix, atoms);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Stores world in an ostream depending on its suffix
|
---|
340 | * \param &output output stream
|
---|
341 | * \param suffix
|
---|
342 | * \return true - storing ok, false - suffix unknown
|
---|
343 | */
|
---|
344 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
345 | {
|
---|
346 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
347 | return save(output, suffix, atoms);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
351 | * \param &output output stream
|
---|
352 | * \param suffix
|
---|
353 | * \return true - storing ok, false - suffix unknown
|
---|
354 | */
|
---|
355 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
356 | {
|
---|
357 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
358 | if (type != ParserTypes_end)
|
---|
359 | get(type).save(&output, atoms);
|
---|
360 | else
|
---|
361 | return false;
|
---|
362 | return true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
|
---|
366 | * \param _type type of desired parser
|
---|
367 | * \return reference to the output FormatParser with desired type
|
---|
368 | */
|
---|
369 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
|
---|
370 | {
|
---|
371 | if (!ParserPresent[_type]) {
|
---|
372 | add(_type);
|
---|
373 | }
|
---|
374 | return *ParserList[_type];
|
---|
375 | }
|
---|
376 |
|
---|
377 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|