| 1 | /* | 
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 | main-programmer: Benjamin Grauer | 
|---|
| 13 | co-programmer: Christian Meyer | 
|---|
| 14 |  | 
|---|
| 15 | 2005-08-14: complete reimplementation: | 
|---|
| 16 | now the File is parsed at the initialisation, | 
|---|
| 17 | and informations is gathered there. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "ini_parser.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include <stdlib.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 |  | 
|---|
| 26 | #if HAVE_CONFIG_H | 
|---|
| 27 | #include <config.h> | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | #ifdef DEBUG | 
|---|
| 31 | #include "../../../defs/debug.h" | 
|---|
| 32 | #else | 
|---|
| 33 | #define PRINTF(x) printf | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | using namespace std; | 
|---|
| 37 |  | 
|---|
| 38 | /** | 
|---|
| 39 | * @brief constructs an IniParser using a file | 
|---|
| 40 | * @param fileName: the path and name of the file to parse | 
|---|
| 41 | */ | 
|---|
| 42 | IniParser::IniParser (const std::string& fileName) | 
|---|
| 43 | { | 
|---|
| 44 | this->fileName = ""; | 
|---|
| 45 | this->comment = ""; | 
|---|
| 46 |  | 
|---|
| 47 | if (!fileName.empty()) | 
|---|
| 48 | this->readFile(fileName); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 | * @brief removes the IniParser from memory | 
|---|
| 54 | */ | 
|---|
| 55 | IniParser::~IniParser () | 
|---|
| 56 | { | 
|---|
| 57 | this->deleteSections(); | 
|---|
| 58 | this->setFileName(""); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | const std::string IniParser::emptyString = ""; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /** | 
|---|
| 65 | * @brief removes all the sections. This is like delete, but even cooler :) | 
|---|
| 66 | */ | 
|---|
| 67 | void IniParser::deleteSections() | 
|---|
| 68 | { | 
|---|
| 69 | // in all sections | 
|---|
| 70 | this->sections.clear(); | 
|---|
| 71 |  | 
|---|
| 72 | this->currentSection = this->sections.end(); | 
|---|
| 73 | this->setFileName(""); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /** | 
|---|
| 78 | * @brief sets the Name of the input-file | 
|---|
| 79 | * @param fileName The new FileName to set to the IniParser | 
|---|
| 80 | * If fileName is NULL the new Name will be set to NULL too. | 
|---|
| 81 | */ | 
|---|
| 82 | void IniParser::setFileName(const std::string& fileName) | 
|---|
| 83 | { | 
|---|
| 84 | this->comment = ""; | 
|---|
| 85 | this->fileName = fileName; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * @brief opens a file to parse | 
|---|
| 91 | * @param fileName: path and name of the new file to parse | 
|---|
| 92 | * @return true on success false otherwise; | 
|---|
| 93 | * | 
|---|
| 94 | * If there was already an opened file, the file will be closed, | 
|---|
| 95 | * and the new one will be opened. | 
|---|
| 96 | */ | 
|---|
| 97 | bool IniParser::readFile(const std::string& fileName) | 
|---|
| 98 | { | 
|---|
| 99 | FILE*    stream;           //< The stream we use to read the file. | 
|---|
| 100 | int      lineCount = 0;    //< The Count of lines. | 
|---|
| 101 |  | 
|---|
| 102 | if (!this->fileName.empty()) | 
|---|
| 103 | this->deleteSections(); | 
|---|
| 104 |  | 
|---|
| 105 | if( fileName.empty()) | 
|---|
| 106 | return false; | 
|---|
| 107 |  | 
|---|
| 108 | if( (stream = fopen (fileName.c_str(), "r")) == NULL) | 
|---|
| 109 | { | 
|---|
| 110 | PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); | 
|---|
| 111 | return false; | 
|---|
| 112 | } | 
|---|
| 113 | else | 
|---|
| 114 | { | 
|---|
| 115 | this->setFileName(fileName); | 
|---|
| 116 |  | 
|---|
| 117 | ///////////////////////////// | 
|---|
| 118 | // READING IN THE INI-FILE // | 
|---|
| 119 | ///////////////////////////// | 
|---|
| 120 | char lineBuffer[PARSELINELENGHT+1]; | 
|---|
| 121 | char buffer[PARSELINELENGHT+1]; | 
|---|
| 122 | const char* lineBegin; | 
|---|
| 123 | char* ptr; | 
|---|
| 124 |  | 
|---|
| 125 | while( fgets (lineBuffer, PARSELINELENGHT, stream)) | 
|---|
| 126 | { | 
|---|
| 127 | lineBegin = lineBuffer; | 
|---|
| 128 | // remove newline char, and \0-terminate | 
|---|
| 129 | if( (ptr = strchr( lineBuffer, '\n')) != NULL) | 
|---|
| 130 | *ptr = 0; | 
|---|
| 131 | else | 
|---|
| 132 | lineBuffer[PARSELINELENGHT] = 0; | 
|---|
| 133 | // cut up to the beginning of the line. | 
|---|
| 134 | while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer)) | 
|---|
| 135 | ++lineBegin; | 
|---|
| 136 |  | 
|---|
| 137 | if ( !strcmp( lineBegin, "" ) ) | 
|---|
| 138 | continue; | 
|---|
| 139 |  | 
|---|
| 140 | // check if we have a FileComment | 
|---|
| 141 | if ( (*lineBegin == '#' || *lineBegin == ';')) | 
|---|
| 142 | { | 
|---|
| 143 | string newCommenLine = lineBegin; | 
|---|
| 144 | this->commentList.push_back(newCommenLine); | 
|---|
| 145 | continue; | 
|---|
| 146 | } | 
|---|
| 147 | if (lineCount == 0 && !this->commentList.empty()) | 
|---|
| 148 | { | 
|---|
| 149 | this->setFileComment(); | 
|---|
| 150 | lineCount++; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | // check for section identifyer | 
|---|
| 154 | else if( sscanf (lineBegin, "[%s", buffer) == 1) | 
|---|
| 155 | { | 
|---|
| 156 | if( (ptr = strchr( buffer, ']')) != NULL) | 
|---|
| 157 | { | 
|---|
| 158 | *ptr = 0; | 
|---|
| 159 | this->addSection(buffer); | 
|---|
| 160 | this->setSectionComment(); | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | // check for Entry identifier (Entry = Value) | 
|---|
| 164 | else if( (ptr = strchr( lineBegin, '=')) != NULL) | 
|---|
| 165 | { | 
|---|
| 166 | if (currentSection == NULL) | 
|---|
| 167 | { | 
|---|
| 168 | PRINTF(2)("Not in a Section yet for %s\n", lineBegin); | 
|---|
| 169 | lineCount++; | 
|---|
| 170 | continue; | 
|---|
| 171 | } | 
|---|
| 172 | if( ptr == lineBegin) | 
|---|
| 173 | { | 
|---|
| 174 | lineCount++; | 
|---|
| 175 | continue; | 
|---|
| 176 | } | 
|---|
| 177 | char* valueBegin = ptr+1; | 
|---|
| 178 | while ((*valueBegin == ' ' || *valueBegin == '\t') && valueBegin <= lineBegin + strlen(lineBegin)) | 
|---|
| 179 | ++valueBegin; | 
|---|
| 180 | char* valueEnd = valueBegin + strlen(valueBegin)-1; | 
|---|
| 181 | while ((*valueEnd == ' ' || *valueEnd == '\t') && valueEnd >= valueBegin) | 
|---|
| 182 | --valueEnd; | 
|---|
| 183 | valueEnd[1] = '\0'; | 
|---|
| 184 | char* nameEnd = ptr-1; | 
|---|
| 185 | while ((*nameEnd == ' ' || *nameEnd == '\t' ) && nameEnd >= lineBegin) | 
|---|
| 186 | --nameEnd; | 
|---|
| 187 | nameEnd[1] = '\0'; | 
|---|
| 188 |  | 
|---|
| 189 | this->addVar(lineBegin, valueBegin); | 
|---|
| 190 | this->setEntryComment(); | 
|---|
| 191 |  | 
|---|
| 192 | lineCount++; | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 | this->currentSection = this->sections.begin(); | 
|---|
| 197 | if (!this->sections.empty()) | 
|---|
| 198 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 199 |  | 
|---|
| 200 | fclose(stream); | 
|---|
| 201 | return true; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | /** | 
|---|
| 206 | * @brief opens a file and writes to it | 
|---|
| 207 | * @param fileName: path and name of the new file to write to | 
|---|
| 208 | * @return true on success false otherwise | 
|---|
| 209 | */ | 
|---|
| 210 | bool IniParser::writeFile(const std::string& fileName) const | 
|---|
| 211 | { | 
|---|
| 212 | FILE*    stream;           //!< The stream we use to read the file. | 
|---|
| 213 | if( fileName.empty()) | 
|---|
| 214 | return false; | 
|---|
| 215 |  | 
|---|
| 216 | if( (stream = fopen (fileName.c_str(), "w")) == NULL) | 
|---|
| 217 | { | 
|---|
| 218 | PRINTF(1)("IniParser could not open %s\n", fileName.c_str()); | 
|---|
| 219 | return false; | 
|---|
| 220 | } | 
|---|
| 221 | else | 
|---|
| 222 | { | 
|---|
| 223 | if (!this->comment.empty()) | 
|---|
| 224 | fprintf(stream, "%s\n\n", this->comment.c_str()); | 
|---|
| 225 |  | 
|---|
| 226 | std::list<IniSection>::const_iterator section; | 
|---|
| 227 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 228 | { | 
|---|
| 229 | if (!(*section).comment.empty()) | 
|---|
| 230 | fprintf(stream, "%s", (*section).comment.c_str()); | 
|---|
| 231 | fprintf(stream, "\n [%s]\n", (*section).name.c_str()); | 
|---|
| 232 |  | 
|---|
| 233 | std::list<IniEntry>::const_iterator entry; | 
|---|
| 234 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) | 
|---|
| 235 | { | 
|---|
| 236 | if (!(*entry).comment.empty()) | 
|---|
| 237 | fprintf(stream, "%s", (*entry).comment.c_str()); | 
|---|
| 238 | fprintf(stream, "   %s = %s\n", (*entry).name.c_str(), (*entry).value.c_str()); | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|
| 242 | fclose(stream); | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | void IniParser::setFileComment(const std::string& fileComment) | 
|---|
| 246 | { | 
|---|
| 247 | this->comment = fileComment; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | /** | 
|---|
| 251 | * @brief adds a section to the list of Sections, | 
|---|
| 252 | * if no Section list is availiable, it will create it | 
|---|
| 253 | * @param sectionName the Name of the section to add | 
|---|
| 254 | * @return true on success... there is only success or segfault :) | 
|---|
| 255 | */ | 
|---|
| 256 | bool IniParser::addSection(const std::string& sectionName) | 
|---|
| 257 | { | 
|---|
| 258 | if (sectionName.empty()) | 
|---|
| 259 | return false; | 
|---|
| 260 | IniSection newSection; | 
|---|
| 261 | newSection.name = sectionName; | 
|---|
| 262 | newSection.comment = ""; | 
|---|
| 263 |  | 
|---|
| 264 | this->sections.push_back(newSection); | 
|---|
| 265 |  | 
|---|
| 266 | this->currentSection = --this->sections.end(); | 
|---|
| 267 | if (!this->sections.empty()) | 
|---|
| 268 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 269 | PRINTF(5)("Added Section %s\n", sectionName.c_str()); | 
|---|
| 270 | return true; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 |  | 
|---|
| 274 | /** | 
|---|
| 275 | * @brief Set the parsing cursor to the specified section | 
|---|
| 276 | * @param sectionName: the name of the section to set the cursor to | 
|---|
| 277 | * @return true on success or false if the section could not be found | 
|---|
| 278 | */ | 
|---|
| 279 | bool IniParser::getSection(const std::string& sectionName) | 
|---|
| 280 | { | 
|---|
| 281 | this->currentSection = this->getSectionIT(sectionName); | 
|---|
| 282 | if (this->currentSection != this->sections.end()) | 
|---|
| 283 | { | 
|---|
| 284 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 285 | return true; | 
|---|
| 286 | } | 
|---|
| 287 | else | 
|---|
| 288 | return false; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | /** | 
|---|
| 292 | * | 
|---|
| 293 | */ | 
|---|
| 294 | void IniParser::setSectionComment(const std::string& comment, const std::string& sectionName) | 
|---|
| 295 | { | 
|---|
| 296 | std::list<IniSection>::iterator section = this->getSectionIT(sectionName); | 
|---|
| 297 | if (section == this->sections.end()) | 
|---|
| 298 | return; | 
|---|
| 299 |  | 
|---|
| 300 | (*section).comment = comment; | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | /** | 
|---|
| 304 | * @param sectionName the Section to query for | 
|---|
| 305 | * @returns the Comment, or NULL on error. | 
|---|
| 306 | */ | 
|---|
| 307 | const std::string& IniParser::getSectionComment(const std::string& sectionName) const | 
|---|
| 308 | { | 
|---|
| 309 | std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); | 
|---|
| 310 | if (section != this->sections.end()) | 
|---|
| 311 | return (*section).comment; | 
|---|
| 312 | else | 
|---|
| 313 | return IniParser::emptyString; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 |  | 
|---|
| 317 | /** | 
|---|
| 318 | * @brief moves to the first section | 
|---|
| 319 | */ | 
|---|
| 320 | void IniParser::firstSection() | 
|---|
| 321 | { | 
|---|
| 322 | this->currentSection = this->sections.begin(); | 
|---|
| 323 | if (!this->sections.empty()) | 
|---|
| 324 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 |  | 
|---|
| 328 | /** | 
|---|
| 329 | * @brief searches the next section | 
|---|
| 330 | * @returns the name of the section if found, NULL otherwise | 
|---|
| 331 | */ | 
|---|
| 332 | const std::string& IniParser::nextSection() | 
|---|
| 333 | { | 
|---|
| 334 | if (this->currentSection == this->sections.end()) | 
|---|
| 335 | return IniParser::emptyString; | 
|---|
| 336 |  | 
|---|
| 337 | this->currentSection++; | 
|---|
| 338 |  | 
|---|
| 339 | if (this->currentSection != this->sections.end()) | 
|---|
| 340 | { | 
|---|
| 341 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 342 | return this->currentSection->name; | 
|---|
| 343 | } | 
|---|
| 344 | else | 
|---|
| 345 | return IniParser::emptyString; | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 |  | 
|---|
| 349 | /** | 
|---|
| 350 | * @brief adds a new Entry to either the currentSection or the section called by sectionName | 
|---|
| 351 | * @param entryName the Name of the Entry to add | 
|---|
| 352 | * @param value the value to assign to this entry | 
|---|
| 353 | * @param sectionName if NULL then this entry will be set to the currentSection | 
|---|
| 354 | * otherwise to the section refered to by sectionName. | 
|---|
| 355 | * If both are NULL no entry will be added | 
|---|
| 356 | * @return true if everything is ok false on error | 
|---|
| 357 | */ | 
|---|
| 358 | bool IniParser::addVar(const std::string& entryName, const std::string& value, const std::string& sectionName) | 
|---|
| 359 | { | 
|---|
| 360 | std::list<IniSection>::iterator section; | 
|---|
| 361 |  | 
|---|
| 362 | if (!sectionName.empty()) | 
|---|
| 363 | { | 
|---|
| 364 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 365 | if ((*section).name == sectionName) | 
|---|
| 366 | break; | 
|---|
| 367 | } | 
|---|
| 368 | else | 
|---|
| 369 | section = this->currentSection; | 
|---|
| 370 |  | 
|---|
| 371 | if (section == this->sections.end()) | 
|---|
| 372 | return false; | 
|---|
| 373 |  | 
|---|
| 374 | if (section == this->sections.end()) | 
|---|
| 375 | { | 
|---|
| 376 | PRINTF(2)("section '%s' not found for value '%s'\n", sectionName.c_str(), entryName.c_str()); | 
|---|
| 377 | return false; | 
|---|
| 378 | } | 
|---|
| 379 | else | 
|---|
| 380 | { | 
|---|
| 381 | (*section).entries.push_back(IniEntry()); | 
|---|
| 382 | (*section).entries.back().comment = ""; | 
|---|
| 383 | (*section).entries.back().name = entryName; | 
|---|
| 384 | (*section).entries.back().value = value; | 
|---|
| 385 | PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", | 
|---|
| 386 | (*section).entries.back().name.c_str(), | 
|---|
| 387 | (*section).entries.back().value.c_str(), | 
|---|
| 388 | (*section).name.c_str()); | 
|---|
| 389 | this->currentEntry = --(*section).entries.end(); | 
|---|
| 390 | return true; | 
|---|
| 391 | } | 
|---|
| 392 | } | 
|---|
| 393 |  | 
|---|
| 394 | /** | 
|---|
| 395 | * @brief edits the entry speciefied by entryName in sectionName/currentSection or creates it if it doesn't exist | 
|---|
| 396 | * @param entryName the Name of the Entry to add | 
|---|
| 397 | * @param value the value to assign to this entry | 
|---|
| 398 | * @param sectionName if NULL then this entry will be set to the currentSection | 
|---|
| 399 | * otherwise to the section refered to by sectionName. | 
|---|
| 400 | * If both are NULL no entry will be added | 
|---|
| 401 | * @return true if everything is ok false on error | 
|---|
| 402 | */ | 
|---|
| 403 | bool IniParser::editVar(const std::string& entryName, const std::string& value, const std::string& sectionName) | 
|---|
| 404 | { | 
|---|
| 405 | std::list<IniSection>::iterator section; | 
|---|
| 406 |  | 
|---|
| 407 | if (!sectionName.empty()) | 
|---|
| 408 | { | 
|---|
| 409 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 410 | if ((*section).name == sectionName) | 
|---|
| 411 | break; | 
|---|
| 412 | } | 
|---|
| 413 | else | 
|---|
| 414 | section = this->currentSection; | 
|---|
| 415 |  | 
|---|
| 416 | if (section == this->sections.end()) | 
|---|
| 417 | { | 
|---|
| 418 | IniSection sec; | 
|---|
| 419 | sec.comment = ""; | 
|---|
| 420 | sec.name = sectionName; | 
|---|
| 421 | section = this->sections.insert(this->sections.end(), sec); | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | if (section == this->sections.end()) | 
|---|
| 425 | { | 
|---|
| 426 | PRINTF(2)("section '%s' not found for value '%s'\n", sectionName.c_str(), entryName.c_str()); | 
|---|
| 427 | return false; | 
|---|
| 428 | } | 
|---|
| 429 | else | 
|---|
| 430 | { | 
|---|
| 431 | //try find item | 
|---|
| 432 | std::list<IniEntry>::iterator entry; | 
|---|
| 433 | for (entry = section->entries.begin(); entry!=section->entries.end(); entry++) | 
|---|
| 434 | if (entry->name == entryName ) | 
|---|
| 435 | break; | 
|---|
| 436 |  | 
|---|
| 437 | //found it? | 
|---|
| 438 | if ( entry != section->entries.end() ) | 
|---|
| 439 | { | 
|---|
| 440 | entry->value = value; | 
|---|
| 441 |  | 
|---|
| 442 | return true; | 
|---|
| 443 | } | 
|---|
| 444 |  | 
|---|
| 445 | //not found -> create it | 
|---|
| 446 | (*section).entries.push_back(IniEntry()); | 
|---|
| 447 | (*section).entries.back().comment = ""; | 
|---|
| 448 | (*section).entries.back().name = entryName; | 
|---|
| 449 | (*section).entries.back().value = value; | 
|---|
| 450 | PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", | 
|---|
| 451 | (*section).entries.back().name.c_str(), | 
|---|
| 452 | (*section).entries.back().value.c_str(), | 
|---|
| 453 | (*section).name); | 
|---|
| 454 | this->currentEntry = --(*section).entries.end(); | 
|---|
| 455 | return true; | 
|---|
| 456 | } | 
|---|
| 457 | } | 
|---|
| 458 |  | 
|---|
| 459 |  | 
|---|
| 460 | /** | 
|---|
| 461 | * @brief directly acesses an entry in a section | 
|---|
| 462 | * @param entryName: the name of the entry to find | 
|---|
| 463 | * @param sectionName: the section where the entry is to be found | 
|---|
| 464 | * @param defaultValue: what should be returned in case the entry cannot be found | 
|---|
| 465 | * @return a pointer to a buffer conatining the value of the specified entry. This buffer will contain the data specified in defvalue in case the entry wasn't found | 
|---|
| 466 | * | 
|---|
| 467 | *  The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly | 
|---|
| 468 | * lead to unwanted behaviour. | 
|---|
| 469 | */ | 
|---|
| 470 | const std::string& IniParser::getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue) const | 
|---|
| 471 | { | 
|---|
| 472 | if (!this->fileName.empty()) | 
|---|
| 473 | { | 
|---|
| 474 | std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); | 
|---|
| 475 | if (entry != NULL &&  (*entry).name == entryName) | 
|---|
| 476 | return (*entry).value; | 
|---|
| 477 | PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName.c_str(), sectionName.c_str()); | 
|---|
| 478 | } | 
|---|
| 479 | else | 
|---|
| 480 | PRINTF(2)("no File opened\n"); | 
|---|
| 481 |  | 
|---|
| 482 | return defaultValue; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | /** | 
|---|
| 486 | * Set the Comment of a specified Entry. | 
|---|
| 487 | * @param comment the Comment to set | 
|---|
| 488 | * @param entryName the Name of the Entry | 
|---|
| 489 | * @param sectionName the Name of the Section | 
|---|
| 490 | */ | 
|---|
| 491 | void IniParser::setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName) | 
|---|
| 492 | { | 
|---|
| 493 | std::list<IniEntry>::iterator entry = this->getEntryIT(entryName, sectionName); | 
|---|
| 494 | (*entry).comment = comment; | 
|---|
| 495 | } | 
|---|
| 496 |  | 
|---|
| 497 | /** | 
|---|
| 498 | * @param entryName the Entry to query for | 
|---|
| 499 | * @param sectionName the Section to Query for | 
|---|
| 500 | * @returns the queried Comment. | 
|---|
| 501 | */ | 
|---|
| 502 | const std::string& IniParser::getEntryComment(const std::string& entryName, const std::string& sectionName) const | 
|---|
| 503 | { | 
|---|
| 504 | std::list<IniEntry>::const_iterator entry = this->getEntryIT(entryName, sectionName); | 
|---|
| 505 |  | 
|---|
| 506 | return (*entry).comment; | 
|---|
| 507 | } | 
|---|
| 508 |  | 
|---|
| 509 |  | 
|---|
| 510 | /** | 
|---|
| 511 | * @brief moves to the first Variable of the current Section | 
|---|
| 512 | */ | 
|---|
| 513 | void IniParser::firstVar() | 
|---|
| 514 | { | 
|---|
| 515 | if (!this->sections.empty() && | 
|---|
| 516 | this->currentSection != this->sections.end()) | 
|---|
| 517 | this->currentEntry = (*this->currentSection).entries.begin(); | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 |  | 
|---|
| 521 | /** | 
|---|
| 522 | * @brief gets the next VarName = VarValue pair from the parsing stream | 
|---|
| 523 | * @return true on success, false otherwise (in the latter case name and value will be NULL) | 
|---|
| 524 | */ | 
|---|
| 525 | bool IniParser::nextVar() | 
|---|
| 526 | { | 
|---|
| 527 | if ( this->sections.empty() | 
|---|
| 528 | || this->currentSection == this->sections.end() | 
|---|
| 529 | || this->currentEntry == (*this->currentSection).entries.end()) | 
|---|
| 530 | return false; | 
|---|
| 531 |  | 
|---|
| 532 | this->currentEntry++; | 
|---|
| 533 |  | 
|---|
| 534 | if (this->currentEntry == (*this->currentSection).entries.end()) | 
|---|
| 535 | return false; | 
|---|
| 536 | else | 
|---|
| 537 | return true; | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 |  | 
|---|
| 541 |  | 
|---|
| 542 | /** | 
|---|
| 543 | * @returns the name of the Current selected Section | 
|---|
| 544 | */ | 
|---|
| 545 | const std::string& IniParser::getCurrentSection() const | 
|---|
| 546 | { | 
|---|
| 547 | if (!this->sections.empty() && | 
|---|
| 548 | this->currentSection != this->sections.end()) | 
|---|
| 549 | return this->currentSection->name; | 
|---|
| 550 | else | 
|---|
| 551 | return IniParser::emptyString ; | 
|---|
| 552 | } | 
|---|
| 553 |  | 
|---|
| 554 |  | 
|---|
| 555 | /** | 
|---|
| 556 | * @returns the current entries Name, or NULL if we havn't selected a Entry | 
|---|
| 557 | */ | 
|---|
| 558 | const std::string& IniParser::getCurrentName() const | 
|---|
| 559 | { | 
|---|
| 560 | if (!this->sections.empty() && | 
|---|
| 561 | this->currentSection != this->sections.end() && | 
|---|
| 562 | this->currentEntry != (*this->currentSection).entries.end()) | 
|---|
| 563 | return (*this->currentEntry).name; | 
|---|
| 564 | else | 
|---|
| 565 | return emptyString; | 
|---|
| 566 | } | 
|---|
| 567 |  | 
|---|
| 568 | /** | 
|---|
| 569 | * @returns the current entries Value, or NULL if we havn't selected a Entry | 
|---|
| 570 | */ | 
|---|
| 571 | const std::string& IniParser::getCurrentValue() const | 
|---|
| 572 | { | 
|---|
| 573 | if (!this->sections.empty() && | 
|---|
| 574 | this->currentSection != this->sections.end() && | 
|---|
| 575 | this->currentEntry != (*this->currentSection).entries.end()) | 
|---|
| 576 | return (*this->currentEntry).value; | 
|---|
| 577 | else | 
|---|
| 578 | return IniParser::emptyString; | 
|---|
| 579 | } | 
|---|
| 580 |  | 
|---|
| 581 |  | 
|---|
| 582 | /** | 
|---|
| 583 | * Finds the Section Iterator of the Section Called sectionName | 
|---|
| 584 | * @param sectionName the Name of the Section to get the Iterator from | 
|---|
| 585 | */ | 
|---|
| 586 | std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const std::string& sectionName) const | 
|---|
| 587 | { | 
|---|
| 588 | std::list<IniSection>::const_iterator section = this->currentSection; | 
|---|
| 589 | if (sectionName.empty()) | 
|---|
| 590 | return this->currentSection; | 
|---|
| 591 | else | 
|---|
| 592 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 593 | if ((*section).name == sectionName) | 
|---|
| 594 | break; | 
|---|
| 595 | return section; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 |  | 
|---|
| 599 | /** | 
|---|
| 600 | * Finds the Section Iterator of the Section Called sectionName | 
|---|
| 601 | * @param sectionName the Name of the Section to get the Iterator from | 
|---|
| 602 | */ | 
|---|
| 603 | std::list<IniParser::IniSection>::iterator IniParser::getSectionIT(const std::string& sectionName) | 
|---|
| 604 | { | 
|---|
| 605 | std::list<IniSection>::iterator section = this->currentSection; | 
|---|
| 606 | if (sectionName.empty()) | 
|---|
| 607 | return this->currentSection; | 
|---|
| 608 | else | 
|---|
| 609 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 610 | if ((*section).name == sectionName) | 
|---|
| 611 | break; | 
|---|
| 612 | return section; | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 |  | 
|---|
| 616 | /** | 
|---|
| 617 | * Finds the Entry Iterator of the Section Called sectionName and entry called EntryName | 
|---|
| 618 | * @param entryName the Name of the Entry to get the Iterator from | 
|---|
| 619 | * @param sectionName the Name of the Section to get the Iterator from | 
|---|
| 620 | */ | 
|---|
| 621 | std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) const | 
|---|
| 622 | { | 
|---|
| 623 | if (entryName.empty()) | 
|---|
| 624 | return this->currentEntry; | 
|---|
| 625 | std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName); | 
|---|
| 626 | std::list<IniEntry>::const_iterator entry = this->currentEntry; | 
|---|
| 627 |  | 
|---|
| 628 | if (section != this->sections.end()) | 
|---|
| 629 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) | 
|---|
| 630 | if ((*entry).name == entryName) | 
|---|
| 631 | break; | 
|---|
| 632 | if (entry == (*section).entries.end()) | 
|---|
| 633 | return NULL; | 
|---|
| 634 | else | 
|---|
| 635 | return entry; | 
|---|
| 636 | } | 
|---|
| 637 |  | 
|---|
| 638 |  | 
|---|
| 639 | /** | 
|---|
| 640 | * Finds the Entry Iterator of the Section Called sectionName and entry called EntryName | 
|---|
| 641 | * @param entryName the Name of the Entry to get the Iterator from | 
|---|
| 642 | * @param sectionName the Name of the Section to get the Iterator from | 
|---|
| 643 | */ | 
|---|
| 644 | std::list<IniParser::IniEntry>::iterator IniParser::getEntryIT(const std::string& entryName, const std::string& sectionName) | 
|---|
| 645 | { | 
|---|
| 646 | if (entryName.empty()) | 
|---|
| 647 | return this->currentEntry; | 
|---|
| 648 | std::list<IniSection>::iterator section = this->getSectionIT(sectionName); | 
|---|
| 649 | std::list<IniEntry>::iterator entry = this->currentEntry; | 
|---|
| 650 |  | 
|---|
| 651 | if (section != this->sections.end()) | 
|---|
| 652 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) | 
|---|
| 653 | if ((*entry).name == entryName) | 
|---|
| 654 | break; | 
|---|
| 655 | if (entry == (*section).entries.end()) | 
|---|
| 656 | return NULL; | 
|---|
| 657 | else | 
|---|
| 658 | return entry; | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 |  | 
|---|
| 662 | /** | 
|---|
| 663 | * takes lines together to form one FileComment, ereasing the commentList | 
|---|
| 664 | */ | 
|---|
| 665 | void IniParser::setFileComment() | 
|---|
| 666 | { | 
|---|
| 667 | if (this->commentList.empty()) | 
|---|
| 668 | { | 
|---|
| 669 | this->comment = ""; | 
|---|
| 670 | return; | 
|---|
| 671 | } | 
|---|
| 672 |  | 
|---|
| 673 | std::list<char*>::iterator comment; | 
|---|
| 674 |  | 
|---|
| 675 | while (!this->commentList.empty()) | 
|---|
| 676 | { | 
|---|
| 677 | if (this->comment[0] != '\0') | 
|---|
| 678 | this->comment += "\n"; | 
|---|
| 679 | this->comment += this->commentList.front(); | 
|---|
| 680 | this->commentList.pop_front(); | 
|---|
| 681 | } | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | /** | 
|---|
| 685 | * takes lines together to form one SectionComment, ereasing the commentList | 
|---|
| 686 | */ | 
|---|
| 687 | void IniParser::setSectionComment() | 
|---|
| 688 | { | 
|---|
| 689 | (*this->currentSection).comment = ""; | 
|---|
| 690 |  | 
|---|
| 691 | if (this->commentList.empty()) | 
|---|
| 692 | return; | 
|---|
| 693 |  | 
|---|
| 694 | while (!this->commentList.empty()) | 
|---|
| 695 | { | 
|---|
| 696 | if ((*this->currentSection).comment[0] != '\0') | 
|---|
| 697 | (*this->currentSection).comment += "\n"; | 
|---|
| 698 | (*this->currentSection).comment += this->commentList.front(); | 
|---|
| 699 | this->commentList.pop_front(); | 
|---|
| 700 | } | 
|---|
| 701 | } | 
|---|
| 702 |  | 
|---|
| 703 | /** | 
|---|
| 704 | * takes lines together to form one EntryComment, ereasing the commentList | 
|---|
| 705 | */ | 
|---|
| 706 | void IniParser::setEntryComment() | 
|---|
| 707 | { | 
|---|
| 708 | (*this->currentEntry).comment = ""; | 
|---|
| 709 | if (this->commentList.empty()) | 
|---|
| 710 | return; | 
|---|
| 711 |  | 
|---|
| 712 | while (!this->commentList.empty()) | 
|---|
| 713 | { | 
|---|
| 714 | if ((*this->currentEntry).comment[0] != '\0') | 
|---|
| 715 | (*this->currentEntry).comment += "\n"; | 
|---|
| 716 | (*this->currentEntry).comment += this->commentList.front(); | 
|---|
| 717 | this->commentList.pop_front(); | 
|---|
| 718 | } | 
|---|
| 719 | } | 
|---|
| 720 |  | 
|---|
| 721 |  | 
|---|
| 722 | /** | 
|---|
| 723 | * @brief output the whole tree in a nice and easy way. | 
|---|
| 724 | */ | 
|---|
| 725 | void IniParser::debug() const | 
|---|
| 726 | { | 
|---|
| 727 | PRINTF(0)("Iniparser %s - debug\n", this->fileName.c_str()); | 
|---|
| 728 | if (!this->comment.empty()) | 
|---|
| 729 | PRINTF(0)("FileComment:\n %s\n\n", this->comment.c_str()); | 
|---|
| 730 |  | 
|---|
| 731 | if (!this->fileName.empty()) | 
|---|
| 732 | { | 
|---|
| 733 | std::list<IniSection>::const_iterator section; | 
|---|
| 734 | for (section = this->sections.begin(); section != this->sections.end(); section++) | 
|---|
| 735 | { | 
|---|
| 736 | if (!(*section).comment.empty()) | 
|---|
| 737 | PRINTF(0)(" %s\n", (*section).comment.c_str()); | 
|---|
| 738 | PRINTF(0)(" [%s]\n", (*section).name.c_str()); | 
|---|
| 739 |  | 
|---|
| 740 | std::list<IniEntry>::const_iterator entry; | 
|---|
| 741 | for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) | 
|---|
| 742 | { | 
|---|
| 743 | if (!(*entry).comment.empty()) | 
|---|
| 744 | PRINTF(0)(" %s\n", (*entry).comment.c_str()); | 
|---|
| 745 | PRINTF(0)("   '%s' -> '%s'\n", (*entry).name.c_str(), (*entry).value.c_str()); | 
|---|
| 746 | } | 
|---|
| 747 | } | 
|---|
| 748 | } | 
|---|
| 749 | else | 
|---|
| 750 | PRINTF(1)("no opened ini-file.\n"); | 
|---|
| 751 | } | 
|---|
| 752 |  | 
|---|