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