| [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 | 
 | 
|---|
| [bbbad5] | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [52baf9] | 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 | 
 | 
|---|
| [952f38] | 33 | #include "Helpers/Log.hpp"
 | 
|---|
 | 34 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [52baf9] | 35 | 
 | 
|---|
 | 36 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "Patterns/Singleton_impl.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [dc0d21] | 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 | 
 | 
|---|
| [52baf9] | 49 | /** Constructor of class FormatParserStorage.
 | 
|---|
 | 50 |  */
 | 
|---|
 | 51 | FormatParserStorage::FormatParserStorage()
 | 
|---|
 | 52 | {
 | 
|---|
 | 53 |   ParserList.resize(ParserTypes_end, NULL);
 | 
|---|
| [60239f] | 54 |   ParserStream.resize(ParserTypes_end, NULL);
 | 
|---|
| [52baf9] | 55 |   ParserPresent.resize(ParserTypes_end, false);
 | 
|---|
 | 56 |   ParserSuffix.resize(ParserTypes_end, "");
 | 
|---|
 | 57 | 
 | 
|---|
| [86cff86] | 58 |   ParserSuffix[mpqc] = "in";
 | 
|---|
| [52baf9] | 59 |   ParserSuffix[pcp] = "conf";
 | 
|---|
| [86cff86] | 60 |   ParserSuffix[tremolo] = "data";
 | 
|---|
 | 61 |   ParserSuffix[xyz] = "xyz";
 | 
|---|
| [52baf9] | 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)
 | 
|---|
| [60239f] | 71 |     if (ParserPresent[iter]) {
 | 
|---|
 | 72 |       if (ParserStream[iter]->is_open())
 | 
|---|
 | 73 |         ParserStream[iter]->close();
 | 
|---|
 | 74 |       delete ParserStream[iter];
 | 
|---|
| [52baf9] | 75 |       delete ParserList[iter];
 | 
|---|
| [60239f] | 76 |     }
 | 
|---|
| [52baf9] | 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];
 | 
|---|
| [60239f] | 96 |       ParserStream[iter] = new std::ofstream(filename.c_str());
 | 
|---|
 | 97 |       ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
 | 
|---|
| [52baf9] | 98 |     }
 | 
|---|
 | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | /** Adds an MpqcParser to the storage.
 | 
|---|
 | 103 |  */
 | 
|---|
| [dc0d21] | 104 | void FormatParserStorage::addMpqc()
 | 
|---|
| [52baf9] | 105 | {
 | 
|---|
| [dc0d21] | 106 |   if (!ParserPresent[mpqc]) {
 | 
|---|
| [52baf9] | 107 |     ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
 | 
|---|
| [dc0d21] | 108 |     ParserPresent[mpqc] = true;
 | 
|---|
 | 109 |   }
 | 
|---|
| [52baf9] | 110 |   else
 | 
|---|
 | 111 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | /** Adds an PcpParser to the storage.
 | 
|---|
 | 115 |  */
 | 
|---|
| [dc0d21] | 116 | void FormatParserStorage::addPcp()
 | 
|---|
| [52baf9] | 117 | {
 | 
|---|
| [dc0d21] | 118 |   if (!ParserPresent[pcp]) {
 | 
|---|
| [52baf9] | 119 |     ParserList[pcp] = new PcpParser();
 | 
|---|
| [dc0d21] | 120 |     ParserPresent[pcp] = true;
 | 
|---|
 | 121 |   } else
 | 
|---|
| [52baf9] | 122 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 | /** Adds an TremoloParser to the storage.
 | 
|---|
 | 127 |  */
 | 
|---|
| [dc0d21] | 128 | void FormatParserStorage::addTremolo()
 | 
|---|
| [52baf9] | 129 | {
 | 
|---|
| [dc0d21] | 130 |   if (!ParserPresent[tremolo]) {
 | 
|---|
| [52baf9] | 131 |     ParserList[tremolo] = new TremoloParser();
 | 
|---|
| [dc0d21] | 132 |     ParserPresent[tremolo] = true;
 | 
|---|
 | 133 |   } else
 | 
|---|
| [52baf9] | 134 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
 | 
|---|
 | 135 | }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | /** Adds an XyzParser to the storage.
 | 
|---|
 | 139 |  */
 | 
|---|
| [dc0d21] | 140 | void FormatParserStorage::addXyz()
 | 
|---|
| [52baf9] | 141 | {
 | 
|---|
| [dc0d21] | 142 |   if (!ParserPresent[xyz]) {
 | 
|---|
| [52baf9] | 143 |     ParserList[xyz] = new XyzParser();
 | 
|---|
| [dc0d21] | 144 |     ParserPresent[xyz] = true;
 | 
|---|
 | 145 |   } else
 | 
|---|
| [52baf9] | 146 |     DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
| [86cff86] | 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 | 
 | 
|---|
| [dc0d21] | 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 | 
 | 
|---|
| [52baf9] | 212 | 
 | 
|---|
 | 213 | CONSTRUCT_SINGLETON(FormatParserStorage)
 | 
|---|