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