| [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 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [bbbad5] | 21 |  | 
|---|
| [6a465e] | 22 | #include <iostream> | 
|---|
|  | 23 | #include <boost/tokenizer.hpp> | 
|---|
|  | 24 | #include <string> | 
|---|
|  | 25 |  | 
|---|
| [88104f] | 26 | #include "ConfigFileBuffer.hpp" | 
|---|
| [952f38] | 27 | #include "Helpers/helpers.hpp" | 
|---|
| [ad011c] | 28 | #include "CodePatterns/Verbose.hpp" | 
|---|
|  | 29 | #include "CodePatterns/Log.hpp" | 
|---|
| [88104f] | 30 | #include "World.hpp" | 
|---|
|  | 31 |  | 
|---|
|  | 32 | /******************************** Functions for class ConfigFileBuffer **********************/ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | /** Structure containing compare function for Ion_Type sorting. | 
|---|
|  | 35 | */ | 
|---|
|  | 36 | struct IonTypeCompare { | 
|---|
| [6a465e] | 37 | bool operator()(std::string s1, std::string s2) const { | 
|---|
|  | 38 | ConvertTo<int> toInt; | 
|---|
|  | 39 | boost::char_separator<char> sep("_"); | 
|---|
|  | 40 | tokenizer tokens1(s1,sep); | 
|---|
|  | 41 | tokenizer tokens2(s2,sep); | 
|---|
|  | 42 | tokenizer::iterator tok_iter1 = tokens1.begin(); | 
|---|
|  | 43 | tokenizer::iterator tok_iter2 = tokens2.begin(); | 
|---|
|  | 44 | ++tok_iter1; | 
|---|
|  | 45 | ++tok_iter2; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | std::string element1(*tok_iter1++); | 
|---|
|  | 48 | std::string element2(*tok_iter2++); | 
|---|
|  | 49 | int elementno1 = toInt(element1.substr(4,string::npos)); | 
|---|
|  | 50 | int elementno2 = toInt(element2.substr(4,string::npos)); | 
|---|
|  | 51 | if (elementno1 != elementno2) | 
|---|
|  | 52 | return elementno1 < elementno2; | 
|---|
| [88104f] | 53 | else { | 
|---|
| [6a465e] | 54 | std::string atom1(*tok_iter1); | 
|---|
|  | 55 | std::string atom2(*tok_iter2); | 
|---|
|  | 56 | int atomno1 = toInt(atom1); | 
|---|
|  | 57 | int atomno2 = toInt(atom2); | 
|---|
|  | 58 | return atomno1 < atomno2; | 
|---|
| [88104f] | 59 | } | 
|---|
| [6a465e] | 60 |  | 
|---|
|  | 61 | //    char number1[8]; | 
|---|
|  | 62 | //    char number2[8]; | 
|---|
|  | 63 | //    const char *dummy1 = s1.c_str(); | 
|---|
|  | 64 | //    const char *dummy2 = s2.c_str(); | 
|---|
|  | 65 | //    //Log() << Verbose(0) << s1 << "  " << s2 << endl; | 
|---|
|  | 66 | //    dummy1 = strchr(s1, '_')+sizeof(char)*5;  // go just after "Ion_Type" | 
|---|
|  | 67 | //    dummy2 = strchr(dummy1, '_'); | 
|---|
|  | 68 | //    strncpy(number1, dummy1, dummy2-dummy1); // copy the number | 
|---|
|  | 69 | //    number1[dummy2-dummy1]='\0'; | 
|---|
|  | 70 | //    dummy1 = strchr(s2, '_')+sizeof(char)*5;  // go just after "Ion_Type" | 
|---|
|  | 71 | //    dummy2 = strchr(dummy1, '_'); | 
|---|
|  | 72 | //    strncpy(number2, dummy1, dummy2-dummy1); // copy the number | 
|---|
|  | 73 | //    number2[dummy2-dummy1]='\0'; | 
|---|
|  | 74 | //    if (atoi(number1) != atoi(number2)) | 
|---|
|  | 75 | //      return (atoi(number1) < atoi(number2)); | 
|---|
|  | 76 | //    else { | 
|---|
|  | 77 | //      dummy1 = strchr(s1, '_')+sizeof(char); | 
|---|
|  | 78 | //      dummy1 = strchr(dummy1, '_')+sizeof(char); | 
|---|
|  | 79 | //      dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t'); | 
|---|
|  | 80 | //      strncpy(number1, dummy1, dummy2-dummy1); // copy the number | 
|---|
|  | 81 | //      number1[dummy2-dummy1]='\0'; | 
|---|
|  | 82 | //      dummy1 = strchr(s2, '_')+sizeof(char); | 
|---|
|  | 83 | //      dummy1 = strchr(dummy1, '_')+sizeof(char); | 
|---|
|  | 84 | //      dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t'); | 
|---|
|  | 85 | //      strncpy(number2, dummy1, dummy2-dummy1); // copy the number | 
|---|
|  | 86 | //      number2[dummy2-dummy1]='\0'; | 
|---|
|  | 87 | //      return (atoi(number1) < atoi(number2)); | 
|---|
|  | 88 | //    } | 
|---|
| [88104f] | 89 | } | 
|---|
| [6a465e] | 90 |  | 
|---|
|  | 91 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer; | 
|---|
| [88104f] | 92 | }; | 
|---|
|  | 93 |  | 
|---|
| [6a465e] | 94 |  | 
|---|
| [88104f] | 95 | /** Constructor for ConfigFileBuffer class. | 
|---|
|  | 96 | */ | 
|---|
| [97b825] | 97 | ConfigFileBuffer::ConfigFileBuffer() : | 
|---|
|  | 98 | buffer(NULL), | 
|---|
|  | 99 | LineMapping(NULL), | 
|---|
|  | 100 | CurrentLine(0), | 
|---|
|  | 101 | NoLines(0) | 
|---|
| [88104f] | 102 | { | 
|---|
|  | 103 | }; | 
|---|
|  | 104 |  | 
|---|
|  | 105 | /** Constructor for ConfigFileBuffer class with filename to be parsed. | 
|---|
|  | 106 | * \param *filename file name | 
|---|
|  | 107 | */ | 
|---|
| [97b825] | 108 | ConfigFileBuffer::ConfigFileBuffer(const char * const filename) : | 
|---|
|  | 109 | buffer(NULL), | 
|---|
|  | 110 | LineMapping(NULL), | 
|---|
|  | 111 | CurrentLine(0), | 
|---|
|  | 112 | NoLines(0) | 
|---|
| [a3fded] | 113 | { | 
|---|
|  | 114 | InitFileBuffer(filename); | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | void ConfigFileBuffer::InitFileBuffer(const char * const filename) | 
|---|
| [88104f] | 118 | { | 
|---|
| [43dad6] | 119 | ifstream *file= new ifstream(filename); | 
|---|
|  | 120 | InitFileBuffer(file); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | void ConfigFileBuffer::InitFileBuffer(istream *file) | 
|---|
|  | 124 | { | 
|---|
| [88104f] | 125 | char line[MAXSTRINGSIZE]; | 
|---|
|  | 126 |  | 
|---|
| [a3fded] | 127 | RemoveMapping(); | 
|---|
|  | 128 |  | 
|---|
| [88104f] | 129 | // prescan number of lines | 
|---|
| [43dad6] | 130 | if (file->fail()) { | 
|---|
|  | 131 | DoeLog(1) && (eLog()<< Verbose(1) << "config file missing!" << endl); | 
|---|
| [88104f] | 132 | return; | 
|---|
|  | 133 | } | 
|---|
|  | 134 | NoLines = 0; // we're overcounting by one | 
|---|
|  | 135 | long file_position = file->tellg(); // mark current position | 
|---|
|  | 136 | do { | 
|---|
| [1b2d30] | 137 | file->getline(line, MAXSTRINGSIZE-1); | 
|---|
| [88104f] | 138 | NoLines++; | 
|---|
|  | 139 | } while (!file->eof()); | 
|---|
|  | 140 | file->clear(); | 
|---|
|  | 141 | file->seekg(file_position, ios::beg); | 
|---|
|  | 142 | DoLog(1) && (Log() << Verbose(1) << NoLines-1 << " lines were recognized." << endl); | 
|---|
|  | 143 |  | 
|---|
|  | 144 | // allocate buffer's 1st dimension | 
|---|
|  | 145 | if (buffer != NULL) { | 
|---|
|  | 146 | DoeLog(1) && (eLog()<< Verbose(1) << "FileBuffer->buffer is not NULL!" << endl); | 
|---|
|  | 147 | return; | 
|---|
|  | 148 | } else | 
|---|
|  | 149 | buffer = new char *[NoLines]; | 
|---|
|  | 150 |  | 
|---|
|  | 151 | // scan each line and put into buffer | 
|---|
|  | 152 | int lines=0; | 
|---|
|  | 153 | int i; | 
|---|
|  | 154 | do { | 
|---|
|  | 155 | buffer[lines] = new char[MAXSTRINGSIZE]; | 
|---|
|  | 156 | file->getline(buffer[lines], MAXSTRINGSIZE-1); | 
|---|
|  | 157 | i = strlen(buffer[lines]); | 
|---|
|  | 158 | buffer[lines][i] = '\n'; | 
|---|
|  | 159 | buffer[lines][i+1] = '\0'; | 
|---|
|  | 160 | lines++; | 
|---|
|  | 161 | } while((!file->eof()) && (lines < NoLines)); | 
|---|
|  | 162 | DoLog(1) && (Log() << Verbose(1) << lines-1 << " lines were read into the buffer." << endl); | 
|---|
| [1b2d30] | 163 | file->clear(); | 
|---|
|  | 164 | file->seekg(file_position, ios::beg); | 
|---|
| [88104f] | 165 |  | 
|---|
| [a3fded] | 166 | InitMapping(); | 
|---|
| [88104f] | 167 | } | 
|---|
|  | 168 |  | 
|---|
|  | 169 | /** Destructor for ConfigFileBuffer class. | 
|---|
|  | 170 | */ | 
|---|
|  | 171 | ConfigFileBuffer::~ConfigFileBuffer() | 
|---|
|  | 172 | { | 
|---|
| [a3fded] | 173 | RemoveBuffer(); | 
|---|
|  | 174 | RemoveMapping(); | 
|---|
| [88104f] | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 |  | 
|---|
|  | 178 | /** Create trivial mapping. | 
|---|
|  | 179 | */ | 
|---|
|  | 180 | void ConfigFileBuffer::InitMapping() | 
|---|
|  | 181 | { | 
|---|
|  | 182 | LineMapping = new int[NoLines]; | 
|---|
|  | 183 | for (int i=0;i<NoLines;i++) | 
|---|
|  | 184 | LineMapping[i] = i; | 
|---|
| [a3fded] | 185 | MappingAllocated = true; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | /** Remove allocated mapping. | 
|---|
|  | 189 | */ | 
|---|
|  | 190 | void ConfigFileBuffer::RemoveMapping() | 
|---|
|  | 191 | { | 
|---|
|  | 192 | delete[](LineMapping); | 
|---|
|  | 193 | MappingAllocated = false; | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
|  | 196 | /** Remove allocated mapping. | 
|---|
|  | 197 | */ | 
|---|
|  | 198 | void ConfigFileBuffer::RemoveBuffer() | 
|---|
|  | 199 | { | 
|---|
|  | 200 | for(int i=0;i<NoLines;++i) | 
|---|
|  | 201 | delete[](buffer[i]); | 
|---|
|  | 202 | delete[](buffer); | 
|---|
| [88104f] | 203 | } | 
|---|
|  | 204 |  | 
|---|
| [a3fded] | 205 |  | 
|---|
| [88104f] | 206 | /** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted. | 
|---|
|  | 207 | * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the | 
|---|
|  | 208 | * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine | 
|---|
|  | 209 | * points to first Ion_Type entry. | 
|---|
|  | 210 | * \param *FileBuffer pointer to buffer structure | 
|---|
|  | 211 | * \param NoAtoms of subsequent lines to look at | 
|---|
|  | 212 | */ | 
|---|
|  | 213 | void ConfigFileBuffer::MapIonTypesInBuffer(const int NoAtoms) | 
|---|
|  | 214 | { | 
|---|
| [6a465e] | 215 | std::multimap<std::string, int, IonTypeCompare> IonTypeLineMap; | 
|---|
| [a3fded] | 216 | if (!MappingAllocated) { | 
|---|
|  | 217 | InitMapping(); | 
|---|
| [88104f] | 218 | } | 
|---|
|  | 219 |  | 
|---|
| [6a465e] | 220 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
|  | 221 | tokenizer; | 
|---|
|  | 222 | boost::char_separator<char> sep("\t "); | 
|---|
|  | 223 |  | 
|---|
| [88104f] | 224 | // put all into hashed map | 
|---|
| [6a465e] | 225 | for (int i=CurrentLine; i<NoLines; ++i) { | 
|---|
|  | 226 | std::string line(buffer[i]); | 
|---|
|  | 227 | tokenizer tokens(line, sep); | 
|---|
|  | 228 | if (tokens.begin() != tokens.end()) { | 
|---|
|  | 229 | const std::string token = *tokens.begin(); | 
|---|
|  | 230 | if (token.find("Ion_Type") != string::npos) { | 
|---|
|  | 231 | IonTypeLineMap.insert(pair<std::string, int> (token, i)); | 
|---|
|  | 232 | } | 
|---|
|  | 233 | } | 
|---|
| [88104f] | 234 | } | 
|---|
|  | 235 |  | 
|---|
| [6a465e] | 236 | // fill map (aka IonType1_1, IonType1_1, IonType1_1, IonType1_2, IonType1_2, IonType1_2, ... | 
|---|
|  | 237 | // ..., IonType2_1, IonType2_1, IonType2_1, ...) | 
|---|
| [88104f] | 238 | int nr=0; | 
|---|
| [6a465e] | 239 | for (map<std::string, int, IonTypeCompare>::iterator runner = IonTypeLineMap.begin(); runner != IonTypeLineMap.end(); ++runner) { | 
|---|
| [88104f] | 240 | if (CurrentLine+nr < NoLines) | 
|---|
|  | 241 | LineMapping[CurrentLine+(nr++)] = runner->second; | 
|---|
|  | 242 | else { | 
|---|
| [6a465e] | 243 | DoeLog(0) && (eLog()<< Verbose(0) << "config::MapIonTypesInBuffer - NoLines is wrong: We are past the end of the file!" << endl); | 
|---|
| [88104f] | 244 | performCriticalExit(); | 
|---|
|  | 245 | } | 
|---|
|  | 246 | } | 
|---|
|  | 247 | } | 
|---|