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