00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #include "OgreStableHeaders.h" 00026 #include "OgreConfigFile.h" 00027 00028 #include "OgreException.h" 00029 00030 #include <iostream> 00031 00032 namespace Ogre { 00033 00034 //----------------------------------------------------------------------- 00035 ConfigFile::ConfigFile() 00036 { 00037 } 00038 //----------------------------------------------------------------------- 00039 void ConfigFile::load(const String& filename, const String& separators, bool trimWhitespace) 00040 { 00041 /* Clear current settings map */ 00042 mSettings.clear(); 00043 00044 /* Open the configuration file */ 00045 std::ifstream fp(filename.c_str()); 00046 if(!fp) 00047 Except( 00048 Exception::ERR_FILE_NOT_FOUND, "'" + filename + "' file not found!", "ConfigFile::load" ); 00049 00050 /* Process the file line for line */ 00051 String line, optName, optVal; 00052 while (std::getline(fp, line)) 00053 { 00054 StringUtil::trim(line); 00055 /* Ignore comments & blanks */ 00056 if (line.length() > 0 && line.at(0) != '#' && line.at(0) != '@') 00057 { 00058 /* Find the first seperator character and split the string there */ 00059 int separator_pos = line.find_first_of(separators, 0); 00060 if (separator_pos != std::string::npos) 00061 { 00062 optName = line.substr(0, separator_pos); 00063 /* Find the first non-seperator character following the name */ 00064 int nonseparator_pos = line.find_first_not_of(separators, separator_pos); 00065 /* ... and extract the value */ 00066 optVal = line.substr(nonseparator_pos); 00067 if (trimWhitespace) 00068 { 00069 StringUtil::trim(optVal); 00070 StringUtil::trim(optName); 00071 } 00072 mSettings.insert(std::multimap<String, String>::value_type(optName, optVal)); 00073 } 00074 } 00075 } 00076 00077 } 00078 //----------------------------------------------------------------------- 00079 String ConfigFile::getSetting(const String& key) const 00080 { 00081 std::multimap<String, String>::const_iterator i; 00082 00083 i = mSettings.find(key); 00084 if (i == mSettings.end()) 00085 { 00086 return ""; 00087 } 00088 else 00089 { 00090 return i->second; 00091 } 00092 } 00093 //----------------------------------------------------------------------- 00094 StringVector ConfigFile::getMultiSetting(const String& key) const 00095 { 00096 StringVector ret; 00097 00098 00099 std::multimap<String, String>::const_iterator i; 00100 00101 i = mSettings.find(key); 00102 // Iterate over matches 00103 while (i != mSettings.end() && i->first == key) 00104 { 00105 ret.push_back(i->second); 00106 ++i; 00107 } 00108 00109 return ret; 00110 00111 00112 } 00113 //----------------------------------------------------------------------- 00114 ConfigFile::SettingsIterator ConfigFile::getSettingsIterator(void) 00115 { 00116 return SettingsIterator(mSettings.begin(), mSettings.end()); 00117 } 00118 00119 }
Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:18 2004