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