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