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