source: src/Parser/FormatParserStorage.cpp@ 5c6946

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 5c6946 was 5c6946, checked in by Frederik Heber <heber@…>, 14 years ago

FormatParserStorage extended a bit.

  • new functions add(type) that accept either parser name or ParserTypes and adds the respective class.
  • new function getType(type) that accepts a parser name and returns the type.
  • Property mode set to 100644
File size: 6.8 KB
Line 
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
8/** \file FormatParserStorage.cpp
9 *
10 * date: Jun, 22 2010
11 * author: heber
12 *
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "Helpers/MemDebug.hpp"
21
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"
30#include "Parser/TremoloParser.hpp"
31#include "Parser/XyzParser.hpp"
32
33#include "Helpers/Log.hpp"
34#include "Helpers/Verbose.hpp"
35
36#include "Helpers/Assert.hpp"
37
38#include "Patterns/Singleton_impl.hpp"
39
40/** Increment operator for the enumeration ParserTypes to allow loops.
41 * \param &type value
42 * \return value incremented by one
43 */
44ParserTypes &operator++(ParserTypes &type)
45{
46 return type = ParserTypes(type+1);
47}
48
49/** Constructor of class FormatParserStorage.
50 */
51FormatParserStorage::FormatParserStorage()
52{
53 ParserList.resize(ParserTypes_end, NULL);
54 ParserStream.resize(ParserTypes_end, NULL);
55 ParserPresent.resize(ParserTypes_end, false);
56 ParserSuffix.resize(ParserTypes_end, "");
57
58 ParserNames[mpqc] = "mpqc";
59 ParserNames[pcp] = "pcp";
60 ParserNames[tremolo] = "tremolo";
61 ParserNames[xyz] = "xyz";
62
63 for (std::map<ParserTypes, std::string>::const_iterator it = ParserNames.begin(); it != ParserNames.end(); ++it)
64 ParserLookupNames.insert(pair<std::string, ParserTypes>(it->second,it->first) );
65
66 ParserSuffix[mpqc] = "in";
67 ParserSuffix[pcp] = "conf";
68 ParserSuffix[tremolo] = "data";
69 ParserSuffix[xyz] = "xyz";
70
71 ParserAddFunction[mpqc] = &FormatParserStorage::addMpqc;
72 ParserAddFunction[pcp] = &FormatParserStorage::addPcp;
73 ParserAddFunction[tremolo] = &FormatParserStorage::addTremolo;
74 ParserAddFunction[xyz] = &FormatParserStorage::addXyz;
75}
76
77/** Destructor of class FormatParserStorage.
78 * Free all stored FormatParsers.
79 * Save on Exit.
80 */
81FormatParserStorage::~FormatParserStorage()
82{
83 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
84 if (ParserPresent[iter]) {
85 if (ParserStream[iter]->is_open())
86 ParserStream[iter]->close();
87 delete ParserStream[iter];
88 delete ParserList[iter];
89 }
90}
91
92/** Sets the filename of all current parsers in storage to prefix.suffix.
93 * \param &prefix prefix to use.
94 */
95void FormatParserStorage::SetOutputPrefixForAll(std::string &_prefix)
96{
97 prefix=_prefix;
98};
99
100
101void FormatParserStorage::SaveAll()
102{
103 std::string filename;
104 for (ParserTypes iter = ParserTypes_begin; iter < ParserTypes_end; ++iter)
105 if (ParserPresent[iter]) {
106 filename = prefix;
107 filename += ".";
108 filename += ParserSuffix[iter];
109 ParserStream[iter] = new std::ofstream(filename.c_str());
110 ParserList[iter]->setOstream((std::ostream *)ParserStream[iter]);
111 }
112}
113
114
115/** Adds an MpqcParser to the storage.
116 */
117void FormatParserStorage::addMpqc()
118{
119 if (!ParserPresent[mpqc]) {
120 ParserList[mpqc] = dynamic_cast<FormatParser *>(new MpqcParser);
121 ParserPresent[mpqc] = true;
122 }
123 else
124 DoeLog(1) && (eLog() << Verbose(1) << "Parser mpqc is already present." << endl);
125}
126
127
128/** Adds an PcpParser to the storage.
129 */
130void FormatParserStorage::addPcp()
131{
132 if (!ParserPresent[pcp]) {
133 ParserList[pcp] = new PcpParser();
134 ParserPresent[pcp] = true;
135 } else
136 DoeLog(1) && (eLog() << Verbose(1) << "Parser pcp is already present." << endl);
137}
138
139
140/** Adds an TremoloParser to the storage.
141 */
142void FormatParserStorage::addTremolo()
143{
144 if (!ParserPresent[tremolo]) {
145 ParserList[tremolo] = new TremoloParser();
146 ParserPresent[tremolo] = true;
147 } else
148 DoeLog(1) && (eLog() << Verbose(1) << "Parser tremolo is already present." << endl);
149}
150
151
152/** Adds an XyzParser to the storage.
153 */
154void FormatParserStorage::addXyz()
155{
156 if (!ParserPresent[xyz]) {
157 ParserList[xyz] = new XyzParser();
158 ParserPresent[xyz] = true;
159 } else
160 DoeLog(1) && (eLog() << Verbose(1) << "Parser xyz is already present." << endl);
161}
162
163ParserTypes FormatParserStorage::getType(std::string type)
164{
165 if (ParserLookupNames.find(type) == ParserLookupNames.end()) {
166 DoeLog(1) && (eLog() << Verbose(1) << "Unknown type " << type << "." << endl);
167 return ParserTypes_end;
168 } else
169 return ParserLookupNames[type];
170}
171
172bool FormatParserStorage::add(ParserTypes ptype)
173{
174 if (ptype != ParserTypes_end) {
175 if (ParserAddFunction.find(ptype) != ParserAddFunction.end()) {
176 DoLog(0) && (Log() << Verbose(0) << "Adding " << ParserNames[ptype] << " type to output." << endl);
177 (getInstance().*(ParserAddFunction[ptype]))(); // we still need an object to work on ...
178 return true;
179 } else {
180 DoeLog(1) && (eLog() << Verbose(1) << "No parser to add for this known type " << ParserNames[ptype] << ", not implemented?" << endl);
181 return false;
182 }
183 } else {
184 return false;
185 }
186}
187
188bool FormatParserStorage::add(std::string type)
189{
190 return add(getType(type));
191}
192
193
194/** Parses an istream depending on its suffix
195 * \param &input input stream
196 * \param suffix
197 * \return true - parsing ok, false - suffix unknown
198 */
199bool FormatParserStorage::get(std::istream &input, std::string suffix)
200{
201 if (suffix == ParserSuffix[mpqc]) {
202 getMpqc().load(&input);
203 } else if (suffix == ParserSuffix[pcp]) {
204 getPcp().load(&input);
205 } else if (suffix == ParserSuffix[tremolo]) {
206 getTremolo().load(&input);
207 } else if (suffix == ParserSuffix[xyz]) {
208 getXyz().load(&input);
209 } else {
210 DoeLog(1) && (eLog() << Verbose(1) << "Unknown suffix " << suffix << " to for FormatParserStorage::get()." << endl);
211 return false;
212 }
213 return true;
214}
215
216/** Returns reference to the output MpqcParser, adds if not present.
217 * \return reference to the output MpqcParser
218 */
219MpqcParser &FormatParserStorage::getMpqc()
220{
221 if (!ParserPresent[mpqc])
222 addMpqc();
223 return dynamic_cast<MpqcParser &>(*ParserList[mpqc]);
224}
225
226/** Returns reference to the output PcpParser, adds if not present.
227 * \return reference to the output PcpParser
228 */
229PcpParser &FormatParserStorage::getPcp()
230{
231 if (!ParserPresent[pcp])
232 addPcp();
233 return dynamic_cast<PcpParser &>(*ParserList[pcp]);
234}
235
236/** Returns reference to the output TremoloParser, adds if not present.
237 * \return reference to the output TremoloParser
238 */
239TremoloParser &FormatParserStorage::getTremolo()
240{
241 if (!ParserPresent[tremolo])
242 addTremolo();
243 return dynamic_cast<TremoloParser &>(*ParserList[tremolo]);
244}
245
246/** Returns reference to the output XyzParser, adds if not present.
247 * \return reference to the output XyzParser
248 */
249XyzParser &FormatParserStorage::getXyz()
250{
251 if (!ParserPresent[xyz])
252 addXyz();
253 return dynamic_cast<XyzParser &>(*ParserList[xyz]);
254}
255
256
257
258CONSTRUCT_SINGLETON(FormatParserStorage)
Note: See TracBrowser for help on using the repository browser.