Changeset 5934 in orxonox.OLD for trunk/src/lib/util
- Timestamp:
- Dec 6, 2005, 1:23:17 AM (19 years ago)
- Location:
- trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/ini_parser.cc
r5933 r5934 33 33 34 34 /** 35 * constructs an IniParser using a file35 * @brief constructs an IniParser using a file 36 36 * @param fileName: the path and name of the file to parse 37 37 */ … … 39 39 { 40 40 this->fileName = NULL; 41 41 42 42 43 if (fileName != NULL) … … 46 47 47 48 /** 48 * removes the IniParser from memory49 * @brief removes the IniParser from memory 49 50 */ 50 51 IniParser::~IniParser () … … 55 56 56 57 /** 57 * removes all the sections. This is like delete, but even cooler :)58 * @brief removes all the sections. This is like delete, but even cooler :) 58 59 */ 59 60 void IniParser::deleteSections() 60 61 { 62 // in all sections 61 63 while(!this->sections.empty()) 62 64 { 65 63 66 IniSection section = this->sections.front(); 64 67 68 // in all entries of the sections 65 69 while(!section.entries.empty()) 66 70 { 71 // delete all strings of entries. 67 72 IniEntry entry = section.entries.front(); 68 73 delete []entry.name; … … 70 75 section.entries.pop_front(); 71 76 } 72 77 // delete all Sections 73 78 delete []section.name; 74 79 this->sections.pop_front(); 75 80 } 76 77 // this->currentEntry = NULL;78 // this->currentSection = NULL;79 81 this->setFileName(NULL); 80 82 } … … 82 84 83 85 /** 84 * opens another file to parse 86 * @brief sets the Name of the input-file 87 * @param fileName The new FileName to set to the IniParser 88 * IF fileName is NULL the new Name will be set to NULL too. 89 */ 90 void IniParser::setFileName(const char* fileName) 91 { 92 if (this->fileName) 93 delete []this->fileName; 94 if (fileName) 95 { 96 this->fileName = new char[strlen(fileName)+1]; 97 strcpy(this->fileName, fileName); 98 } 99 else 100 this->fileName = NULL; 101 } 102 103 104 /** 105 * @brief opens a file to parse 85 106 * @param fileName: path and name of the new file to parse 86 107 * @return true on success false otherwise; 108 * 109 * If there was already an opened file, the file will be closed, 110 * and the new one will be opened. 87 111 */ 88 112 bool IniParser::readFile(const char* fileName) 89 113 { 90 FILE* stream; //!< The stream we use to read the file. 91 if (!sections.empty()) 92 deleteSections(); 114 FILE* stream; //< The stream we use to read the file. 115 116 if (this->fileName != NULL) 117 this->deleteSections(); 93 118 if( fileName == NULL) 94 119 return false; 95 this->setFileName(fileName);96 120 97 121 if( (stream = fopen (fileName, "r")) == NULL) … … 102 126 else 103 127 { 104 this->currentEntry = 0;//this->sections.begin(); 105 this->currentSection = this->sections.begin(); 128 this->setFileName(fileName); 106 129 107 130 ///////////////////////////// … … 159 182 } 160 183 } 184 this->currentSection = this->sections.begin(); 185 if (!this->sections.empty()) 186 this->currentEntry = (*this->currentSection).entries.begin(); 187 161 188 fclose(stream); 162 189 return true; … … 165 192 166 193 /** 167 * opens a file and writes to it194 * @brief opens a file and writes to it 168 195 * @param fileName: path and name of the new file to write to 169 196 * @return true on success false otherwise … … 182 209 else 183 210 { 184 if (!this->sections.empty()) 185 { 186 std::list<IniSection>::iterator section; 187 for (section = this->sections.begin(); section != this->sections.end(); section++) 211 std::list<IniSection>::iterator section; 212 for (section = this->sections.begin(); section != this->sections.end(); section++) 188 213 { 189 214 fprintf(stream, "\n [%s]\n", (*section).name); 190 215 191 216 std::list<IniEntry>::iterator entry; 192 217 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 193 218 fprintf(stream, " %s = %s\n", (*entry).name, (*entry).value); 194 219 } 195 }196 else197 PRINTF(1)("%s no sections defined yet\n", fileName);198 220 } 199 221 fclose(stream); … … 202 224 203 225 /** 204 * adds a section to the list of Sections,226 * @brief adds a section to the list of Sections, 205 227 * if no Section list is availiable, it will create it 206 228 * @param sectionName the Name of the section to add … … 215 237 216 238 this->currentSection = --this->sections.end(); 239 if (!this->sections.empty()) 240 this->currentEntry = (*this->currentSection).entries.begin(); 217 241 PRINTF(5)("Added Section %s\n", sectionName); 218 242 return true; … … 221 245 222 246 /** 223 * set the parsing cursor to the specified section247 * @brief Set the parsing cursor to the specified section 224 248 * @param sectionName: the name of the section to set the cursor to 225 249 * @return true on success or false if the section could not be found … … 229 253 std::list<IniSection>::iterator section; 230 254 for (section = this->sections.begin(); section != this->sections.end(); section++) 231 {232 255 if (!strcmp((*section).name, sectionName)) 233 {234 235 this->currentEntry = (*section).entries.begin();236 237 }238 } 256 { 257 this->currentSection = section; 258 this->currentEntry = (*this->currentSection).entries.begin(); 259 return true; 260 } 261 239 262 return false; 240 263 } … … 242 265 243 266 /** 244 * moves to the first section267 * @brief moves to the first section 245 268 */ 246 269 void IniParser::getFirstSection() 247 270 { 248 271 this->currentSection = this->sections.begin(); 249 this->currentEntry = (*this->currentSection).entries.begin(); 250 } 251 252 253 /** 254 * searches the next section 272 if (!this->sections.empty()) 273 this->currentEntry = (*this->currentSection).entries.begin(); 274 } 275 276 277 /** 278 * @brief searches the next section 255 279 * @returns the name of the section if found, NULL otherwise 256 280 */ … … 261 285 else 262 286 { 287 this->currentSection++; 288 } 263 289 if (this->currentSection == this->sections.end()) 264 290 return NULL; 265 else 266 this->currentSection++;267 }268 269 if (this->currentSection != NULL)270 return this->currentSection->name;291 292 if (this->currentSection != this->sections.end()) 293 { 294 this->currentEntry = (*this->currentSection).entries.begin(); 295 return this->currentSection->name; 296 } 271 297 else 272 298 return NULL; … … 275 301 276 302 /** 277 * moves to the first Variable of the current Section303 * @brief moves to the first Variable of the current Section 278 304 */ 279 305 void IniParser::getFirstVar() … … 281 307 if (this->currentSection != this->sections.end()) 282 308 this->currentEntry = (*this->currentSection).entries.begin(); 283 // else 284 // this->currentEntry = NULL; 285 } 286 287 288 /** 289 * gets the next VarName=VarValue pair from the parsing stream 309 } 310 311 312 /** 313 * @brief gets the next VarName = VarValue pair from the parsing stream 290 314 * @return true on success, false otherwise (in the latter case name and value will be NULL) 291 315 */ 292 316 bool IniParser::nextVar() 293 317 { 294 if (this->currentSection == NULL 295 || this->currentEntry == NULL 296 || this->currentEntry == (*this->currentSection).entries.end()) 297 { 298 this->currentEntry = NULL; 299 return false; 300 } 318 if ( this->sections.empty() 319 || this->currentSection == this->sections.end() 320 || this->currentEntry == (*this->currentSection).entries.end()) 321 return false; 322 301 323 this->currentEntry++; 302 324 303 if (this->currentEntry == NULL)325 if (this->currentEntry == (*this->currentSection).entries.end()) 304 326 return false; 305 327 else … … 309 331 310 332 /** 311 * adds a new Entry to either the currentSection or the section called by sectionName333 * @brief adds a new Entry to either the currentSection or the section called by sectionName 312 334 * @param entryName the Name of the Entry to add 313 335 * @param value the value to assign to this entry … … 319 341 bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName) 320 342 { 321 std::list<IniSection>::iterator section = this->sections.end();343 std::list<IniSection>::iterator section; 322 344 323 345 if (sectionName != NULL) … … 332 354 if (section == this->sections.end()) 333 355 { 334 PRINTF(2)("section not found for value %s\n", entryName);356 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName); 335 357 return false; 336 358 } … … 349 371 350 372 /** 351 * directly acesses an entry in a section373 * @brief directly acesses an entry in a section 352 374 * @param entryName: the name of the entry to find 353 375 * @param sectionName: the section where the entry is to be found … … 363 385 { 364 386 std::list<IniSection>::const_iterator section; 365 for (section = this->sections.begin(); section != this->sections.end(); section++) 366 { 367 if (!strcmp((*section).name, sectionName)) 387 if (sectionName != NULL) 368 388 { 369 std::list<IniEntry>::const_iterator entry; 370 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 371 if (!strcmp((*entry).name, entryName)) 372 return (*entry).value; 373 PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName); 374 break; 389 390 for (section = this->sections.begin(); section != this->sections.end(); section++) 391 { 392 if (!strcmp((*section).name, sectionName)) 393 { 394 break; 395 } 396 } 397 PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName); 375 398 } 376 } 377 PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName); 378 } 379 else 380 PRINTF(1)("%s not opened\n", fileName); 399 else 400 section = this->currentSection; 401 402 std::list<IniEntry>::const_iterator entry; 403 for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++) 404 if (!strcmp((*entry).name, entryName)) 405 return (*entry).value; 406 PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName, sectionName); 407 408 } 409 else 410 PRINTF(2)("%s not opened\n", fileName); 381 411 382 412 return defaultValue; … … 384 414 } 385 415 386 387 void IniParser::setFileName(const char* fileName) 388 { 389 if (this->fileName) 390 delete []this->fileName; 391 if (fileName) 392 { 393 this->fileName = new char[strlen(fileName)+1]; 394 strcpy(this->fileName, fileName); 395 } 396 else 397 this->fileName = NULL; 398 } 399 400 401 /** 402 * output the whole tree in a nice and easy way. 416 /** 417 * @brief output the whole tree in a nice and easy way. 403 418 */ 404 419 void IniParser::debug() const -
trunk/src/lib/util/ini_parser.h
r5933 r5934 48 48 bool getSection(const char* sectionName); 49 49 50 /** @returns true if the file is opened, false otherwise*/ 51 bool isOpen() const { return (this->fileName != NULL)? true : false; }; 52 50 53 void getFirstSection(); 51 54 const char* nextSection(); 52 55 53 /** @returns true if the file is opened, false otherwise*/54 bool isOpen() const { return (this->fileName != NULL)? true : false; };55 56 /** @returns the fileName we have opened. */ 56 57 const char* getFileName() const { return this->fileName; }; … … 71 72 void debug() const; 72 73 74 73 75 private: 74 76 void deleteSections();
Note: See TracChangeset
for help on using the changeset viewer.