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