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 namespace Ogre { 00031 00032 //----------------------------------------------------------------------- 00033 ConfigFile::ConfigFile() 00034 { 00035 } 00036 //----------------------------------------------------------------------- 00037 void ConfigFile::load(const String& filename, const String& separators, bool trimWhitespace) 00038 { 00039 FILE *fp; 00040 char rec[100], *ret; 00041 String optName, optVal; 00042 00043 mSettings.clear(); 00044 00045 // Open and parse entire file 00046 fp = fopen(filename, "r"); 00047 if( !fp ) 00048 Except( 00049 Exception::ERR_FILE_NOT_FOUND, "'" + filename + "' file not found!", "ConfigFile::load" ); 00050 00051 ret = fgets(rec, 100, fp); 00052 while (ret != NULL) 00053 { 00054 String tst = rec; 00055 tst.trim(); 00056 // Ignore comments & blanks 00057 if (tst.length() > 0 && tst.at(0) != '#' && tst.at(0) != '@' && tst.at(0) != '\n') 00058 { 00059 // Tokenise on tab 00060 char* pName = strtok(rec, separators); 00061 char* pVal = strtok(NULL, "\n"); 00062 if (pName && pVal) 00063 { 00064 String optName = pName; 00065 String optVal = pVal; 00066 if (trimWhitespace) 00067 { 00068 optVal.trim(); 00069 optName.trim(); 00070 } 00071 mSettings.insert(std::multimap<String, String>::value_type(optName, optVal)); 00072 } 00073 } 00074 ret = fgets(rec, 100, fp); 00075 } 00076 00077 fclose(fp); 00078 00079 00080 } 00081 //----------------------------------------------------------------------- 00082 String ConfigFile::getSetting(const String& key) const 00083 { 00084 std::multimap<String, String>::const_iterator i; 00085 00086 i = mSettings.find(key); 00087 if (i == mSettings.end()) 00088 { 00089 return ""; 00090 } 00091 else 00092 { 00093 return i->second; 00094 } 00095 } 00096 //----------------------------------------------------------------------- 00097 StringVector ConfigFile::getMultiSetting(const String& key) const 00098 { 00099 StringVector ret; 00100 00101 std::multimap<String, String>::const_iterator i; 00102 00103 i = mSettings.find(key); 00104 // Iterate over matches 00105 while (i != mSettings.end() && i->first == key) 00106 { 00107 ret.push_back(i->second); 00108 ++i; 00109 } 00110 00111 return ret; 00112 00113 00114 } 00115 //----------------------------------------------------------------------- 00116 ConfigFile::SettingsIterator ConfigFile::getSettingsIterator(void) 00117 { 00118 return SettingsIterator(mSettings.begin(), mSettings.end()); 00119 } 00120 00121 }
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:21:58 2004