[bcf653] | 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 |
|
---|
[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 |
|
---|
[dc0d21] | 43 |
|
---|
[52baf9] | 44 | /** Constructor of class FormatParserStorage.
|
---|
| 45 | */
|
---|
| 46 | FormatParserStorage::FormatParserStorage()
|
---|
| 47 | {
|
---|
| 48 | ParserList.resize(ParserTypes_end, NULL);
|
---|
[60239f] | 49 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
[52baf9] | 50 | ParserPresent.resize(ParserTypes_end, false);
|
---|
| 51 |
|
---|
[765f16] | 52 | #include "ParserTypes.def"
|
---|
| 53 |
|
---|
| 54 | #define insert_print(z,n,seq,map, before, after) \
|
---|
| 55 | map .insert( std::make_pair( \
|
---|
| 56 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 57 | , before < \
|
---|
| 58 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 59 | > after \
|
---|
| 60 | ) );
|
---|
[db67ea] | 61 |
|
---|
[765f16] | 62 | #define insert_invert_print(z,n,seq,map, before, after) \
|
---|
| 63 | map .insert( std::make_pair( before < \
|
---|
| 64 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 65 | > after, \
|
---|
| 66 | BOOST_PP_SEQ_ELEM(n, seq) \
|
---|
| 67 | ) );
|
---|
| 68 |
|
---|
| 69 | // fill ParserNames
|
---|
| 70 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 71 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserNames, FormatParserTrait, ::name)
|
---|
| 72 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 73 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 74 | #endif
|
---|
| 75 |
|
---|
| 76 | // fill ParserLookupNames
|
---|
| 77 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 78 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupNames, FormatParserTrait, ::name)
|
---|
| 79 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 80 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 83 | // fill ParserSuffixes
|
---|
| 84 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 85 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserSuffixes, FormatParserTrait, ::suffix)
|
---|
| 86 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 87 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 88 | #endif
|
---|
| 89 |
|
---|
| 90 | // fill ParserLookupSuffixes
|
---|
| 91 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 92 | #define BOOST_PP_LOCAL_MACRO(n) insert_invert_print(~, n, PARSERSEQUENCE, ParserLookupSuffixes, FormatParserTrait, ::suffix)
|
---|
| 93 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 94 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 95 | #endif
|
---|
| 96 |
|
---|
| 97 | // fill ParserAddFunction
|
---|
| 98 | #if defined ParserTypes_END // do we have parameters at all?
|
---|
| 99 | #define BOOST_PP_LOCAL_MACRO(n) insert_print(~, n, PARSERSEQUENCE, ParserAddFunction, &FormatParserStorage::addParser, )
|
---|
| 100 | #define BOOST_PP_LOCAL_LIMITS (0, ParserTypes_END-1)
|
---|
| 101 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 102 | #endif
|
---|
| 103 |
|
---|
| 104 | #undef insert_print
|
---|
| 105 | #undef insert_invert_print
|
---|
| 106 | #include "ParserTypes.undef"
|
---|
| 107 |
|
---|
| 108 | //std::cout << "ParserNames:" << std::endl << ParserNames << std::endl;
|
---|
| 109 | //std::cout << "ParserSuffixes:" << std::endl << ParserSuffixes << std::endl;
|
---|
| 110 | //std::cout << "ParserLookupNames:" << std::endl << ParserLookupNames << std::endl;
|
---|
| 111 | //std::cout << "ParserLookupSuffixes:" << std::endl << ParserLookupSuffixes << std::endl;
|
---|
| 112 | //std::cout << "ParserAddFunction:" << std::endl << ParserAddFunction << std::endl;
|
---|
| 113 |
|
---|
[52baf9] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Destructor of class FormatParserStorage.
|
---|
| 117 | * Free all stored FormatParsers.
|
---|
| 118 | * Save on Exit.
|
---|
| 119 | */
|
---|
| 120 | FormatParserStorage::~FormatParserStorage()
|
---|
| 121 | {
|
---|
| 122 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
[60239f] | 123 | if (ParserPresent[iter]) {
|
---|
| 124 | if (ParserStream[iter]->is_open())
|
---|
| 125 | ParserStream[iter]->close();
|
---|
| 126 | delete ParserStream[iter];
|
---|
[52baf9] | 127 | delete ParserList[iter];
|
---|
[60239f] | 128 | }
|
---|
[52baf9] | 129 | }
|
---|
| 130 |
|
---|
| 131 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
| 132 | * \param &prefix prefix to use.
|
---|
| 133 | */
|
---|
| 134 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
| 135 | {
|
---|
| 136 | prefix=_prefix;
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | void FormatParserStorage::SaveAll()
|
---|
| 141 | {
|
---|
| 142 | std::string filename;
|
---|
| 143 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
| 144 | if (ParserPresent[iter]) {
|
---|
| 145 | filename = prefix;
|
---|
| 146 | filename += ".";
|
---|
[a42054] | 147 | filename += ParserSuffixes[iter];
|
---|
[60239f] | 148 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
| 149 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
[52baf9] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
[a42054] | 154 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 155 | {
|
---|
| 156 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
| 157 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
| 158 | return ParserTypes_end;
|
---|
| 159 | } else
|
---|
| 160 | return ParserLookupNames[type];
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[a42054] | 163 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
| 164 | {
|
---|
| 165 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
| 166 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
| 167 | return ParserTypes_end;
|
---|
| 168 | } else
|
---|
| 169 | return ParserLookupSuffixes[type];
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[5c6946] | 172 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
| 173 | {
|
---|
| 174 | if (ptype != ParserTypes_end) {
|
---|
| 175 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
[765f16] | 176 | LOG(0, "STATUS: Adding " << ParserNames[ptype] << " type to output.");
|
---|
[5c6946] | 177 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
| 178 | return true;
|
---|
| 179 | } else {
|
---|
[765f16] | 180 | ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
|
---|
[5c6946] | 181 | return false;
|
---|
| 182 | }
|
---|
| 183 | } else {
|
---|
| 184 | return false;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | bool FormatParserStorage::add(std::string type)
|
---|
| 189 | {
|
---|
[765f16] | 190 | enum ParserTypes Ptype = getTypeFromName(type);
|
---|
| 191 | return add(Ptype);
|
---|
[5c6946] | 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
[86cff86] | 195 | /** Parses an istream depending on its suffix
|
---|
| 196 | * \param &input input stream
|
---|
| 197 | * \param suffix
|
---|
| 198 | * \return true - parsing ok, false - suffix unknown
|
---|
| 199 | */
|
---|
[73916f] | 200 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
|
---|
[86cff86] | 201 | {
|
---|
[6d0f058] | 202 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 203 | if (type != ParserTypes_end)
|
---|
| 204 | get(type).load(&input);
|
---|
| 205 | else
|
---|
[86cff86] | 206 | return false;
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[73916f] | 210 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 211 | * \param &output output stream
|
---|
| 212 | * \param suffix
|
---|
| 213 | * \return true - storing ok, false - suffix unknown
|
---|
| 214 | */
|
---|
| 215 | bool FormatParserStorage::saveSelectedAtoms(std::ostream &output, std::string suffix)
|
---|
| 216 | {
|
---|
| 217 | std::vector<atom *> atoms = World::getInstance().getSelectedAtoms();
|
---|
| 218 | return save(output, suffix, atoms);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /** Stores all selected atoms in an ostream depending on its suffix
|
---|
| 222 | * We store in the order of the atomic ids, not in the order they appear in the molecules.
|
---|
| 223 | * Hence, we first create a vector from all selected molecules' atoms.
|
---|
| 224 | * \param &output output stream
|
---|
| 225 | * \param suffix
|
---|
| 226 | * \return true - storing ok, false - suffix unknown
|
---|
| 227 | */
|
---|
| 228 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
|
---|
| 229 | {
|
---|
| 230 | std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
| 231 | std::map<size_t, atom *> IdAtoms;
|
---|
| 232 | for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
|
---|
| 233 | MolIter != molecules.end();
|
---|
| 234 | ++MolIter) {
|
---|
| 235 | for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
|
---|
| 236 | AtomIter != (*MolIter)->end();
|
---|
| 237 | ++AtomIter) {
|
---|
| 238 | IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | std::vector<atom *> atoms;
|
---|
| 242 | atoms.reserve(IdAtoms.size());
|
---|
| 243 | for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
|
---|
| 244 | iter != IdAtoms.end();
|
---|
| 245 | ++iter) {
|
---|
| 246 | atoms.push_back(iter->second);
|
---|
| 247 | }
|
---|
| 248 | return save(output, suffix, atoms);
|
---|
| 249 | }
|
---|
[cabb46] | 250 |
|
---|
| 251 | /** Stores world in an ostream depending on its suffix
|
---|
| 252 | * \param &output output stream
|
---|
| 253 | * \param suffix
|
---|
| 254 | * \return true - storing ok, false - suffix unknown
|
---|
| 255 | */
|
---|
[73916f] | 256 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
|
---|
| 257 | {
|
---|
| 258 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
| 259 | return save(output, suffix, atoms);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
|
---|
| 263 | * \param &output output stream
|
---|
| 264 | * \param suffix
|
---|
| 265 | * \return true - storing ok, false - suffix unknown
|
---|
| 266 | */
|
---|
| 267 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
|
---|
[cabb46] | 268 | {
|
---|
[6d0f058] | 269 | enum ParserTypes type = getTypeFromSuffix(suffix);
|
---|
| 270 | if (type != ParserTypes_end)
|
---|
| 271 | get(type).save(&output, atoms);
|
---|
| 272 | else
|
---|
[cabb46] | 273 | return false;
|
---|
| 274 | return true;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[7a51be] | 277 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
|
---|
| 278 | * \param _type type of desired parser
|
---|
| 279 | * \return reference to the output FormatParser with desired type
|
---|
| 280 | */
|
---|
[765f16] | 281 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
|
---|
[7a51be] | 282 | {
|
---|
| 283 | if (!ParserPresent[_type]) {
|
---|
| 284 | add(_type);
|
---|
| 285 | }
|
---|
| 286 | return *ParserList[_type];
|
---|
| 287 | }
|
---|
[52baf9] | 288 |
|
---|
| 289 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|