- Timestamp:
- Jan 1, 2010, 10:05:56 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gamestate/src/libraries/core/ConfigFileManager.cc
r6432 r6437 33 33 #include "util/Convert.h" 34 34 #include "util/Math.h" 35 #include "util/StringUtils.h"36 35 #include "ConsoleCommand.h" 37 36 #include "ConfigValueContainer.h" … … 116 115 } 117 116 118 std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name) const117 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name) const 119 118 { 120 119 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 121 120 { 122 121 if ((*it)->getName() == name) 123 return it; 124 } 125 return this->entries_.end(); 122 return *it; 123 } 124 return NULL; 125 } 126 127 ConfigFileEntry* ConfigFileSection::getEntry(const std::string& name, unsigned int index) const 128 { 129 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it) 130 { 131 if (((*it)->getName() == name) && ((*it)->getIndex() == index)) 132 return *it; 133 } 134 return NULL; 126 135 } 127 136 … … 140 149 141 150 return this->entries_.insert(this->entries_.end(), new ConfigFileEntryValue(name, fallback, bString)); 142 }143 144 std::list<ConfigFileEntry*>::const_iterator ConfigFileSection::getEntryIterator(const std::string& name, unsigned int index) const145 {146 for (std::list<ConfigFileEntry*>::const_iterator it = this->entries_.begin(); it != this->entries_.end(); ++it)147 {148 if (((*it)->getName() == name) && ((*it)->getIndex() == index))149 return it;150 }151 return this->entries_.end();152 151 } 153 152 … … 175 174 // ConfigFile // 176 175 //////////////// 177 ConfigFile::ConfigFile(const std::string& filename) 176 177 const char* ConfigFile::DEFAULT_CONFIG_FOLDER = "defaultConfig"; 178 179 ConfigFile::ConfigFile(const std::string& filename, bool bCopyFallbackFile) 178 180 : filename_(filename) 181 , bCopyFallbackFile_(bCopyFallbackFile) 179 182 , bUpdated_(false) 180 183 { … … 191 194 this->clear(); 192 195 193 // Get default file if necessary and available 194 boost::filesystem::path filepath(PathConfig::getConfigPath() / this->filename_); 195 if (!boost::filesystem::exists(filepath)) 196 { 197 // Try to get default one from the data folder 198 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / "defaultConfig" / this->filename_); 199 if (boost::filesystem::exists(defaultFilepath)) 200 { 201 COUT(3) << "Copied " << this->filename_ << " from the defaultConfig folder." << std::endl; 202 boost::filesystem::copy_file(defaultFilepath, filepath); 196 boost::filesystem::path filepath(this->filename_); 197 if (!filepath.is_complete()) 198 { 199 filepath = PathConfig::getConfigPath() / filepath; 200 if (this->bCopyFallbackFile_) 201 { 202 // Look for default file in the data folder 203 if (!boost::filesystem::exists(filepath)) 204 { 205 boost::filesystem::path defaultFilepath(PathConfig::getDataPath() / DEFAULT_CONFIG_FOLDER / this->filename_); 206 if (boost::filesystem::exists(defaultFilepath)) 207 { 208 // Try to copy default file from the data folder 209 try 210 { 211 boost::filesystem::copy_file(defaultFilepath, filepath); 212 COUT(3) << "Copied " << this->filename_ << " from the default config folder." << std::endl; 213 } 214 catch (const boost::filesystem::filesystem_error& ex) 215 { COUT(1) << "Error in ConfigFile: " << ex.what() << std::endl; } 216 } 217 } 203 218 } 204 219 } … … 295 310 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 296 311 297 // Save the file in case something changed (like stripped white space) 298 this->save(); 312 // DO NOT save the file --> we can open supposedly read only config files 299 313 } // end file.is_open() 300 314 } … … 307 321 void ConfigFile::saveAs(const std::string& filename) const 308 322 { 323 boost::filesystem::path filepath(filename); 324 if (!filepath.is_complete()) 325 filepath = PathConfig::getConfigPath() / filename; 309 326 std::ofstream file; 310 file.open( (PathConfig::getConfigPathString() + filename).c_str(), std::fstream::out);327 file.open(filepath.string().c_str(), std::fstream::out); 311 328 file.setf(std::ios::fixed, std::ios::floatfield); 312 329 file.precision(6);
Note: See TracChangeset
for help on using the changeset viewer.