source: src/ConfigFileBuffer.cpp@ 9cd807

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 9cd807 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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