| 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 | const std::string FormatParserStorage::unknownTypeString("unknown");
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /** Constructor of class FormatParserStorage.
 | 
|---|
| 46 |  */
 | 
|---|
| 47 | FormatParserStorage::FormatParserStorage()
 | 
|---|
| 48 | {
 | 
|---|
| 49 |   ParserList.resize(ParserTypes_end, NULL);
 | 
|---|
| 50 |   ParserStream.resize(ParserTypes_end, NULL);
 | 
|---|
| 51 |   ParserPresent.resize(ParserTypes_end, false);
 | 
|---|
| 52 |   ParserDesiredOutputFormat.resize(ParserTypes_end, false);
 | 
|---|
| 53 | 
 | 
|---|
| 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 |      ) );
 | 
|---|
| 63 |   
 | 
|---|
| 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 | 
 | 
|---|
| 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)
 | 
|---|
| 125 |     if (ParserPresent[iter]) {
 | 
|---|
| 126 |       if (ParserStream[iter] != NULL) {
 | 
|---|
| 127 |         if (ParserStream[iter]->is_open())
 | 
|---|
| 128 |           ParserStream[iter]->close();
 | 
|---|
| 129 |         delete ParserStream[iter];
 | 
|---|
| 130 |       }
 | 
|---|
| 131 |       delete ParserList[iter];
 | 
|---|
| 132 |     }
 | 
|---|
| 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 | 
 | 
|---|
| 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 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | /** Saves the world in the desired output formats.
 | 
|---|
| 166 |  *
 | 
|---|
| 167 |  */
 | 
|---|
| 168 | void FormatParserStorage::SaveAll()
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   std::string filename;
 | 
|---|
| 171 |   for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
 | 
|---|
| 172 |     if (ParserPresent[iter] && ParserDesiredOutputFormat[iter]) {
 | 
|---|
| 173 |       filename = prefix;
 | 
|---|
| 174 |       filename += ".";
 | 
|---|
| 175 |       filename += ParserSuffixes[iter];
 | 
|---|
| 176 |       ParserStream[iter] = new std::ofstream(filename.c_str());
 | 
|---|
| 177 |       ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
 | 
|---|
| 178 |     }
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | 
 | 
|---|
| 182 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
 | 
|---|
| 185 |     DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
 | 
|---|
| 186 |     return ParserTypes_end;
 | 
|---|
| 187 |   } else
 | 
|---|
| 188 |     return ParserLookupNames[type];
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
 | 
|---|
| 192 | {
 | 
|---|
| 193 |   if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
 | 
|---|
| 194 |     DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
 | 
|---|
| 195 |     return ParserTypes_end;
 | 
|---|
| 196 |   } else
 | 
|---|
| 197 |     return ParserLookupSuffixes[type];
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | const std::string &FormatParserStorage::getNameFromType(ParserTypes type)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   if (ParserNames.find(type) == ParserNames.end()) {
 | 
|---|
| 203 |     DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
 | 
|---|
| 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()) {
 | 
|---|
| 212 |     DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
 | 
|---|
| 213 |     return unknownTypeString;
 | 
|---|
| 214 |   } else
 | 
|---|
| 215 |     return ParserSuffixes[type];
 | 
|---|
| 216 | }
 | 
|---|
| 217 | 
 | 
|---|
| 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 {
 | 
|---|
| 225 |       ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
 | 
|---|
| 226 |       return false;
 | 
|---|
| 227 |     }
 | 
|---|
| 228 |   } else {
 | 
|---|
| 229 |     return false;
 | 
|---|
| 230 |   }
 | 
|---|
| 231 | }
 | 
|---|
| 232 | 
 | 
|---|
| 233 | bool FormatParserStorage::add(std::string type)
 | 
|---|
| 234 | {
 | 
|---|
| 235 |   enum ParserTypes Ptype = getTypeFromName(type);
 | 
|---|
| 236 |   return add(Ptype);
 | 
|---|
| 237 | }
 | 
|---|
| 238 | 
 | 
|---|
| 239 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 245 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
 | 
|---|
| 246 | {
 | 
|---|
| 247 |   enum ParserTypes type = getTypeFromSuffix(suffix);
 | 
|---|
| 248 |   if (type != ParserTypes_end)
 | 
|---|
| 249 |     get(type).load(&input);
 | 
|---|
| 250 |   else
 | 
|---|
| 251 |     return false;
 | 
|---|
| 252 |   return true;
 | 
|---|
| 253 | }
 | 
|---|
| 254 | 
 | 
|---|
| 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.
 | 
|---|
| 269 |  * \param &output output stream
 | 
|---|
| 270 |  * \param suffix
 | 
|---|
| 271 |  * \return true - storing ok, false - suffix unknown
 | 
|---|
| 272 |  */
 | 
|---|
| 273 | bool FormatParserStorage::saveSelectedMolecules(std::ostream &output, std::string suffix)
 | 
|---|
| 274 | {
 | 
|---|
| 275 |   std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 276 |   std::map<size_t, atom *> IdAtoms;
 | 
|---|
| 277 |   for (std::vector<molecule *>::const_iterator MolIter = molecules.begin();
 | 
|---|
| 278 |       MolIter != molecules.end();
 | 
|---|
| 279 |       ++MolIter) {
 | 
|---|
| 280 |     for(molecule::atomSet::const_iterator AtomIter = (*MolIter)->begin();
 | 
|---|
| 281 |         AtomIter != (*MolIter)->end();
 | 
|---|
| 282 |         ++AtomIter) {
 | 
|---|
| 283 |       IdAtoms.insert( make_pair((*AtomIter)->getId(), (*AtomIter)) );
 | 
|---|
| 284 |     }
 | 
|---|
| 285 |   }
 | 
|---|
| 286 |   std::vector<atom *> atoms;
 | 
|---|
| 287 |   atoms.reserve(IdAtoms.size());
 | 
|---|
| 288 |   for (std::map<size_t, atom *>::const_iterator iter = IdAtoms.begin();
 | 
|---|
| 289 |       iter != IdAtoms.end();
 | 
|---|
| 290 |       ++iter) {
 | 
|---|
| 291 |     atoms.push_back(iter->second);
 | 
|---|
| 292 |   }
 | 
|---|
| 293 |   return save(output, suffix, atoms);
 | 
|---|
| 294 | }
 | 
|---|
| 295 | 
 | 
|---|
| 296 | /** Stores world in an ostream depending on its suffix
 | 
|---|
| 297 |  * \param &output output stream
 | 
|---|
| 298 |  * \param suffix
 | 
|---|
| 299 |  * \return true - storing ok, false - suffix unknown
 | 
|---|
| 300 |  */
 | 
|---|
| 301 | bool FormatParserStorage::saveWorld(std::ostream &output, std::string suffix)
 | 
|---|
| 302 | {
 | 
|---|
| 303 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 304 |   return save(output, suffix, atoms);
 | 
|---|
| 305 | }
 | 
|---|
| 306 | 
 | 
|---|
| 307 | /** Stores a given vector of \a atoms in an ostream depending on its suffix
 | 
|---|
| 308 |  * \param &output output stream
 | 
|---|
| 309 |  * \param suffix
 | 
|---|
| 310 |  * \return true - storing ok, false - suffix unknown
 | 
|---|
| 311 |  */
 | 
|---|
| 312 | bool FormatParserStorage::save(std::ostream &output, std::string suffix, const std::vector<atom *> &atoms)
 | 
|---|
| 313 | {
 | 
|---|
| 314 |   enum ParserTypes type = getTypeFromSuffix(suffix);
 | 
|---|
| 315 |   if (type != ParserTypes_end)
 | 
|---|
| 316 |     get(type).save(&output, atoms);
 | 
|---|
| 317 |   else
 | 
|---|
| 318 |     return false;
 | 
|---|
| 319 |   return true;
 | 
|---|
| 320 | }
 | 
|---|
| 321 | 
 | 
|---|
| 322 | /** Returns reference to the desired output parser as FormatParser, adds if not present.
 | 
|---|
| 323 |  * \param _type type of desired parser
 | 
|---|
| 324 |  * \return reference to the output FormatParser with desired type
 | 
|---|
| 325 |  */
 | 
|---|
| 326 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
 | 
|---|
| 327 | {
 | 
|---|
| 328 |   if (!ParserPresent[_type]) {
 | 
|---|
| 329 |     add(_type);
 | 
|---|
| 330 |   }
 | 
|---|
| 331 |   return *ParserList[_type];
 | 
|---|
| 332 | }
 | 
|---|
| 333 | 
 | 
|---|
| 334 | CONSTRUCT_SINGLETON(FormatParserStorage)
 | 
|---|