- Timestamp:
- Dec 29, 2009, 10:30:19 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gamestate/src/libraries/core/ConfigFileManager.h
r6427 r6432 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 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 42 namespace orxonox // tolua_export 43 { // tolua_export 67 44 68 45 ///////////////////// … … 196 173 { 197 174 friend class ConfigFile; 175 friend class SettingsConfigFile; 198 176 199 177 public: … … 212 190 213 191 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(); } 192 { this->getOrCreateEntry(name, value, bString)->setValue(value); } 193 inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) const 194 { 195 ConfigFileEntry* entry = this->getEntry(name); 196 return (entry ? entry->getValue() : BLANKSTRING); 197 } 198 inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString) 199 { return this->getOrCreateEntry(name, fallback, bString)->getValue(); } 217 200 218 201 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(); } 202 { this->getOrCreateEntry(name, index, value, bString)->setValue(value); } 203 inline const std::string& getValue(const std::string& name, unsigned int index) const 204 { 205 ConfigFileEntry* entry = this->getEntry(name, index); 206 return (entry ? entry->getValue() : BLANKSTRING); 207 } 208 inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 209 { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); } 222 210 223 211 void deleteVectorEntries(const std::string& name, unsigned int startindex = 0); 224 unsigned int getVectorSize(const std::string& name) ;212 unsigned int getVectorSize(const std::string& name) const; 225 213 226 214 std::string getFileEntry() const; … … 234 222 { return this->entries_.end(); } 235 223 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)); } 224 std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name) const; 225 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString); 226 std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name, unsigned int index) const; 227 std::list<ConfigFileEntry*>::iterator getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString); 228 229 inline ConfigFileEntry* getEntry(const std::string& name) const 230 { return (*this->getEntryIterator(name)); } 231 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString) 232 { return (*this->getOrCreateEntryIterator(name, fallback, bString)); } 233 inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const 234 { return (*this->getEntryIterator(name, index)); } 235 inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString) 236 { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); } 243 237 244 238 std::string name_; … … 255 249 { 256 250 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_; } 251 ConfigFile(const std::string& filename); 252 virtual ~ConfigFile(); 253 254 virtual void load(); 255 virtual void save() const; 256 virtual void saveAs(const std::string& filename) const; 257 virtual void clear(); 258 259 inline const std::string& getFilename() 260 { return this->filename_; } 271 261 272 262 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; } 263 { 264 this->getOrCreateSection(section)->setValue(name, value, bString); 265 this->save(); 266 } 267 inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const 268 { 269 ConfigFileSection* sectionPtr = this->getSection(section); 270 return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING); 271 } 272 const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString); 276 273 277 274 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 275 { 276 this->getSection(section)->setValue(name, index, value, bString); 277 this->save(); 278 } 279 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) const 280 { 281 ConfigFileSection* sectionPtr = this->getSection(section); 282 return (sectionPtr ? sectionPtr->getValue(name, index) : BLANKSTRING); 283 } 284 const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString); 285 286 void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0); 287 inline unsigned int getVectorSize(const std::string& section, const std::string& name) const 288 { 289 ConfigFileSection* sectionPtr = this->getSection(section); 290 return (sectionPtr ? sectionPtr->getVectorSize(name) : 0); 291 } 292 293 protected: 294 ConfigFileSection* getSection(const std::string& section) const; 295 ConfigFileSection* getOrCreateSection(const std::string& section); 296 297 std::list<ConfigFileSection*> sections_; 298 299 private: 300 void saveIfUpdated(); 301 const std::string filename_; 302 bool bUpdated_; 303 }; 304 305 306 //////////////////////// 307 // SettingsConfigFile // 308 //////////////////////// 309 class _CoreExport SettingsConfigFile // tolua_export 310 : public ConfigFile, public Singleton<SettingsConfigFile> 311 { // tolua_export 312 friend class Singleton<SettingsConfigFile>; 313 314 public: 315 typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap; 316 317 SettingsConfigFile(const std::string& filename); 318 ~SettingsConfigFile(); 319 320 void load(); // tolua_export 321 void setFilename(const std::string& filename); // tolua_export 322 void clean(bool bCleanComments = false); // tolua_export 323 324 bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 325 bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export 326 std::string getConfig(const std::string& section, const std::string& entry); // tolua_export 327 328 void addConfigValueContainer(ConfigValueContainer* container); 329 void removeConfigValueContainer(ConfigValueContainer* container); 330 331 inline const std::set<std::string>& getSectionNames() 332 { return this->sectionNames_; } 333 inline ContainerMap::const_iterator getContainerLowerBound(const std::string section) 334 { return this->containers_.lower_bound(section); } 335 inline ContainerMap::const_iterator getContainerUpperBound(const std::string section) 336 { return this->containers_.upper_bound(section); } 337 338 static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export 339 340 private: 287 341 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 }; 342 bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&)); 343 344 ContainerMap containers_; 345 std::set<std::string> sectionNames_; 346 static SettingsConfigFile* singletonPtr_s; 347 }; // tolua_export 298 348 299 349 … … 308 358 ~ConfigFileManager(); 309 359 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; 360 void setFilename(ConfigFileType::Value type, const std::string& filename); 361 362 inline ConfigFile* getConfigFile(ConfigFileType::Value type) 363 { 364 // Check array bounds 365 return configFiles_.at(type); 366 } 343 367 344 368 private: 345 369 ConfigFileManager(const ConfigFileManager&); 346 370 347 ConfigFile* getFile(ConfigFileType type); 348 349 std::map<ConfigFileType, ConfigFile*> configFiles_; 350 unsigned int mininmalFreeType_; 351 371 boost::array<ConfigFile*, 3> configFiles_; 352 372 static ConfigFileManager* singletonPtr_s; 353 373 };
Note: See TracChangeset
for help on using the changeset viewer.