| 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 |  | 
|---|
| 19 | #include "log.hpp" | 
|---|
| 20 | #include "verbose.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "Helpers/Assert.hpp" | 
|---|
| 23 |  | 
|---|
| 24 | #include "Patterns/Singleton_impl.hpp" | 
|---|
| 25 |  | 
|---|
| 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 |  | 
|---|
| 35 | /** Constructor of class FormatParserStorage. | 
|---|
| 36 | */ | 
|---|
| 37 | FormatParserStorage::FormatParserStorage() | 
|---|
| 38 | { | 
|---|
| 39 | ParserList.resize(ParserTypes_end, NULL); | 
|---|
| 40 | ParserPresent.resize(ParserTypes_end, false); | 
|---|
| 41 | ParserSuffix.resize(ParserTypes_end, ""); | 
|---|
| 42 |  | 
|---|
| 43 | ParserSuffix[mpqc] = "conf.in"; | 
|---|
| 44 | ParserSuffix[pcp] = "conf"; | 
|---|
| 45 | ParserSuffix[tremolo] = "conf.data"; | 
|---|
| 46 | ParserSuffix[xyz] = "conf.xyz"; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | /** Destructor of class FormatParserStorage. | 
|---|
| 50 | * Free all stored FormatParsers. | 
|---|
| 51 | * Save on Exit. | 
|---|
| 52 | */ | 
|---|
| 53 | FormatParserStorage::~FormatParserStorage() | 
|---|
| 54 | { | 
|---|
| 55 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter) | 
|---|
| 56 | if (ParserPresent[iter]) | 
|---|
| 57 | delete ParserList[iter]; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /** Sets the filename of all current parsers in storage to prefix.suffix. | 
|---|
| 61 | * \param &prefix prefix to use. | 
|---|
| 62 | */ | 
|---|
| 63 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix) | 
|---|
| 64 | { | 
|---|
| 65 | prefix=_prefix; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | void FormatParserStorage::SaveAll() | 
|---|
| 70 | { | 
|---|
| 71 | std::string filename; | 
|---|
| 72 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter) | 
|---|
| 73 | if (ParserPresent[iter]) { | 
|---|
| 74 | filename = prefix; | 
|---|
| 75 | filename += "."; | 
|---|
| 76 | filename += ParserSuffix[iter]; | 
|---|
| 77 | std::ofstream *file = new std::ofstream(filename.c_str()); | 
|---|
| 78 | ParserList[iter]->setOstream((std::ostream *)file); | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /** Adds an MpqcParser to the storage. | 
|---|
| 84 | */ | 
|---|
| 85 | void FormatParserStorage::addMpqc() | 
|---|
| 86 | { | 
|---|
| 87 | if (!ParserPresent[mpqc]) { | 
|---|
| 88 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser); | 
|---|
| 89 | ParserPresent[mpqc] = true; | 
|---|
| 90 | } | 
|---|
| 91 | else | 
|---|
| 92 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /** Adds an PcpParser to the storage. | 
|---|
| 96 | */ | 
|---|
| 97 | void FormatParserStorage::addPcp() | 
|---|
| 98 | { | 
|---|
| 99 | if (!ParserPresent[pcp]) { | 
|---|
| 100 | ParserList[pcp] = new PcpParser(); | 
|---|
| 101 | ParserPresent[pcp] = true; | 
|---|
| 102 | } else | 
|---|
| 103 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | /** Adds an TremoloParser to the storage. | 
|---|
| 108 | */ | 
|---|
| 109 | void FormatParserStorage::addTremolo() | 
|---|
| 110 | { | 
|---|
| 111 | if (!ParserPresent[tremolo]) { | 
|---|
| 112 | ParserList[tremolo] = new TremoloParser(); | 
|---|
| 113 | ParserPresent[tremolo] = true; | 
|---|
| 114 | } else | 
|---|
| 115 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | /** Adds an XyzParser to the storage. | 
|---|
| 120 | */ | 
|---|
| 121 | void FormatParserStorage::addXyz() | 
|---|
| 122 | { | 
|---|
| 123 | if (!ParserPresent[xyz]) { | 
|---|
| 124 | ParserList[xyz] = new XyzParser(); | 
|---|
| 125 | ParserPresent[xyz] = true; | 
|---|
| 126 | } else | 
|---|
| 127 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | /** Returns reference to the output MpqcParser, adds if not present. | 
|---|
| 131 | * \return reference to the output MpqcParser | 
|---|
| 132 | */ | 
|---|
| 133 | MpqcParser &FormatParserStorage::getMpqc() | 
|---|
| 134 | { | 
|---|
| 135 | if (!ParserPresent[mpqc]) | 
|---|
| 136 | addMpqc(); | 
|---|
| 137 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /** Returns reference to the output PcpParser, adds if not present. | 
|---|
| 141 | * \return reference to the output PcpParser | 
|---|
| 142 | */ | 
|---|
| 143 | PcpParser &FormatParserStorage::getPcp() | 
|---|
| 144 | { | 
|---|
| 145 | if (!ParserPresent[pcp]) | 
|---|
| 146 | addPcp(); | 
|---|
| 147 | return dynamic_cast<PcpParser &>(*ParserList[pcp]); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** Returns reference to the output TremoloParser, adds if not present. | 
|---|
| 151 | * \return reference to the output TremoloParser | 
|---|
| 152 | */ | 
|---|
| 153 | TremoloParser &FormatParserStorage::getTremolo() | 
|---|
| 154 | { | 
|---|
| 155 | if (!ParserPresent[tremolo]) | 
|---|
| 156 | addTremolo(); | 
|---|
| 157 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | /** Returns reference to the output XyzParser, adds if not present. | 
|---|
| 161 | * \return reference to the output XyzParser | 
|---|
| 162 | */ | 
|---|
| 163 | XyzParser &FormatParserStorage::getXyz() | 
|---|
| 164 | { | 
|---|
| 165 | if (!ParserPresent[xyz]) | 
|---|
| 166 | addXyz(); | 
|---|
| 167 | return dynamic_cast<XyzParser &>(*ParserList[xyz]); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | CONSTRUCT_SINGLETON(FormatParserStorage) | 
|---|