Changeset 2690 for code/branches/buildsystem3/src/core/ConfigFileManager.cc
- Timestamp:
- Feb 23, 2009, 8:57:42 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/buildsystem3/src/core/ConfigFileManager.cc
r2687 r2690 223 223 void ConfigFile::load(bool bCreateIfNotExisting) 224 224 { 225 // Be sure we start from new 225 // Be sure we start from new in the memory 226 226 this->clear(); 227 227 228 boost::filesystem::path filepath(Core::getConfigPath() + "/" + this->filename_); 229 230 // This creates the config file if it's not existing 231 std::ofstream createFile; 232 createFile.open(filepath.file_string().c_str(), std::fstream::app); 233 createFile.close(); 228 // Get default file if necessary and available 229 boost::filesystem::path filepath(Core::getConfigPath()); 230 filepath /= this->filename_; 231 if (!boost::filesystem::exists(filepath)) 232 { 233 // Try to get default one from the media folder 234 boost::filesystem::path defaultFilepath(Core::getMediaPath()); 235 defaultFilepath = defaultFilepath / "defaultConfig" / this->filename_; 236 if (boost::filesystem::exists(defaultFilepath)) 237 { 238 boost::filesystem::copy_file(defaultFilepath, filepath); 239 } 240 } 234 241 235 242 // Open the file 236 243 std::ifstream file; 237 244 file.open(filepath.file_string().c_str(), std::fstream::in); 238 239 if (!file.is_open()) 240 { 241 COUT(1) << "An error occurred in ConfigFileManager.cc:" << std::endl; 242 COUT(1) << "Error: Couldn't open config-file \"" << this->filename_ << "\"." << std::endl; 243 return; 244 } 245 246 char linearray[CONFIG_FILE_MAX_LINELENGHT]; 247 248 ConfigFileSection* newsection = 0; 249 250 while (file.good() && !file.eof()) 251 { 252 file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT); 253 254 std::string line = std::string(linearray); 255 256 std::string temp = getStripped(line); 257 if (!isEmpty(temp) && !isComment(temp)) 258 { 259 size_t pos1 = temp.find('['); 260 if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos; 261 size_t pos2 = line.find(']'); 262 263 if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1) 245 if (file.is_open()) 246 { 247 248 char linearray[CONFIG_FILE_MAX_LINELENGHT]; 249 250 ConfigFileSection* newsection = 0; 251 252 while (file.good() && !file.eof()) 253 { 254 file.getline(linearray, CONFIG_FILE_MAX_LINELENGHT); 255 256 std::string line = std::string(linearray); 257 258 std::string temp = getStripped(line); 259 if (!isEmpty(temp) && !isComment(temp)) 264 260 { 265 // New section 266 std::string comment = line.substr(pos2 + 1); 267 if (isComment(comment)) 268 newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment); 269 else 270 newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1)); 271 this->sections_.insert(this->sections_.end(), newsection); 272 continue; 273 } 274 } 275 276 if (newsection != 0) 277 { 278 if (isComment(line)) 279 { 280 // New comment 281 newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line))); 282 continue; 283 } 284 else 285 { 286 size_t pos1 = line.find('='); 287 288 if (pos1 != std::string::npos && pos1 > 0) 261 size_t pos1 = temp.find('['); 262 if (pos1 == 0) pos1 = line.find('['); else pos1 = std::string::npos; 263 size_t pos2 = line.find(']'); 264 265 if (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1) 289 266 { 290 // New entry 291 size_t pos2 = line.find('['); 292 size_t pos3 = line.find(']'); 293 size_t commentposition = getNextCommentPosition(line, pos1 + 1); 294 while (isBetweenQuotes(line, commentposition)) 295 { 296 commentposition = getNextCommentPosition(line, commentposition + 1); 297 } 298 std::string value = "", comment = ""; 299 if (commentposition == std::string::npos) 300 { 301 value = removeTrailingWhitespaces(line.substr(pos1 + 1)); 302 } 267 // New section 268 std::string comment = line.substr(pos2 + 1); 269 if (isComment(comment)) 270 newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1), comment); 303 271 else 304 { 305 value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1)); 306 comment = removeTrailingWhitespaces(line.substr(commentposition)); 307 } 308 309 if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1) 310 { 311 // There might be an array index 312 unsigned int index = 0; 313 if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1))) 314 { 315 // New array 316 std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); 317 (*it)->setValue(value); 318 (*it)->setComment(comment); 319 continue; 320 } 321 } 322 323 // New value 324 newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment)); 272 newsection = new ConfigFileSection(line.substr(pos1 + 1, pos2 - pos1 - 1)); 273 this->sections_.insert(this->sections_.end(), newsection); 325 274 continue; 326 275 } 327 276 } 328 } 329 } 330 331 file.close(); 332 333 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 334 335 // Save the file in case something changed (like stripped whitespaces) 336 this->save(); 337 338 // Update all ConfigValueContainers 339 this->updateConfigValues(); 277 278 if (newsection != 0) 279 { 280 if (isComment(line)) 281 { 282 // New comment 283 newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryComment(removeTrailingWhitespaces(line))); 284 continue; 285 } 286 else 287 { 288 size_t pos1 = line.find('='); 289 290 if (pos1 != std::string::npos && pos1 > 0) 291 { 292 // New entry 293 size_t pos2 = line.find('['); 294 size_t pos3 = line.find(']'); 295 size_t commentposition = getNextCommentPosition(line, pos1 + 1); 296 while (isBetweenQuotes(line, commentposition)) 297 { 298 commentposition = getNextCommentPosition(line, commentposition + 1); 299 } 300 std::string value = "", comment = ""; 301 if (commentposition == std::string::npos) 302 { 303 value = removeTrailingWhitespaces(line.substr(pos1 + 1)); 304 } 305 else 306 { 307 value = removeTrailingWhitespaces(line.substr(pos1 + 1, commentposition - pos1 - 1)); 308 comment = removeTrailingWhitespaces(line.substr(commentposition)); 309 } 310 311 if (pos2 != std::string::npos && pos3 != std::string::npos && pos3 > pos2 + 1) 312 { 313 // There might be an array index 314 unsigned int index = 0; 315 if (ConvertValue(&index, line.substr(pos2 + 1, pos3 - pos2 - 1))) 316 { 317 // New array 318 std::list<ConfigFileEntry*>::iterator it = newsection->getEntryIterator(getStripped(line.substr(0, pos2)), index, value, false); 319 (*it)->setValue(value); 320 (*it)->setComment(comment); 321 continue; 322 } 323 } 324 325 // New value 326 newsection->getEntries().insert(newsection->getEntries().end(), new ConfigFileEntryValue(getStripped(line.substr(0, pos1)), value, false, comment)); 327 continue; 328 } 329 } 330 } 331 } 332 333 file.close(); 334 335 COUT(3) << "Loaded config file \"" << this->filename_ << "\"." << std::endl; 336 337 // Save the file in case something changed (like stripped whitespaces) 338 this->save(); 339 340 // Update all ConfigValueContainers 341 this->updateConfigValues(); 342 } // end file.is_open() 340 343 } 341 344 342 345 void ConfigFile::save() const 343 346 { 344 boost::filesystem::path filepath(Core::getConfigPath() + "/" + this->filename_); 347 boost::filesystem::path filepath(Core::getConfigPath()); 348 filepath /= this->filename_; 345 349 346 350 std::ofstream file;
Note: See TracChangeset
for help on using the changeset viewer.