[88104f] | 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)
|
---|
[a3fded] | 60 | {
|
---|
| 61 | InitFileBuffer(filename);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void ConfigFileBuffer::InitFileBuffer(const char * const filename)
|
---|
[88104f] | 65 | {
|
---|
[43dad6] | 66 | ifstream *file= new ifstream(filename);
|
---|
| 67 | InitFileBuffer(file);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void ConfigFileBuffer::InitFileBuffer(istream *file)
|
---|
| 71 | {
|
---|
[88104f] | 72 | char line[MAXSTRINGSIZE];
|
---|
| 73 |
|
---|
[a3fded] | 74 | RemoveMapping();
|
---|
| 75 |
|
---|
[88104f] | 76 | // prescan number of lines
|
---|
[43dad6] | 77 | if (file->fail()) {
|
---|
| 78 | DoeLog(1) && (eLog()<< Verbose(1) << "config file missing!" << endl);
|
---|
[88104f] | 79 | return;
|
---|
| 80 | }
|
---|
| 81 | NoLines = 0; // we're overcounting by one
|
---|
| 82 | long file_position = file->tellg(); // mark current position
|
---|
| 83 | do {
|
---|
[1b2d30] | 84 | file->getline(line, MAXSTRINGSIZE-1);
|
---|
[88104f] | 85 | NoLines++;
|
---|
| 86 | } while (!file->eof());
|
---|
| 87 | file->clear();
|
---|
| 88 | file->seekg(file_position, ios::beg);
|
---|
| 89 | DoLog(1) && (Log() << Verbose(1) << NoLines-1 << " lines were recognized." << endl);
|
---|
| 90 |
|
---|
| 91 | // allocate buffer's 1st dimension
|
---|
| 92 | if (buffer != NULL) {
|
---|
| 93 | DoeLog(1) && (eLog()<< Verbose(1) << "FileBuffer->buffer is not NULL!" << endl);
|
---|
| 94 | return;
|
---|
| 95 | } else
|
---|
| 96 | buffer = new char *[NoLines];
|
---|
| 97 |
|
---|
| 98 | // scan each line and put into buffer
|
---|
| 99 | int lines=0;
|
---|
| 100 | int i;
|
---|
| 101 | do {
|
---|
| 102 | buffer[lines] = new char[MAXSTRINGSIZE];
|
---|
| 103 | file->getline(buffer[lines], MAXSTRINGSIZE-1);
|
---|
| 104 | i = strlen(buffer[lines]);
|
---|
| 105 | buffer[lines][i] = '\n';
|
---|
| 106 | buffer[lines][i+1] = '\0';
|
---|
| 107 | lines++;
|
---|
| 108 | } while((!file->eof()) && (lines < NoLines));
|
---|
| 109 | DoLog(1) && (Log() << Verbose(1) << lines-1 << " lines were read into the buffer." << endl);
|
---|
[1b2d30] | 110 | file->clear();
|
---|
| 111 | file->seekg(file_position, ios::beg);
|
---|
[88104f] | 112 |
|
---|
[a3fded] | 113 | InitMapping();
|
---|
[88104f] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Destructor for ConfigFileBuffer class.
|
---|
| 117 | */
|
---|
| 118 | ConfigFileBuffer::~ConfigFileBuffer()
|
---|
| 119 | {
|
---|
[a3fded] | 120 | RemoveBuffer();
|
---|
| 121 | RemoveMapping();
|
---|
[88104f] | 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | /** Create trivial mapping.
|
---|
| 126 | */
|
---|
| 127 | void ConfigFileBuffer::InitMapping()
|
---|
| 128 | {
|
---|
| 129 | LineMapping = new int[NoLines];
|
---|
| 130 | for (int i=0;i<NoLines;i++)
|
---|
| 131 | LineMapping[i] = i;
|
---|
[a3fded] | 132 | MappingAllocated = true;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Remove allocated mapping.
|
---|
| 136 | */
|
---|
| 137 | void ConfigFileBuffer::RemoveMapping()
|
---|
| 138 | {
|
---|
| 139 | delete[](LineMapping);
|
---|
| 140 | MappingAllocated = false;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /** Remove allocated mapping.
|
---|
| 144 | */
|
---|
| 145 | void ConfigFileBuffer::RemoveBuffer()
|
---|
| 146 | {
|
---|
| 147 | for(int i=0;i<NoLines;++i)
|
---|
| 148 | delete[](buffer[i]);
|
---|
| 149 | delete[](buffer);
|
---|
[88104f] | 150 | }
|
---|
| 151 |
|
---|
[a3fded] | 152 |
|
---|
[88104f] | 153 | /** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted.
|
---|
| 154 | * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the
|
---|
| 155 | * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine
|
---|
| 156 | * points to first Ion_Type entry.
|
---|
| 157 | * \param *FileBuffer pointer to buffer structure
|
---|
| 158 | * \param NoAtoms of subsequent lines to look at
|
---|
| 159 | */
|
---|
| 160 | void ConfigFileBuffer::MapIonTypesInBuffer(const int NoAtoms)
|
---|
| 161 | {
|
---|
| 162 | map<const char *, int, IonTypeCompare> IonTypeLineMap;
|
---|
[a3fded] | 163 | if (!MappingAllocated) {
|
---|
| 164 | InitMapping();
|
---|
[88104f] | 165 | }
|
---|
| 166 |
|
---|
| 167 | // put all into hashed map
|
---|
| 168 | for (int i=0; i<NoAtoms; ++i) {
|
---|
| 169 | IonTypeLineMap.insert(pair<const char *, int> (buffer[CurrentLine+i], CurrentLine+i));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | // fill map
|
---|
| 173 | int nr=0;
|
---|
| 174 | for (map<const char *, int, IonTypeCompare>::iterator runner = IonTypeLineMap.begin(); runner != IonTypeLineMap.end(); ++runner) {
|
---|
| 175 | if (CurrentLine+nr < NoLines)
|
---|
| 176 | LineMapping[CurrentLine+(nr++)] = runner->second;
|
---|
| 177 | else {
|
---|
| 178 | DoeLog(0) && (eLog()<< Verbose(0) << "config::MapIonTypesInBuffer - NoAtoms is wrong: We are past the end of the file!" << endl);
|
---|
| 179 | performCriticalExit();
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|