| 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 "TremoloParser.hpp"
 | 
|---|
| 38 | #include "XyzParser.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /** Constructor of class FormatParserStorage.
 | 
|---|
| 44 |  */
 | 
|---|
| 45 | FormatParserStorage::FormatParserStorage()
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   ParserList.resize(ParserTypes_end, NULL);
 | 
|---|
| 48 |   ParserStream.resize(ParserTypes_end, NULL);
 | 
|---|
| 49 |   ParserPresent.resize(ParserTypes_end, false);
 | 
|---|
| 50 | 
 | 
|---|
| 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 |      ) );
 | 
|---|
| 60 |   
 | 
|---|
| 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 | 
 | 
|---|
| 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)
 | 
|---|
| 122 |     if (ParserPresent[iter]) {
 | 
|---|
| 123 |       if (ParserStream[iter]->is_open())
 | 
|---|
| 124 |         ParserStream[iter]->close();
 | 
|---|
| 125 |       delete ParserStream[iter];
 | 
|---|
| 126 |       delete ParserList[iter];
 | 
|---|
| 127 |     }
 | 
|---|
| 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 += ".";
 | 
|---|
| 146 |       filename += ParserSuffixes[iter];
 | 
|---|
| 147 |       ParserStream[iter] = new std::ofstream(filename.c_str());
 | 
|---|
| 148 |       ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
 | 
|---|
| 149 |     }
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 171 | bool FormatParserStorage::add(ParserTypes ptype)
 | 
|---|
| 172 | {
 | 
|---|
| 173 |   if (ptype != ParserTypes_end) {
 | 
|---|
| 174 |     if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
 | 
|---|
| 175 |       LOG(0, "STATUS: Adding " << ParserNames[ptype] << " type to output.");
 | 
|---|
| 176 |       (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
 | 
|---|
| 177 |       return true;
 | 
|---|
| 178 |     } else {
 | 
|---|
| 179 |       ELOG(1, "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?");
 | 
|---|
| 180 |       return false;
 | 
|---|
| 181 |     }
 | 
|---|
| 182 |   } else {
 | 
|---|
| 183 |     return false;
 | 
|---|
| 184 |   }
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | bool FormatParserStorage::add(std::string type)
 | 
|---|
| 188 | {
 | 
|---|
| 189 |   enum ParserTypes Ptype = getTypeFromName(type);
 | 
|---|
| 190 |   return add(Ptype);
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 199 | bool FormatParserStorage::load(std::istream &input, std::string suffix)
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   enum ParserTypes type = getTypeFromSuffix(suffix);
 | 
|---|
| 202 |   if (type != ParserTypes_end)
 | 
|---|
| 203 |     get(type).load(&input);
 | 
|---|
| 204 |   else
 | 
|---|
| 205 |     return false;
 | 
|---|
| 206 |   return true;
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 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 | }
 | 
|---|
| 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 |  */
 | 
|---|
| 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)
 | 
|---|
| 267 | {
 | 
|---|
| 268 |   enum ParserTypes type = getTypeFromSuffix(suffix);
 | 
|---|
| 269 |   if (type != ParserTypes_end)
 | 
|---|
| 270 |     get(type).save(&output, atoms);
 | 
|---|
| 271 |   else
 | 
|---|
| 272 |     return false;
 | 
|---|
| 273 |   return true;
 | 
|---|
| 274 | }
 | 
|---|
| 275 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 280 | FormatParserInterface &FormatParserStorage::get(ParserTypes _type)
 | 
|---|
| 281 | {
 | 
|---|
| 282 |   if (!ParserPresent[_type]) {
 | 
|---|
| 283 |     add(_type);
 | 
|---|
| 284 |   }
 | 
|---|
| 285 |   return *ParserList[_type];
 | 
|---|
| 286 | }
 | 
|---|
| 287 | 
 | 
|---|
| 288 | CONSTRUCT_SINGLETON(FormatParserStorage)
 | 
|---|