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