Changeset 5020 in orxonox.OLD for orxonox/trunk/src/lib/util
- Timestamp:
- Aug 14, 2005, 11:44:49 PM (19 years ago)
- Location:
- orxonox/trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/util/ini_parser.cc
r5019 r5020 34 34 { 35 35 this->setClassID(CL_INI_PARSER, "IniParser"); 36 this->setName(fileName);37 36 38 37 this->currentEntry = NULL; … … 84 83 this->currentSection = NULL; 85 84 this->sections = NULL; 85 this->setName(NULL); 86 86 } 87 87 … … 98 98 if( fileName == NULL) 99 99 return false; 100 101 printf("1\n"); 100 this->setName(fileName); 101 102 102 if( (stream = fopen (fileName, "r")) == NULL) 103 103 { … … 107 107 else 108 108 { 109 printf("2\n");110 109 this->currentEntry = NULL; 111 110 this->currentSection = NULL; … … 134 133 { 135 134 *ptr = 0; 136 IniSection* newSection = new IniSection; 137 newSection->name = new char[strlen(buffer)+1]; 138 strcpy(newSection->name, buffer); 139 newSection->entries = new tList<IniEntry>; 140 this->currentSection = newSection; 141 this->sections->add(newSection); 135 this->addSection(buffer); 142 136 } 143 137 } … … 163 157 nameEnd[1] = '\0'; 164 158 165 IniEntry* newEntry = new IniEntry; 166 newEntry->value = new char[strlen(valueBegin)+1]; 167 strcpy(newEntry->value, valueBegin); 168 newEntry->name = new char[strlen (nameBegin)+1]; 169 strcpy(newEntry->name, nameBegin); 170 171 this->currentSection->entries->add(newEntry); 159 this->addVar(nameBegin, valueBegin); 172 160 } 173 161 } … … 175 163 fclose(stream); 176 164 return true; 165 } 166 167 /** 168 * opens a file and writes to it 169 * @param fileName: path and name of the new file to write to 170 * @return true on success false otherwise 171 */ 172 bool IniParser::writeFile(const char* fileName) 173 { 174 FILE* stream; //!< The stream we use to read the file. 175 if (sections != NULL) 176 deleteSections(); 177 if( fileName == NULL) 178 return false; 179 180 if( (stream = fopen (fileName, "w")) == NULL) 181 { 182 PRINTF(1)("IniParser could not open %s\n", fileName); 183 return false; 184 } 185 else 186 { 187 if (this->sections) 188 { 189 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 190 IniSection* sectionEnum = sectionIt->nextElement(); 191 while (sectionEnum) 192 { 193 fprintf(stream, " [%s]\n", sectionEnum->name); 194 195 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 196 IniEntry* entryEnum = entryIt->nextElement(); 197 while (entryEnum) 198 { 199 fprintf(stream, " %s = %s\n", entryEnum->name, entryEnum->value); 200 201 entryEnum = entryIt->nextElement(); 202 } 203 delete entryIt; 204 205 sectionEnum = sectionIt->nextElement(); 206 } 207 delete sectionIt; 208 } 209 else 210 PRINTF(1)("%s no sections defined yet\n", fileName); 211 } 212 fclose(stream); 213 } 214 215 bool IniParser::addSection(const char* sectionName) 216 { 217 if (this->sections == NULL) 218 this->sections = new tList<IniSection>; 219 220 IniSection* newSection = new IniSection; 221 newSection->name = new char[strlen(sectionName)+1]; 222 strcpy(newSection->name, sectionName); 223 newSection->entries = new tList<IniEntry>; 224 this->currentSection = newSection; 225 this->sections->add(newSection); 177 226 } 178 227 … … 269 318 else 270 319 return true; 320 } 321 322 /** 323 * adds a new Entry to either the currentSection or the section called by sectionName 324 * @param entryName the Name of the Entry to add 325 * @param value the value to assign to this entry 326 * @param sectionName if NULL then this entry will be set to the currentSection 327 * otherwise to the section refered to by sectionName. 328 * If both are NULL no entry will be added 329 * @return true if everything is ok false on error 330 */ 331 bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName) 332 { 333 IniSection* addSection = NULL; 334 if (sectionName != NULL) 335 { 336 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 337 IniSection* sectionEnum = sectionIt->nextElement(); 338 while (sectionEnum) 339 { 340 if (!strcmp(sectionEnum->name, sectionName)) 341 { 342 addSection = sectionEnum; 343 break; 344 } 345 sectionEnum = sectionIt->nextElement(); 346 } 347 delete sectionIt; 348 } 349 else 350 addSection = this->currentSection; 351 352 if (addSection == NULL) 353 { 354 PRINTF(2)("section not found for value %s\n", entryName); 355 return false; 356 } 357 else 358 { 359 IniEntry* newEntry = new IniEntry; 360 newEntry->name = new char[strlen (entryName)+1]; 361 strcpy(newEntry->name, entryName); 362 newEntry->value = new char[strlen(value)+1]; 363 strcpy(newEntry->value, value); 364 this->currentSection->entries->add(newEntry); 365 return true; 366 } 271 367 } 272 368 … … 277 373 * @param defaultValue: what should be returned in case the entry cannot be found 278 374 * @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 279 280 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 certainly281 375 * 376 * 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 377 * lead to unwanted behaviour. 282 378 */ 283 379 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const -
orxonox/trunk/src/lib/util/ini_parser.h
r5018 r5020 42 42 ~IniParser (); 43 43 44 bool readFile(const char* name); 44 bool readFile(const char* fileName); 45 bool writeFile(const char* fileName); 45 46 47 bool addSection(const char* sectionName); 46 48 bool getSection(const char* sectionName); 47 49 … … 52 54 bool isOpen() const { return (sections != NULL)?true:false; }; 53 55 56 bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); 54 57 const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; 55 58
Note: See TracChangeset
for help on using the changeset viewer.