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