source: src/ConfigFileBuffer.cpp@ bbbad5

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 Candidate_v1.7.0 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 bbbad5 was bbbad5, checked in by Frederik Heber <heber@…>, 15 years ago

Added MemDebug.hpp to each and every .cpp file (were it was still missing).

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