| 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 "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <iostream>
 | 
|---|
| 23 | #include <fstream>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "Parser/FormatParser.hpp"
 | 
|---|
| 28 | #include "Parser/MpqcParser.hpp"
 | 
|---|
| 29 | #include "Parser/PcpParser.hpp"
 | 
|---|
| 30 | #include "Parser/TremoloParser.hpp"
 | 
|---|
| 31 | #include "Parser/XyzParser.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "Helpers/Log.hpp"
 | 
|---|
| 34 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "Helpers/Assert.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "Patterns/Singleton_impl.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /** Increment operator for the enumeration ParserTypes to allow loops.
 | 
|---|
| 41 |  * \param &type value
 | 
|---|
| 42 |  * \return value incremented by one
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | ParserTypes &operator++(ParserTypes &type)
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   return type = ParserTypes(type+1);
 | 
|---|
| 47 | }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /** Constructor of class FormatParserStorage.
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | FormatParserStorage::FormatParserStorage()
 | 
|---|
| 52 | {
 | 
|---|
| 53 |   ParserList.resize(ParserTypes_end, NULL);
 | 
|---|
| 54 |   ParserStream.resize(ParserTypes_end, NULL);
 | 
|---|
| 55 |   ParserPresent.resize(ParserTypes_end, false);
 | 
|---|
| 56 |   ParserSuffix.resize(ParserTypes_end, "");
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   ParserSuffix[mpqc] = "in";
 | 
|---|
| 59 |   ParserSuffix[pcp] = "conf";
 | 
|---|
| 60 |   ParserSuffix[tremolo] = "data";
 | 
|---|
| 61 |   ParserSuffix[xyz] = "xyz";
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | /** Destructor of class FormatParserStorage.
 | 
|---|
| 65 |  * Free all stored FormatParsers. 
 | 
|---|
| 66 |  * Save on Exit.
 | 
|---|
| 67 |  */
 | 
|---|
| 68 | FormatParserStorage::~FormatParserStorage()
 | 
|---|
| 69 | {
 | 
|---|
| 70 |   for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
 | 
|---|
| 71 |     if (ParserPresent[iter]) {
 | 
|---|
| 72 |       if (ParserStream[iter]->is_open())
 | 
|---|
| 73 |         ParserStream[iter]->close();
 | 
|---|
| 74 |       delete ParserStream[iter];
 | 
|---|
| 75 |       delete ParserList[iter];
 | 
|---|
| 76 |     }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /** Sets the filename of all current parsers in storage to prefix.suffix.
 | 
|---|
| 80 |  * \param &prefix prefix to use.
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   prefix=_prefix;
 | 
|---|
| 85 | };
 | 
|---|
| 86 | 
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void FormatParserStorage::SaveAll()
 | 
|---|
| 89 | {
 | 
|---|
| 90 |   std::string filename;
 | 
|---|
| 91 |   for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
 | 
|---|
| 92 |     if (ParserPresent[iter]) {
 | 
|---|
| 93 |       filename = prefix;
 | 
|---|
| 94 |       filename += ".";
 | 
|---|
| 95 |       filename += ParserSuffix[iter];
 | 
|---|
| 96 |       ParserStream[iter] = new std::ofstream(filename.c_str());
 | 
|---|
| 97 |       ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
 | 
|---|
| 98 |     }
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|
| 101 | 
 | 
|---|
| 102 | /** Adds an MpqcParser to the storage.
 | 
|---|
| 103 |  */
 | 
|---|
| 104 | void FormatParserStorage::addMpqc()
 | 
|---|
| 105 | {
 | 
|---|
| 106 |   if (!ParserPresent[mpqc]) {
 | 
|---|
| 107 |     ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
 | 
|---|
| 108 |     ParserPresent[mpqc] = true;
 | 
|---|
| 109 |   }
 | 
|---|
| 110 |   else
 | 
|---|
| 111 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | /** Adds an PcpParser to the storage.
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | void FormatParserStorage::addPcp()
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   if (!ParserPresent[pcp]) {
 | 
|---|
| 119 |     ParserList[pcp] = new PcpParser();
 | 
|---|
| 120 |     ParserPresent[pcp] = true;
 | 
|---|
| 121 |   } else
 | 
|---|
| 122 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** Adds an TremoloParser to the storage.
 | 
|---|
| 127 |  */
 | 
|---|
| 128 | void FormatParserStorage::addTremolo()
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   if (!ParserPresent[tremolo]) {
 | 
|---|
| 131 |     ParserList[tremolo] = new TremoloParser();
 | 
|---|
| 132 |     ParserPresent[tremolo] = true;
 | 
|---|
| 133 |   } else
 | 
|---|
| 134 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Adds an XyzParser to the storage.
 | 
|---|
| 139 |  */
 | 
|---|
| 140 | void FormatParserStorage::addXyz()
 | 
|---|
| 141 | {
 | 
|---|
| 142 |   if (!ParserPresent[xyz]) {
 | 
|---|
| 143 |     ParserList[xyz] = new XyzParser();
 | 
|---|
| 144 |     ParserPresent[xyz] = true;
 | 
|---|
| 145 |   } else
 | 
|---|
| 146 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /** Parses an istream depending on its suffix
 | 
|---|
| 150 |  * \param &input input stream
 | 
|---|
| 151 |  * \param suffix
 | 
|---|
| 152 |  * \return true - parsing ok, false - suffix unknown
 | 
|---|
| 153 |  */
 | 
|---|
| 154 | bool FormatParserStorage::get(std::istream &input, std::string suffix)
 | 
|---|
| 155 | {
 | 
|---|
| 156 |   if (suffix == ParserSuffix[mpqc]) {
 | 
|---|
| 157 |     getMpqc().load(&input);
 | 
|---|
| 158 |   } else if (suffix == ParserSuffix[pcp]) {
 | 
|---|
| 159 |     getPcp().load(&input);
 | 
|---|
| 160 |   } else if (suffix == ParserSuffix[tremolo]) {
 | 
|---|
| 161 |     getTremolo().load(&input);
 | 
|---|
| 162 |   } else if (suffix == ParserSuffix[xyz]) {
 | 
|---|
| 163 |     getXyz().load(&input);
 | 
|---|
| 164 |   } else {
 | 
|---|
| 165 |     DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix to for FormatParserStorage::get()." << endl);
 | 
|---|
| 166 |     return false;
 | 
|---|
| 167 |   }
 | 
|---|
| 168 |   return true;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /** Returns reference to the output MpqcParser, adds if not present.
 | 
|---|
| 172 |  * \return reference to the output MpqcParser
 | 
|---|
| 173 |  */
 | 
|---|
| 174 | MpqcParser &FormatParserStorage::getMpqc()
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   if (!ParserPresent[mpqc])
 | 
|---|
| 177 |     addMpqc();
 | 
|---|
| 178 |   return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /** Returns reference to the output PcpParser, adds if not present.
 | 
|---|
| 182 |  * \return reference to the output PcpParser
 | 
|---|
| 183 |  */
 | 
|---|
| 184 | PcpParser &FormatParserStorage::getPcp()
 | 
|---|
| 185 | {
 | 
|---|
| 186 |   if (!ParserPresent[pcp])
 | 
|---|
| 187 |     addPcp();
 | 
|---|
| 188 |   return dynamic_cast<PcpParser &>(*ParserList[pcp]);
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | /** Returns reference to the output TremoloParser, adds if not present.
 | 
|---|
| 192 |  * \return reference to the output TremoloParser
 | 
|---|
| 193 |  */
 | 
|---|
| 194 | TremoloParser &FormatParserStorage::getTremolo()
 | 
|---|
| 195 | {
 | 
|---|
| 196 |   if (!ParserPresent[tremolo])
 | 
|---|
| 197 |     addTremolo();
 | 
|---|
| 198 |   return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | /** Returns reference to the output XyzParser, adds if not present.
 | 
|---|
| 202 |  * \return reference to the output XyzParser
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | XyzParser &FormatParserStorage::getXyz()
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   if (!ParserPresent[xyz])
 | 
|---|
| 207 |     addXyz();
 | 
|---|
| 208 |   return dynamic_cast<XyzParser &>(*ParserList[xyz]);
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | 
 | 
|---|
| 212 | 
 | 
|---|
| 213 | CONSTRUCT_SINGLETON(FormatParserStorage)
 | 
|---|