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