[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"
|
---|
[bb6193] | 30 | #include "Parser/PdbParser.hpp"
|
---|
[52baf9] | 31 | #include "Parser/TremoloParser.hpp"
|
---|
| 32 | #include "Parser/XyzParser.hpp"
|
---|
| 33 |
|
---|
[952f38] | 34 | #include "Helpers/Log.hpp"
|
---|
| 35 | #include "Helpers/Verbose.hpp"
|
---|
[52baf9] | 36 |
|
---|
| 37 | #include "Helpers/Assert.hpp"
|
---|
| 38 |
|
---|
| 39 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 40 |
|
---|
[dc0d21] | 41 | /** Increment operator for the enumeration ParserTypes to allow loops.
|
---|
| 42 | * \param &type value
|
---|
| 43 | * \return value incremented by one
|
---|
| 44 | */
|
---|
| 45 | ParserTypes &operator++(ParserTypes &type)
|
---|
| 46 | {
|
---|
| 47 | return type = ParserTypes(type+1);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[52baf9] | 50 | /** Constructor of class FormatParserStorage.
|
---|
| 51 | */
|
---|
| 52 | FormatParserStorage::FormatParserStorage()
|
---|
| 53 | {
|
---|
| 54 | ParserList.resize(ParserTypes_end, NULL);
|
---|
[60239f] | 55 | ParserStream.resize(ParserTypes_end, NULL);
|
---|
[52baf9] | 56 | ParserPresent.resize(ParserTypes_end, false);
|
---|
| 57 |
|
---|
[5c6946] | 58 | ParserNames[mpqc] = "mpqc";
|
---|
| 59 | ParserNames[pcp] = "pcp";
|
---|
[bb6193] | 60 | ParserNames[pdb] = "pdb";
|
---|
[5c6946] | 61 | ParserNames[tremolo] = "tremolo";
|
---|
| 62 | ParserNames[xyz] = "xyz";
|
---|
| 63 |
|
---|
| 64 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
|
---|
| 65 | ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
| 66 |
|
---|
[a42054] | 67 | ParserSuffixes[mpqc] = "in";
|
---|
| 68 | ParserSuffixes[pcp] = "conf";
|
---|
| 69 | ParserSuffixes[pdb] = "pdb";
|
---|
| 70 | ParserSuffixes[tremolo] = "data";
|
---|
| 71 | ParserSuffixes[xyz] = "xyz";
|
---|
| 72 |
|
---|
| 73 | for (std::map<ParserTypes, std::string>::const_iterator it = ParserSuffixes.begin(); it != ParserSuffixes.end(); ++it)
|
---|
| 74 | ParserLookupSuffixes.insert(pair<std::string, ParserTypes>(it->second,it->first) );
|
---|
[5c6946] | 75 |
|
---|
| 76 | ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
|
---|
| 77 | ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
|
---|
[bb6193] | 78 | ParserAddFunction[pdb] = &FormatParserStorage::addPdb;
|
---|
[5c6946] | 79 | ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
|
---|
| 80 | ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
|
---|
[52baf9] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Destructor of class FormatParserStorage.
|
---|
| 84 | * Free all stored FormatParsers.
|
---|
| 85 | * Save on Exit.
|
---|
| 86 | */
|
---|
| 87 | FormatParserStorage::~FormatParserStorage()
|
---|
| 88 | {
|
---|
| 89 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
[60239f] | 90 | if (ParserPresent[iter]) {
|
---|
| 91 | if (ParserStream[iter]->is_open())
|
---|
| 92 | ParserStream[iter]->close();
|
---|
| 93 | delete ParserStream[iter];
|
---|
[52baf9] | 94 | delete ParserList[iter];
|
---|
[60239f] | 95 | }
|
---|
[52baf9] | 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Sets the filename of all current parsers in storage to prefix.suffix.
|
---|
| 99 | * \param &prefix prefix to use.
|
---|
| 100 | */
|
---|
| 101 | void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
|
---|
| 102 | {
|
---|
| 103 | prefix=_prefix;
|
---|
| 104 | };
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | void FormatParserStorage::SaveAll()
|
---|
| 108 | {
|
---|
| 109 | std::string filename;
|
---|
| 110 | for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
|
---|
| 111 | if (ParserPresent[iter]) {
|
---|
| 112 | filename = prefix;
|
---|
| 113 | filename += ".";
|
---|
[a42054] | 114 | filename += ParserSuffixes[iter];
|
---|
[60239f] | 115 | ParserStream[iter] = new std::ofstream(filename.c_str());
|
---|
| 116 | ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
|
---|
[52baf9] | 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /** Adds an MpqcParser to the storage.
|
---|
| 122 | */
|
---|
[dc0d21] | 123 | void FormatParserStorage::addMpqc()
|
---|
[52baf9] | 124 | {
|
---|
[dc0d21] | 125 | if (!ParserPresent[mpqc]) {
|
---|
[52baf9] | 126 | ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
|
---|
[dc0d21] | 127 | ParserPresent[mpqc] = true;
|
---|
| 128 | }
|
---|
[52baf9] | 129 | else
|
---|
| 130 | DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[5c6946] | 133 |
|
---|
[52baf9] | 134 | /** Adds an PcpParser to the storage.
|
---|
| 135 | */
|
---|
[dc0d21] | 136 | void FormatParserStorage::addPcp()
|
---|
[52baf9] | 137 | {
|
---|
[dc0d21] | 138 | if (!ParserPresent[pcp]) {
|
---|
[52baf9] | 139 | ParserList[pcp] = new PcpParser();
|
---|
[dc0d21] | 140 | ParserPresent[pcp] = true;
|
---|
| 141 | } else
|
---|
[52baf9] | 142 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
[bb6193] | 146 | /** Adds an PdbParser to the storage.
|
---|
| 147 | */
|
---|
| 148 | void FormatParserStorage::addPdb()
|
---|
| 149 | {
|
---|
| 150 | if (!ParserPresent[pdb]) {
|
---|
| 151 | ParserList[pdb] = new PdbParser();
|
---|
| 152 | ParserPresent[pdb] = true;
|
---|
| 153 | } else
|
---|
| 154 | DoeLog(1) && (eLog() << Verbose(1) << "Parser pdb is already present." << endl);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
[52baf9] | 158 | /** Adds an TremoloParser to the storage.
|
---|
| 159 | */
|
---|
[dc0d21] | 160 | void FormatParserStorage::addTremolo()
|
---|
[52baf9] | 161 | {
|
---|
[dc0d21] | 162 | if (!ParserPresent[tremolo]) {
|
---|
[52baf9] | 163 | ParserList[tremolo] = new TremoloParser();
|
---|
[dc0d21] | 164 | ParserPresent[tremolo] = true;
|
---|
| 165 | } else
|
---|
[52baf9] | 166 | DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | /** Adds an XyzParser to the storage.
|
---|
| 171 | */
|
---|
[dc0d21] | 172 | void FormatParserStorage::addXyz()
|
---|
[52baf9] | 173 | {
|
---|
[dc0d21] | 174 | if (!ParserPresent[xyz]) {
|
---|
[52baf9] | 175 | ParserList[xyz] = new XyzParser();
|
---|
[dc0d21] | 176 | ParserPresent[xyz] = true;
|
---|
| 177 | } else
|
---|
[52baf9] | 178 | DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[a42054] | 181 | ParserTypes FormatParserStorage::getTypeFromName(std::string type)
|
---|
[5c6946] | 182 | {
|
---|
| 183 | if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
|
---|
| 184 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
| 185 | return ParserTypes_end;
|
---|
| 186 | } else
|
---|
| 187 | return ParserLookupNames[type];
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[a42054] | 190 | ParserTypes FormatParserStorage::getTypeFromSuffix(std::string type)
|
---|
| 191 | {
|
---|
| 192 | if (ParserLookupSuffixes.find(type) == ParserLookupSuffixes.end()) {
|
---|
| 193 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
|
---|
| 194 | return ParserTypes_end;
|
---|
| 195 | } else
|
---|
| 196 | return ParserLookupSuffixes[type];
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[5c6946] | 199 | bool FormatParserStorage::add(ParserTypes ptype)
|
---|
| 200 | {
|
---|
| 201 | if (ptype != ParserTypes_end) {
|
---|
| 202 | if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
|
---|
| 203 | DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
|
---|
| 204 | (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
|
---|
| 205 | return true;
|
---|
| 206 | } else {
|
---|
| 207 | DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
|
---|
| 208 | return false;
|
---|
| 209 | }
|
---|
| 210 | } else {
|
---|
| 211 | return false;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | bool FormatParserStorage::add(std::string type)
|
---|
| 216 | {
|
---|
[a42054] | 217 | return add(getTypeFromName(type));
|
---|
[5c6946] | 218 | }
|
---|
| 219 |
|
---|
| 220 |
|
---|
[86cff86] | 221 | /** Parses an istream depending on its suffix
|
---|
| 222 | * \param &input input stream
|
---|
| 223 | * \param suffix
|
---|
| 224 | * \return true - parsing ok, false - suffix unknown
|
---|
| 225 | */
|
---|
| 226 | bool FormatParserStorage::get(std::istream &input, std::string suffix)
|
---|
| 227 | {
|
---|
[a42054] | 228 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
[86cff86] | 229 | getMpqc().load(&input);
|
---|
[a42054] | 230 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
[86cff86] | 231 | getPcp().load(&input);
|
---|
[a42054] | 232 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
[bb6193] | 233 | getPdb().load(&input);
|
---|
[a42054] | 234 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
[86cff86] | 235 | getTremolo().load(&input);
|
---|
[a42054] | 236 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
[86cff86] | 237 | getXyz().load(&input);
|
---|
| 238 | } else {
|
---|
[5c6946] | 239 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
|
---|
[86cff86] | 240 | return false;
|
---|
| 241 | }
|
---|
| 242 | return true;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[cabb46] | 245 |
|
---|
| 246 | /** Stores world in an ostream depending on its suffix
|
---|
| 247 | * \param &output output stream
|
---|
| 248 | * \param suffix
|
---|
| 249 | * \return true - storing ok, false - suffix unknown
|
---|
| 250 | */
|
---|
| 251 | bool FormatParserStorage::put(std::ostream &output, std::string suffix)
|
---|
| 252 | {
|
---|
| 253 | if (suffix == ParserSuffixes[mpqc]) {
|
---|
| 254 | getMpqc().save(&output);
|
---|
| 255 | } else if (suffix == ParserSuffixes[pcp]) {
|
---|
| 256 | getPcp().save(&output);
|
---|
| 257 | } else if (suffix == ParserSuffixes[pdb]) {
|
---|
| 258 | getPdb().save(&output);
|
---|
| 259 | } else if (suffix == ParserSuffixes[tremolo]) {
|
---|
| 260 | getTremolo().save(&output);
|
---|
| 261 | } else if (suffix == ParserSuffixes[xyz]) {
|
---|
| 262 | getXyz().save(&output);
|
---|
| 263 | } else {
|
---|
| 264 | DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::put()." << endl);
|
---|
| 265 | return false;
|
---|
| 266 | }
|
---|
| 267 | return true;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[dc0d21] | 270 | /** Returns reference to the output MpqcParser, adds if not present.
|
---|
| 271 | * \return reference to the output MpqcParser
|
---|
| 272 | */
|
---|
| 273 | MpqcParser &FormatParserStorage::getMpqc()
|
---|
| 274 | {
|
---|
| 275 | if (!ParserPresent[mpqc])
|
---|
| 276 | addMpqc();
|
---|
| 277 | return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /** Returns reference to the output PcpParser, adds if not present.
|
---|
| 281 | * \return reference to the output PcpParser
|
---|
| 282 | */
|
---|
| 283 | PcpParser &FormatParserStorage::getPcp()
|
---|
| 284 | {
|
---|
| 285 | if (!ParserPresent[pcp])
|
---|
| 286 | addPcp();
|
---|
| 287 | return dynamic_cast<PcpParser &>(*ParserList[pcp]);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[bb6193] | 290 | /** Returns reference to the output PdbParser, adds if not present.
|
---|
| 291 | * \return reference to the output PdbParser
|
---|
| 292 | */
|
---|
| 293 | PdbParser &FormatParserStorage::getPdb()
|
---|
| 294 | {
|
---|
| 295 | if (!ParserPresent[pdb])
|
---|
| 296 | addPdb();
|
---|
| 297 | return dynamic_cast<PdbParser &>(*ParserList[pdb]);
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[dc0d21] | 300 | /** Returns reference to the output TremoloParser, adds if not present.
|
---|
| 301 | * \return reference to the output TremoloParser
|
---|
| 302 | */
|
---|
| 303 | TremoloParser &FormatParserStorage::getTremolo()
|
---|
| 304 | {
|
---|
| 305 | if (!ParserPresent[tremolo])
|
---|
| 306 | addTremolo();
|
---|
| 307 | return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /** Returns reference to the output XyzParser, adds if not present.
|
---|
| 311 | * \return reference to the output XyzParser
|
---|
| 312 | */
|
---|
| 313 | XyzParser &FormatParserStorage::getXyz()
|
---|
| 314 | {
|
---|
| 315 | if (!ParserPresent[xyz])
|
---|
| 316 | addXyz();
|
---|
| 317 | return dynamic_cast<XyzParser &>(*ParserList[xyz]);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
[52baf9] | 321 |
|
---|
| 322 | CONSTRUCT_SINGLETON(FormatParserStorage)
|
---|