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