source: src/ConfigFileBuffer.cpp@ ea2830

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 ea2830 was 97b825, checked in by Frederik Heber <heber@…>, 15 years ago

Shortened constructors [Meyers, "Effective C++" item 12]

  • also rearranged some initialization list (one per line).
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 * ConfigFileBuffer.cpp
3 *
4 * Created on: 12.06.2010
5 * Author: heber
6 */
7
8#include "ConfigFileBuffer.hpp"
9#include "Helpers/helpers.hpp"
10#include "lists.hpp"
11#include "Helpers/Verbose.hpp"
12#include "Helpers/Log.hpp"
13#include "World.hpp"
14
15/******************************** Functions for class ConfigFileBuffer **********************/
16
17/** Structure containing compare function for Ion_Type sorting.
18 */
19struct IonTypeCompare {
20 bool operator()(const char* s1, const char *s2) const {
21 char number1[8];
22 char number2[8];
23 const char *dummy1, *dummy2;
24 //Log() << Verbose(0) << s1 << " " << s2 << endl;
25 dummy1 = strchr(s1, '_')+sizeof(char)*5; // go just after "Ion_Type"
26 dummy2 = strchr(dummy1, '_');
27 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
28 number1[dummy2-dummy1]='\0';
29 dummy1 = strchr(s2, '_')+sizeof(char)*5; // go just after "Ion_Type"
30 dummy2 = strchr(dummy1, '_');
31 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
32 number2[dummy2-dummy1]='\0';
33 if (atoi(number1) != atoi(number2))
34 return (atoi(number1) < atoi(number2));
35 else {
36 dummy1 = strchr(s1, '_')+sizeof(char);
37 dummy1 = strchr(dummy1, '_')+sizeof(char);
38 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
39 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
40 number1[dummy2-dummy1]='\0';
41 dummy1 = strchr(s2, '_')+sizeof(char);
42 dummy1 = strchr(dummy1, '_')+sizeof(char);
43 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
44 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
45 number2[dummy2-dummy1]='\0';
46 return (atoi(number1) < atoi(number2));
47 }
48 }
49};
50
51/** Constructor for ConfigFileBuffer class.
52 */
53ConfigFileBuffer::ConfigFileBuffer() :
54 buffer(NULL),
55 LineMapping(NULL),
56 CurrentLine(0),
57 NoLines(0)
58{
59};
60
61/** Constructor for ConfigFileBuffer class with filename to be parsed.
62 * \param *filename file name
63 */
64ConfigFileBuffer::ConfigFileBuffer(const char * const filename) :
65 buffer(NULL),
66 LineMapping(NULL),
67 CurrentLine(0),
68 NoLines(0)
69{
70 InitFileBuffer(filename);
71}
72
73void ConfigFileBuffer::InitFileBuffer(const char * const filename)
74{
75 ifstream *file= new ifstream(filename);
76 InitFileBuffer(file);
77}
78
79void ConfigFileBuffer::InitFileBuffer(istream *file)
80{
81 char line[MAXSTRINGSIZE];
82
83 RemoveMapping();
84
85 // prescan number of lines
86 if (file->fail()) {
87 DoeLog(1) && (eLog()<< Verbose(1) << "config file missing!" << endl);
88 return;
89 }
90 NoLines = 0; // we're overcounting by one
91 long file_position = file->tellg(); // mark current position
92 do {
93 file->getline(line, MAXSTRINGSIZE-1);
94 NoLines++;
95 } while (!file->eof());
96 file->clear();
97 file->seekg(file_position, ios::beg);
98 DoLog(1) && (Log() << Verbose(1) << NoLines-1 << " lines were recognized." << endl);
99
100 // allocate buffer's 1st dimension
101 if (buffer != NULL) {
102 DoeLog(1) && (eLog()<< Verbose(1) << "FileBuffer->buffer is not NULL!" << endl);
103 return;
104 } else
105 buffer = new char *[NoLines];
106
107 // scan each line and put into buffer
108 int lines=0;
109 int i;
110 do {
111 buffer[lines] = new char[MAXSTRINGSIZE];
112 file->getline(buffer[lines], MAXSTRINGSIZE-1);
113 i = strlen(buffer[lines]);
114 buffer[lines][i] = '\n';
115 buffer[lines][i+1] = '\0';
116 lines++;
117 } while((!file->eof()) && (lines < NoLines));
118 DoLog(1) && (Log() << Verbose(1) << lines-1 << " lines were read into the buffer." << endl);
119 file->clear();
120 file->seekg(file_position, ios::beg);
121
122 InitMapping();
123}
124
125/** Destructor for ConfigFileBuffer class.
126 */
127ConfigFileBuffer::~ConfigFileBuffer()
128{
129 RemoveBuffer();
130 RemoveMapping();
131}
132
133
134/** Create trivial mapping.
135 */
136void ConfigFileBuffer::InitMapping()
137{
138 LineMapping = new int[NoLines];
139 for (int i=0;i<NoLines;i++)
140 LineMapping[i] = i;
141 MappingAllocated = true;
142}
143
144/** Remove allocated mapping.
145 */
146void ConfigFileBuffer::RemoveMapping()
147{
148 delete[](LineMapping);
149 MappingAllocated = false;
150}
151
152/** Remove allocated mapping.
153 */
154void ConfigFileBuffer::RemoveBuffer()
155{
156 for(int i=0;i<NoLines;++i)
157 delete[](buffer[i]);
158 delete[](buffer);
159}
160
161
162/** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted.
163 * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the
164 * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine
165 * points to first Ion_Type entry.
166 * \param *FileBuffer pointer to buffer structure
167 * \param NoAtoms of subsequent lines to look at
168 */
169void ConfigFileBuffer::MapIonTypesInBuffer(const int NoAtoms)
170{
171 map<const char *, int, IonTypeCompare> IonTypeLineMap;
172 if (!MappingAllocated) {
173 InitMapping();
174 }
175
176 // put all into hashed map
177 for (int i=0; i<NoAtoms; ++i) {
178 IonTypeLineMap.insert(pair<const char *, int> (buffer[CurrentLine+i], CurrentLine+i));
179 }
180
181 // fill map
182 int nr=0;
183 for (map<const char *, int, IonTypeCompare>::iterator runner = IonTypeLineMap.begin(); runner != IonTypeLineMap.end(); ++runner) {
184 if (CurrentLine+nr < NoLines)
185 LineMapping[CurrentLine+(nr++)] = runner->second;
186 else {
187 DoeLog(0) && (eLog()<< Verbose(0) << "config::MapIonTypesInBuffer - NoAtoms is wrong: We are past the end of the file!" << endl);
188 performCriticalExit();
189 }
190 }
191}
Note: See TracBrowser for help on using the repository browser.