Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreConfigFile.cpp

Go to the documentation of this file.
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                 optName = strtok(rec, separators);
00061                 optVal = strtok(NULL, "\n");
00062                 if (optName.length() != 0 && optVal != 0)
00063                 {
00064                     if (trimWhitespace)
00065                     {
00066                         optVal.trim();
00067                         optName.trim();
00068                     }
00069                     mSettings.insert(std::multimap<String, String>::value_type(optName, optVal));
00070                 }
00071             }
00072             ret = fgets(rec, 100, fp);
00073         }
00074 
00075         fclose(fp);
00076 
00077 
00078     }
00079     //-----------------------------------------------------------------------
00080     String ConfigFile::getSetting(const String& key) const
00081     {
00082         std::multimap<String, String>::const_iterator i;
00083 
00084         i = mSettings.find(key);
00085         if (i == mSettings.end())
00086         {
00087             return "";
00088         }
00089         else
00090         {
00091             return i->second;
00092         }
00093     }
00094     //-----------------------------------------------------------------------
00095     StringVector ConfigFile::getMultiSetting(const String& key) const
00096     {
00097         StringVector ret;
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 Wed Jan 21 00:10:05 2004