[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]) {
|
---|
[091838] | 142 | if (ParserStream[iter] != NULL) {
|
---|
| 143 | if (ParserStream[iter]->is_open())
|
---|
| 144 | ParserStream[iter]->close();
|
---|
| 145 | delete ParserStream[iter];
|
---|
| 146 | }
|
---|
[52baf9] | 147 | delete ParserList[iter];
|
---|
[60239f] | 148 | }
|
---|
[52baf9] | 149 | }
|
---|
| 150 |
|
---|
[59ec04] | 151 |
|
---|
| 152 | /** Tests whether a file and parsers are selected for saving.
|
---|
| 153 | */
|
---|
| 154 | bool FormatParserStorage::isAbleToSave()
|
---|
| 155 | {
|
---|
| 156 | if (prefix.empty())
|
---|
| 157 | return false;
|
---|
| 158 |
|
---|
| 159 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
| 160 | if (ParserPresent[iter] && ParserDesiredOutputFormat[iter])
|
---|
| 161 | return true;
|
---|
| 162 | return false;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[52baf9] | 165 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
| 166 | * \param &prefix prefix to use.
|
---|
| 167 | */
|
---|
| 168 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
| 169 | {
|
---|
| 170 | prefix=_prefix;
|
---|
| 171 | };
|
---|
| 172 |
|
---|
[091838] | 173 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
| 174 | *
|
---|
| 175 | * @param type type to add to desired output formats
|
---|
| 176 | */
|
---|
| 177 | void FormatParserStorage::setOutputFormat(ParserTypes type)
|
---|
| 178 | {
|
---|
| 179 | LOG(0, "STATUS: Adding " << ParserNames[type] << " type to output.");
|
---|
| 180 | ParserDesiredOutputFormat[type] = true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /** Sets \a type as a format to be stored on call of SaveAll.
|
---|
| 184 | *
|
---|
| 185 | * @param type type to add to desired output formats
|
---|
| 186 | */
|
---|
| 187 | void FormatParserStorage::setOutputFormat(std::string type)
|
---|
| 188 | {
|
---|
| 189 | std::map<std::string, ParserTypes>::const_iterator iter = ParserLookupNames.find(type);
|
---|
| 190 | ASSERT(iter != ParserLookupNames.end(),
|
---|
| 191 | "FormatParserStorage::setOutputFormat() - output format "+type+" is unknown.");
|
---|
| 192 | setOutputFormat(iter->second);
|
---|
| 193 | }
|
---|
[52baf9] | 194 |
|
---|
[091838] | 195 | /** Saves the world in the desired output formats.
|
---|
| 196 | *
|
---|
| 197 | */
|
---|
[52baf9] | 198 | void FormatParserStorage::SaveAll()
|
---|
| 199 | {
|
---|
| 200 | std::string filename;
|
---|
| 201 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
[091838] | 202 | if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
|
---|
[52baf9] | 203 | filename = prefix;
|
---|
| 204 | filename += ".";
|
---|
[a42054] | 205 | filename += ParserSuffixes[iter];
|
---|
[60239f] | 206 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
| 207 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
[52baf9] | 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 |
|
---|
[a42054] | 212 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 213 | {
|
---|
| 214 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
[47d041] | 215 | ELOG(1, "Unknown type " << type << ".");
|
---|
[5c6946] | 216 | return ParserTypes_end;
|
---|
| 217 | } else
|
---|
| 218 | return ParserLookupNames[type];
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[a42054] | 221 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
| 222 | {
|
---|
| 223 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
[47d041] | 224 | ELOG(1, "Unknown type " << type << ".");
|
---|
[a42054] | 225 | return ParserTypes_end;
|
---|
| 226 | } else
|
---|
| 227 | return ParserLookupSuffixes[type];
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[babcc1] | 230 | const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
|
---|
| 231 | {
|
---|
| 232 | if (ParserNames.find(type) == ParserNames.end()) {
|
---|
[47d041] | 233 | ELOG(1, "Unknown type " << type << ".");
|
---|
[babcc1] | 234 | return unknownTypeString;
|
---|
| 235 | } else
|
---|
| 236 | return ParserNames[type];
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | const std::string &FormatParserStorage::getSuffixFromType(ParserTypes type)
|
---|
| 240 | {
|
---|
| 241 | if (ParserSuffixes.find(type) == ParserSuffixes.end()) {
|
---|
[47d041] | 242 | ELOG(1, "Unknown type " << type << ".");
|
---|
[babcc1] | 243 | return unknownTypeString;
|
---|
| 244 | } else
|
---|
| 245 | return ParserSuffixes[type];
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[5c6946] | 248 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
| 249 | {
|
---|
| 250 | if (ptype != ParserTypes_end) {
|
---|
| 251 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
| 252 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
| 253 | return true;
|
---|
| 254 | } else {
|
---|
[765f16] | 255 | ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
|
---|
[5c6946] | 256 | return false;
|
---|
| 257 | }
|
---|
| 258 | } else {
|
---|
| 259 | return false;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | bool FormatParserStorage::add(std::string type)
|
---|
| 264 | {
|
---|
[765f16] | 265 | enum ParserTypes Ptype = getTypeFromName(type);
|
---|
| 266 | return add(Ptype);
|
---|
[5c6946] | 267 | }
|
---|
| 268 |
|
---|
[98c428] | 269 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
| 270 | * \param filename path and filename
|
---|
| 271 | * \return true - parsing ok, false - suffix unknown
|
---|
| 272 | */
|
---|
| 273 | bool FormatParserStorage::load(boost::filesystem::path filename)
|
---|
| 274 | {
|
---|
| 275 | return load(filename.string());
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /** Recognizes type of file and parse via FormatParserStorage::load().
|
---|
| 279 | * \param filename path and filename
|
---|
| 280 | * \return true - parsing ok, false - suffix unknown
|
---|
| 281 | */
|
---|
| 282 | bool FormatParserStorage::load(std::string &filename)
|
---|
| 283 | {
|
---|
| 284 | std::string FilenameSuffix = filename.substr(filename.find_last_of('.')+1, filename.length());
|
---|
| 285 | ifstream input;
|
---|
| 286 | LOG(0, "STATUS: Loading filler molecule " << filename
|
---|
| 287 | << " of suffix " << FilenameSuffix << ".");
|
---|
| 288 | input.open(filename.c_str());
|
---|
| 289 | const bool status = load(input, FilenameSuffix);
|
---|
| 290 | input.close();
|
---|
| 291 |
|
---|
| 292 | return status;
|
---|
| 293 | }
|
---|
[5c6946] | 294 |
|
---|
[86cff86] | 295 | /** Parses an istream depending on its suffix
|
---|
| 296 | * \param &input input stream
|
---|
| 297 | * \param suffix
|
---|
| 298 | * \return true - parsing ok, false - suffix unknown
|
---|
| 299 | */
|
---|
[98c428] | 300 | bool FormatParserStorage::load(std::istream &input, std::string &suffix)
|
---|
[86cff86] | 301 | {
|
---|
[6d0f058] | 302 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 303 | if (type != ParserTypes_end)
|
---|
| 304 | get(type).load(&input);
|
---|
| 305 | else
|
---|
[86cff86] | 306 | return false;
|
---|
| 307 | return true;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[73916f] | 310 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 311 | * \param &output output stream
|
---|
| 312 | * \param suffix
|
---|
| 313 | * \return true - storing ok, false - suffix unknown
|
---|
| 314 | */
|
---|
| 315 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
| 316 | {
|
---|
| 317 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
| 318 | return save(output, suffix, atoms);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 322 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
| 323 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
[59fff1] | 324 | *
|
---|
| 325 | * TODO: Change here atom * to const atom * when FormatParserStorage::save() uses vector<const atom *>
|
---|
| 326 | *
|
---|
[73916f] | 327 | * \param &output output stream
|
---|
| 328 | * \param suffix
|
---|
| 329 | * \return true - storing ok, false - suffix unknown
|
---|
| 330 | */
|
---|
| 331 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
| 332 | {
|
---|
| 333 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
| 334 | std::map<size_t, atom *> IdAtoms;
|
---|
| 335 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
| 336 | MolIter != molecules.end();
|
---|
| 337 | ++MolIter) {
|
---|
[59fff1] | 338 | for(molecule::iterator AtomIter = (*MolIter)->begin();
|
---|
[73916f] | 339 | AtomIter != (*MolIter)->end();
|
---|
| 340 | ++AtomIter) {
|
---|
| 341 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | std::vector<atom *> atoms;
|
---|
| 345 | atoms.reserve(IdAtoms.size());
|
---|
| 346 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
| 347 | iter != IdAtoms.end();
|
---|
| 348 | ++iter) {
|
---|
| 349 | atoms.push_back(iter->second);
|
---|
| 350 | }
|
---|
| 351 | return save(output, suffix, atoms);
|
---|
| 352 | }
|
---|
[cabb46] | 353 |
|
---|
| 354 | /** Stores world in an ostream depending on its suffix
|
---|
| 355 | * \param &output output stream
|
---|
| 356 | * \param suffix
|
---|
| 357 | * \return true - storing ok, false - suffix unknown
|
---|
| 358 | */
|
---|
[73916f] | 359 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
| 360 | {
|
---|
| 361 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
| 362 | return save(output, suffix, atoms);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
| 366 | * \param &output output stream
|
---|
| 367 | * \param suffix
|
---|
| 368 | * \return true - storing ok, false - suffix unknown
|
---|
| 369 | */
|
---|
| 370 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
[cabb46] | 371 | {
|
---|
[6d0f058] | 372 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 373 | if (type != ParserTypes_end)
|
---|
| 374 | get(type).save(&output, atoms);
|
---|
| 375 | else
|
---|
[cabb46] | 376 | return false;
|
---|
| 377 | return true;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[7a51be] | 380 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
|
---|
| 381 | * \param _type type of desired parser
|
---|
| 382 | * \return reference to the output FormatParser with desired type
|
---|
| 383 | */
|
---|
[765f16] | 384 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
|
---|
[7a51be] | 385 | {
|
---|
| 386 | if (!ParserPresent[_type]) {
|
---|
| 387 | add(_type);
|
---|
| 388 | }
|
---|
| 389 | return *ParserList[_type];
|
---|
| 390 | }
|
---|
[52baf9] | 391 |
|
---|
| 392 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|