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