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