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