Changeset 6388 in orxonox.OLD for branches/preferences/src
- Timestamp:
- Jan 2, 2006, 11:09:42 PM (19 years ago)
- Location:
- branches/preferences/src/lib
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/preferences/src/lib/parser/ini_parser/ini_parser.cc
r5953 r6388 431 431 } 432 432 433 /** 434 * @brief edits the entry speciefied by entryName in sectionName/currentSection or creates it if it doesn't exist 435 * @param entryName the Name of the Entry to add 436 * @param value the value to assign to this entry 437 * @param sectionName if NULL then this entry will be set to the currentSection 438 * otherwise to the section refered to by sectionName. 439 * If both are NULL no entry will be added 440 * @return true if everything is ok false on error 441 */ 442 bool IniParser::editVar(const char* entryName, const char* value, const char* sectionName) 443 { 444 std::list<IniSection>::iterator section; 445 446 if (sectionName != NULL) 447 { 448 for (section = this->sections.begin(); section != this->sections.end(); section++) 449 if (!strcmp((*section).name, sectionName)) 450 break; 451 } 452 else 453 section = this->currentSection; 454 455 if (section == this->sections.end()) 456 { 457 assert( sectionName != NULL ); 458 459 IniSection sec; 460 sec.comment = NULL; 461 sec.name = new char[strlen(sectionName)+1]; 462 strcpy(sec.name, sectionName); 463 section = this->sections.insert(this->sections.end(), sec); 464 } 465 466 if (section == this->sections.end()) 467 { 468 PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName); 469 return false; 470 } 471 else 472 { 473 //try find item 474 std::list<IniEntry>::iterator entry; 475 for (entry = section->entries.begin(); entry!=section->entries.end(); entry++) 476 if (!strcmp( entry->name, entryName )) 477 break; 478 479 //found it? 480 if ( entry != section->entries.end() ) 481 { 482 if ( entry->value != NULL ) 483 { 484 delete[] entry->value; 485 } 486 entry->value = new char[strlen(value)+1]; 487 strcpy(entry->value, value); 488 489 return true; 490 } 491 492 //not found -> create it 493 (*section).entries.push_back(IniEntry()); 494 (*section).entries.back().comment = NULL; 495 (*section).entries.back().name = new char[strlen(entryName)+1]; 496 strcpy((*section).entries.back().name, entryName); 497 (*section).entries.back().value = new char[strlen(value)+1]; 498 strcpy((*section).entries.back().value, value); 499 PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", 500 (*section).entries.back().name, 501 (*section).entries.back().value, 502 (*section).name); 503 this->currentEntry = --(*section).entries.end(); 504 return true; 505 } 506 } 507 433 508 434 509 /** -
branches/preferences/src/lib/parser/ini_parser/ini_parser.h
r5952 r6388 67 67 68 68 bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); 69 bool editVar(const char* entryName, const char* value, const char* sectionName = NULL); 69 70 const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; 70 71 void setEntryComment(const char* comment, const char* entryName, const char* sectionName); -
branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.cc
r6383 r6388 36 36 } 37 37 38 /** 39 * reads the commandline arguments and stores them in Preferences 40 * @return true if commanline arguments have been interpreted correctly 41 */ 42 bool CmdLinePrefsReader::readCmdLine() 43 { 44 } 38 -
branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.h
r6383 r6388 14 14 virtual ~CmdLinePrefsReader(); 15 15 16 bool readCmdLine();17 18 16 private: 19 17 -
branches/preferences/src/lib/parser/preferences/ini_file_prefs_reader.cc
r6383 r6388 24 24 * standard constructor 25 25 */ 26 IniFilePrefsReader::IniFilePrefsReader ( )26 IniFilePrefsReader::IniFilePrefsReader ( const char * fileName ) 27 27 { 28 IniParser iniParser; 29 30 if ( !iniParser.readFile( fileName ) ) 31 return; 32 33 Preferences* prefs = Preferences::getInstance(); 34 35 iniParser.firstSection(); 36 37 do 38 { 39 do 40 { 41 prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue(), true ); 42 } while (iniParser.nextVar()); 43 } while (iniParser.nextSection()!=NULL); 44 45 /*iniParser.firstVar(); 46 47 Preferences* prefs = Preferences::getInstance(); 48 49 while ( iniParser.nextVar() ) 50 { 51 prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue(), true ); 52 }*/ 28 53 } 29 54 … … 36 61 } 37 62 38 /**39 * reads entries from inifile fileName into Preferences40 * @param fileName filename of inifile to read41 * @return true on success42 */43 bool IniFilePrefsReader::readIniFile( const char * fileName )44 {45 IniParser iniParser;46 63 47 if ( !iniParser.readFile( fileName ) )48 return false;49 50 iniParser.firstVar();51 52 Preferences* prefs = Preferences::getInstance();53 54 while ( iniParser.nextVar() )55 {56 prefs->setString( iniParser.getCurrentSection(), iniParser.getCurrentName(), iniParser.getCurrentValue() );57 }58 59 return true;60 } -
branches/preferences/src/lib/parser/preferences/ini_file_prefs_reader.h
r6383 r6388 15 15 16 16 public: 17 IniFilePrefsReader( );17 IniFilePrefsReader(const char* fileName); 18 18 virtual ~IniFilePrefsReader(); 19 19 20 bool readIniFile(const char* fileName);21 22 private:23 20 24 21 }; -
branches/preferences/src/lib/util/preferences.cc
r6383 r6388 28 28 this->setClassID(CL_PREFERENCES, "Preferences"); 29 29 this->setName("Preferences"); 30 this->fileName = NULL; 30 31 } 31 32 … … 41 42 { 42 43 Preferences::singletonRef = NULL; 44 45 if ( this->fileName ) 46 { 47 delete this->fileName; 48 this->fileName = NULL; 49 } 43 50 } 44 51 … … 78 85 * @param value value 79 86 */ 80 void Preferences::setString(const char* section, const char* name, const char* value) 81 { 82 setMultiType(section, name, MultiType(value)); 87 void Preferences::setString(const char* section, const char* name, const char* value, bool dontSetModified) 88 { 89 MultiType t(value); 90 setMultiType(section, name, t, dontSetModified); 83 91 } 84 92 … … 89 97 * @param value value 90 98 */ 91 void Preferences::setInt(const char* section, const char* name, int value) 92 { 93 setMultiType(section, name, MultiType(value)); 99 void Preferences::setInt(const char* section, const char* name, int value, bool dontSetModified) 100 { 101 MultiType t(value); 102 setMultiType(section, name, t, dontSetModified); 94 103 } 95 104 … … 100 109 * @param value value 101 110 */ 102 void Preferences::setFloat(const char* section, const char* name, float value) 103 { 104 setMultiType(section, name, MultiType(value)); 111 void Preferences::setFloat(const char* section, const char* name, float value, bool dontSetModified) 112 { 113 MultiType t(value); 114 setMultiType(section, name, t, dontSetModified); 105 115 } 106 116 … … 147 157 * @param value value 148 158 */ 149 void Preferences::setMultiType(const char* section, const char* name, const MultiType& value)159 void Preferences::setMultiType(const char* section, const char* name, MultiType& value, bool dontSetModified) 150 160 { 151 161 std::list<prefSection>::iterator it = data.begin(); … … 161 171 if ( strcmp(it2->name, name) == 0 ) 162 172 { 173 if (!dontSetModified) 174 it2->modified = strcmp(value.getString(), it2->value.getString())!=0; 175 163 176 it2->value = value; 177 164 178 return; 165 179 } … … 167 181 prefItem item; 168 182 item.value = value; 183 item.modified = !dontSetModified; 169 184 item.name = new char[strlen(name)+1]; 170 185 strcpy( item.name, name ); … … 176 191 prefItem item; 177 192 item.value = value; 193 item.modified = !dontSetModified; 178 194 item.name = new char[strlen(name)+1]; 179 195 strcpy( item.name, name ); … … 218 234 } 219 235 236 void Preferences::setUserIni(const char* fileName) 237 { 238 if ( this->fileName != NULL ) 239 { 240 delete this->fileName; 241 } 242 243 this->fileName = new char[strlen(fileName)+1]; 244 strcpy(this->fileName, fileName); 245 } 246 247 bool Preferences::save() 248 { 249 if ( this->fileName == NULL ) 250 { 251 PRINTF(1)("You must call setUserIni before you can call save()\n"); 252 return false; 253 } 254 IniParser iniParser(this->fileName); 255 256 if ( !iniParser.isOpen() ) 257 return false; 258 259 std::list<prefSection>::iterator it = data.begin(); 260 bool didChanges = false; 261 for ( ; it!=data.end(); it++) 262 { 263 std::list<prefItem>::iterator it2 = it->items.begin(); 264 265 for ( ; it2!=it->items.end(); it2++) 266 { 267 if ( it2->modified ) 268 { 269 iniParser.editVar(it2->name, it2->value.getString(), it->sectionName); 270 didChanges = true; 271 } 272 } 273 } 274 275 if ( didChanges ) 276 { 277 iniParser.writeFile( this->fileName ); 278 } 279 280 return true; 281 } 282 220 283 /** 221 284 * prints out all section with its items and values … … 232 295 for ( ; it2!=it->items.end(); it2++) 233 296 { 234 PRINTF(0)("--> %s = '%s' \n", it2->name, it2->value.getString());235 } 236 } 237 } 238 239 297 PRINTF(0)("--> %s = '%s'%s\n", it2->name, it2->value.getString(), ((!it2->modified)?"":" <modified>")); 298 } 299 } 300 } 301 302 -
branches/preferences/src/lib/util/preferences.h
r6381 r6388 9 9 #include "base_object.h" 10 10 #include "multi_type.h" 11 #include "lib/parser/ini_parser/ini_parser.h" 11 12 12 13 // FORWARD DECLARATION 13 14 15 class IniFilePrefsReader; 14 16 15 17 typedef struct { 16 18 char* name; 17 19 MultiType value; 20 bool modified; 18 21 } prefItem; 19 22 … … 35 38 bool exists(const char* section, const char* name); 36 39 37 void setString(const char* section, const char* name, const char* value );38 void setInt(const char* section, const char* name, int value );39 void setFloat(const char* section, const char* name, float value );40 void setMultiType(const char* section, const char* name, const MultiType& value);40 void setString(const char* section, const char* name, const char* value, bool dontSetModified = false); 41 void setInt(const char* section, const char* name, int value, bool dontSetModified = false); 42 void setFloat(const char* section, const char* name, float value, bool dontSetModified = false); 43 void setMultiType(const char* section, const char* name, MultiType& value, bool dontSetModified = false); 41 44 42 45 const char* getString(const char* section, const char* name, const char* defaultValue); … … 44 47 float getFloat(const char* section, const char* name, float defaultValue); 45 48 MultiType getMultiType(const char* section, const char* name, const MultiType& defaultValue); 49 50 void setUserIni(const char* fileName); 51 52 bool save(); 46 53 47 54 … … 54 61 55 62 std::list<prefSection> data; 63 64 std::list<IniFilePrefsReader*> iniFilePrefsReaders; 65 66 char* fileName; 67 56 68 }; 57 69
Note: See TracChangeset
for help on using the changeset viewer.