Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreConfigFile.cpp @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 8.1 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreConfigFile.h"
31#include "OgreResourceGroupManager.h"
32
33#include "OgreException.h"
34
35#include <iostream>
36
37namespace Ogre {
38
39    //-----------------------------------------------------------------------
40    ConfigFile::ConfigFile()
41    {
42    }
43    //-----------------------------------------------------------------------
44    ConfigFile::~ConfigFile()
45    {
46        SettingsBySection::iterator seci, secend;
47        secend = mSettings.end();
48        for (seci = mSettings.begin(); seci != secend; ++seci)
49        {
50            delete seci->second;
51        }
52    }
53    //-----------------------------------------------------------------------
54    void ConfigFile::clear(void)
55    {
56        for (SettingsBySection::iterator seci = mSettings.begin(); 
57            seci != mSettings.end(); ++seci)
58        {
59            delete seci->second;
60        }
61        mSettings.clear();
62    }
63    //-----------------------------------------------------------------------
64    void ConfigFile::load(const String& filename, const String& separators, bool trimWhitespace)
65    {
66        loadDirect(filename, separators, trimWhitespace);
67    }
68    //-----------------------------------------------------------------------
69    void ConfigFile::load(const String& filename, const String& resourceGroup, 
70        const String& separators, bool trimWhitespace)
71    {
72                loadFromResourceSystem(filename, resourceGroup, separators, trimWhitespace);
73    }
74        //-----------------------------------------------------------------------
75        void ConfigFile::loadDirect(const String& filename, const String& separators, 
76                bool trimWhitespace)
77        {
78                /* Open the configuration file */
79                std::ifstream fp;
80        // Always open in binary mode
81                fp.open(filename.c_str(), std::ios::in | std::ios::binary);
82                if(!fp)
83                        OGRE_EXCEPT(
84                        Exception::ERR_FILE_NOT_FOUND, "'" + filename + "' file not found!", "ConfigFile::load" );
85
86                // Wrap as a stream
87                DataStreamPtr stream(new FileStreamDataStream(filename, &fp, false));
88                load(stream, separators, trimWhitespace);
89
90        }
91        //-----------------------------------------------------------------------
92        void ConfigFile::loadFromResourceSystem(const String& filename, 
93                const String& resourceGroup, const String& separators, bool trimWhitespace)
94        {
95                DataStreamPtr stream = 
96                        ResourceGroupManager::getSingleton().openResource(filename, resourceGroup);
97                load(stream, separators, trimWhitespace);
98        }
99    //-----------------------------------------------------------------------
100    void ConfigFile::load(const DataStreamPtr& stream, const String& separators, 
101        bool trimWhitespace)
102    {
103        /* Clear current settings map */
104        clear();
105
106        String currentSection = StringUtil::BLANK;
107        SettingsMultiMap* currentSettings = new SettingsMultiMap();
108        mSettings[currentSection] = currentSettings;
109
110
111        /* Process the file line for line */
112        String line, optName, optVal;
113        while (!stream->eof())
114        {
115            line = stream->getLine();
116            /* Ignore comments & blanks */
117            if (line.length() > 0 && line.at(0) != '#' && line.at(0) != '@')
118            {
119                if (line.at(0) == '[' && line.at(line.length()-1) == ']')
120                {
121                    // Section
122                    currentSection = line.substr(1, line.length() - 2);
123                                        SettingsBySection::const_iterator seci = mSettings.find(currentSection);
124                                        if (seci == mSettings.end())
125                                        {
126                                                currentSettings = new SettingsMultiMap();
127                                                mSettings[currentSection] = currentSettings;
128                                        }
129                                        else
130                                        {
131                                                currentSettings = seci->second;
132                                        } 
133                }
134                else
135                {
136                    /* Find the first seperator character and split the string there */
137                    std::string::size_type separator_pos = line.find_first_of(separators, 0);
138                    if (separator_pos != std::string::npos)
139                    {
140                        optName = line.substr(0, separator_pos);
141                        /* Find the first non-seperator character following the name */
142                        std::string::size_type nonseparator_pos = line.find_first_not_of(separators, separator_pos);
143                        /* ... and extract the value */
144                        /* Make sure we don't crash on an empty setting (it might be a valid value) */
145                        optVal = (nonseparator_pos == std::string::npos) ? "" : line.substr(nonseparator_pos);
146                        if (trimWhitespace)
147                        {
148                            StringUtil::trim(optVal);
149                            StringUtil::trim(optName);
150                        }
151                        currentSettings->insert(std::multimap<String, String>::value_type(optName, optVal));
152                    }
153                }
154            }
155        }
156    }
157    //-----------------------------------------------------------------------
158    String ConfigFile::getSetting(const String& key, const String& section) const
159    {
160       
161        SettingsBySection::const_iterator seci = mSettings.find(section);
162        if (seci == mSettings.end())
163        {
164            return StringUtil::BLANK;
165        }
166        else
167        {
168            SettingsMultiMap::const_iterator i = seci->second->find(key);
169            if (i == seci->second->end())
170            {
171                return StringUtil::BLANK;
172            }
173            else
174            {
175                return i->second;
176            }
177        }
178    }
179    //-----------------------------------------------------------------------
180    StringVector ConfigFile::getMultiSetting(const String& key, const String& section) const
181    {
182        StringVector ret;
183
184
185        SettingsBySection::const_iterator seci = mSettings.find(section);
186        if (seci != mSettings.end())
187        {
188            SettingsMultiMap::const_iterator i;
189
190            i = seci->second->find(key);
191            // Iterate over matches
192            while (i != seci->second->end() && i->first == key)
193            {
194                ret.push_back(i->second);
195                ++i;
196            }
197        }
198        return ret;
199
200
201    }
202    //-----------------------------------------------------------------------
203    ConfigFile::SettingsIterator ConfigFile::getSettingsIterator(const String& section)
204    {
205        SettingsBySection::const_iterator seci = mSettings.find(section);
206        if (seci == mSettings.end())
207        {
208            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, 
209                "Cannot find section " + section, 
210                "ConfigFile::getSettingsIterator");
211        }
212        else
213        {
214            return SettingsIterator(seci->second->begin(), seci->second->end());
215        }
216    }
217    //-----------------------------------------------------------------------
218    ConfigFile::SectionIterator ConfigFile::getSectionIterator(void)
219    {
220        return SectionIterator(mSettings.begin(), mSettings.end());
221    }
222
223}
Note: See TracBrowser for help on using the repository browser.