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