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