Changeset 6536 for code/trunk/src/libraries/core/ConfigFileManager.h
- Timestamp:
- Mar 16, 2010, 11:22:36 AM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/gamestate (added) merged: 6430-6432,6437,6439-6440
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/ConfigFileManager.h
r6427 r6536 32 32 #include "CorePrereqs.h" 33 33 34 #include <cassert>35 #include <string>36 34 #include <list> 37 35 #include <map> 38 39 #include "util/OrxEnum.h" 36 #include <set> 37 #include <string> 38 #include <boost/array.hpp> 39 40 40 #include "util/Singleton.h" 41 42 // tolua_begin 43 namespace orxonox 44 { 45 // tolua_end 46 // Use int as config file type to have an arbitrary number of files 47 struct ConfigFileType : OrxEnum<ConfigFileType> 48 { 49 OrxEnumConstructors(ConfigFileType); 50 51 static const int NoType = 0; 52 static const int Settings = 1; 53 static const int JoyStickCalibration = 2; 54 static const int CommandHistory = 3; 55 56 static const int numberOfReservedTypes = 1024; 57 }; 58 59 _CoreExport bool config(const std::string& classname, const std::string& varname, const std::string& value); // tolua_export 60 _CoreExport const std::string& getConfig(const std::string& classname, const std::string& varname); // tolua_export 61 _CoreExport bool tconfig(const std::string& classname, const std::string& varname, const std::string& value); 62 _CoreExport void reloadConfig(); 63 _CoreExport void saveConfig(); 64 _CoreExport void cleanConfig(); 65 _CoreExport void loadSettings(const std::string& filename); 66 41 #include "util/StringUtils.h" 42 43 namespace orxonox // tolua_export 44 { // tolua_export 67 45 68 46 ///////////////////// … … 196 174 { 197 175 friend class ConfigFile; 176 friend class SettingsConfigFile; 198 177 199 178 public: … … 212 191 213 192 inline void setValue(const std::string& name, const std::string& value, bool bString) 214 { this->getEntry(name, value, bString)->setValue(value); } 215 inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) 216 { return this->getEntry(name, fallback, bString)->getValue(); } 193 { this->getOrCreateEntry(name, value, bString)->setValue(value); } 194 inline const std::string& getValue(const std::string& name, bool bString) 195 { 196 ConfigFileEntry* entry = this->getEntry(name); 197 if (entry) 198 { 199 entry->setString(bString); 200 return entry->getValue(); 201 } 202 return BLANKSTRING; 203 } 204 inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) 205 { return this->getOrCreateEntry(name, fallback, bString)->getValue(); } 217 206 218 207 inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString) 219 { this->getEntry(name, index, value, bString)->setValue(value); } 220 inline const std::string& getValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 221 { return this->getEntry(name, index, fallback, bString)->getValue(); } 208 { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } 209 inline const std::string& getValue(const std::string& name, unsigned int index, bool bString) 210 { 211 ConfigFileEntry* entry = this->getEntry(name, index); 212 if (entry) 213 { 214 entry->setString(bString); 215 return entry->getValue(); 216 } 217 return BLANKSTRING; 218 } 219 inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 220 { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); } 222 221 223 222 void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); 224 unsigned int getVectorSize(const std::string& name) ;223 unsigned int getVectorSize(const std::string& name) const; 225 224 226 225 std::string getFileEntry() const; … … 234 233 { return this->entries_.end(); } 235 234 236 std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, const std::string& fallback, bool bString); 237 std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); 238 239 inline ConfigFileEntry* getEntry(const std::string& name, const std::string& fallback, bool bString) 240 { return (*this->getEntryIterator(name, fallback, bString)); } 241 inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 242 { return (*this->getEntryIterator(name, index, fallback, bString)); } 235 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString); 236 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); 237 238 ConfigFileEntry* getEntry(const std::string& name) const; 239 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) 240 { return (*this->getOrCreateEntryIterator(name, fallback, bString)); } 241 ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const; 242 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 243 { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } 243 244 244 245 std::string name_; … … 255 256 { 256 257 public: 257 inline ConfigFile(const std::string& filename, ConfigFileType type) 258 : filename_(filename) 259 , type_(type) 260 , bUpdated_(false) 261 { } 262 ~ConfigFile(); 263 264 void load(bool bCreateIfNotExisting = true); 265 void save() const; 266 void saveAs(const std::string& filename); 267 void clean(bool bCleanComments = false); 268 void clear(); 269 270 const std::string& getFilename() { return this->filename_; } 258 ConfigFile(const std::string& filename, bool bCopyFallbackFile = true); 259 virtual ~ConfigFile(); 260 261 virtual void load(); 262 virtual void save() const; 263 virtual void saveAs(const std::string& filename) const; 264 virtual void clear(); 265 266 inline const std::string& getFilename() 267 { return this->filename_; } 271 268 272 269 inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString) 273 { this->getSection(section)->setValue(name, value, bString); this->save(); } 274 inline const std::string& getValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString) 275 { const std::string& output = this->getSection(section)->getValue(name, fallback, bString); this->saveIfUpdated(); return output; } 270 { 271 this->getOrCreateSection(section)->setValue(name, value, bString); 272 this->save(); 273 } 274 inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) 275 { 276 ConfigFileSection* sectionPtr = this->getSection(section); 277 return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); 278 } 279 const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString); 276 280 277 281 inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) 278 { this->getSection(section)->setValue(name, index, value, bString); this->save(); } 279 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) 280 { const std::string& output = this->getSection(section)->getValue(name, index, fallback, bString); this->saveIfUpdated(); return output; } 281 282 inline void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0) 283 { this->getSection(section)->deleteVectorEntries(name, startindex); } 284 inline unsigned int getVectorSize(const std::string& section, const std::string& name) 285 { return this->getSection(section)->getVectorSize(name); } 286 282 { 283 this->getOrCreateSection(section)->setValue(name, index, value, bString); 284 this->save(); 285 } 286 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index, bool bString) 287 { 288 ConfigFileSection* sectionPtr = this->getSection(section); 289 return (sectionPtr ? sectionPtr->getValue(name, index, bString) : BLANKSTRING); 290 } 291 const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); 292 293 void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); 294 inline unsigned int getVectorSize(const std::string& section, const std::string& name) const 295 { 296 ConfigFileSection* sectionPtr = this->getSection(section); 297 return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); 298 } 299 300 static const char* DEFAULT_CONFIG_FOLDER; 301 302 protected: 303 ConfigFileSection* getSection(const std::string& section) const; 304 ConfigFileSection* getOrCreateSection(const std::string& section); 305 306 std::list<ConfigFileSection*> sections_; 307 308 private: 309 void saveIfUpdated(); 310 const std::string filename_; 311 const bool bCopyFallbackFile_; 312 bool bUpdated_; 313 }; 314 315 316 //////////////////////// 317 // SettingsConfigFile // 318 //////////////////////// 319 class _CoreExport SettingsConfigFile // tolua_export 320 : public ConfigFile, public Singleton<SettingsConfigFile> 321 { // tolua_export 322 friend class Singleton<SettingsConfigFile>; 323 324 public: 325 typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap; 326 327 SettingsConfigFile(const std::string& filename); 328 ~SettingsConfigFile(); 329 330 void load(); // tolua_export 331 void setFilename(const std::string& filename); // tolua_export 332 void clean(bool bCleanComments = false); // tolua_export 333 334 bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 335 bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 336 std::string getConfig(const std::string& section, const std::string& entry); // tolua_export 337 338 void addConfigValueContainer(ConfigValueContainer* container); 339 void removeConfigValueContainer(ConfigValueContainer* container); 340 341 inline const std::set<std::string>& getSectionNames() 342 { return this->sectionNames_; } 343 inline ContainerMap::const_iterator getContainerLowerBound(const std::string section) 344 { return this->containers_.lower_bound(section); } 345 inline ContainerMap::const_iterator getContainerUpperBound(const std::string section) 346 { return this->containers_.upper_bound(section); } 347 348 static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export 349 350 private: 287 351 void updateConfigValues(); 288 289 private: 290 ConfigFileSection* getSection(const std::string& section); 291 void saveIfUpdated(); 292 293 std::string filename_; 294 ConfigFileType type_; 295 std::list<ConfigFileSection*> sections_; 296 bool bUpdated_; 297 }; 352 bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)); 353 354 ContainerMap containers_; 355 std::set<std::string> sectionNames_; 356 static SettingsConfigFile* singletonPtr_s; 357 }; // tolua_export 298 358 299 359 … … 308 368 ~ConfigFileManager(); 309 369 310 void load(); 311 void save(); 312 void clean(bool bCleanComments = false); 313 314 void setFilename(ConfigFileType type, const std::string& filename); 315 const std::string& getFilename(ConfigFileType type); 316 317 ConfigFileType getNewConfigFileType() { return mininmalFreeType_++; } 318 319 void load(ConfigFileType type); 320 void save(ConfigFileType type); 321 void saveAs(ConfigFileType type, const std::string& saveFilename); 322 void clean(ConfigFileType type, bool bCleanComments = false); 323 324 inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value, bool bString) 325 { this->getFile(type)->setValue(section, name, value, bString); } 326 inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& fallback, bool bString) 327 { return this->getFile(type)->getValue(section, name, fallback, bString); } 328 329 inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString) 330 { this->getFile(type)->setValue(section, name, index, value, bString); } 331 inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString) 332 { return this->getFile(type)->getValue(section, name, index, fallback, bString); } 333 334 inline void deleteVectorEntries(ConfigFileType type, const std::string& section, const std::string& name, unsigned int startindex = 0) 335 { this->getFile(type)->deleteVectorEntries(section, name, startindex); } 336 inline unsigned int getVectorSize(ConfigFileType type, const std::string& section, const std::string& name) 337 { return this->getFile(type)->getVectorSize(section, name); } 338 339 void updateConfigValues(); 340 void updateConfigValues(ConfigFileType type); 341 342 static std::string DEFAULT_CONFIG_FILE; 370 void setFilename(ConfigFileType::Value type, const std::string& filename); 371 372 inline ConfigFile* getConfigFile(ConfigFileType::Value type) 373 { 374 // Check array bounds 375 return configFiles_.at(type); 376 } 343 377 344 378 private: 345 379 ConfigFileManager(const ConfigFileManager&); 346 380 347 ConfigFile* getFile(ConfigFileType type); 348 349 std::map<ConfigFileType, ConfigFile*> configFiles_; 350 unsigned int mininmalFreeType_; 351 381 boost::array<ConfigFile*, 3> configFiles_; 352 382 static ConfigFileManager* singletonPtr_s; 353 383 };
Note: See TracChangeset
for help on using the changeset viewer.