/src/ogre/OgreMain/src/OgreConfigFile.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreConfigFile.h" |
30 | | #include "OgreResourceGroupManager.h" |
31 | | |
32 | | #include <iostream> |
33 | | |
34 | | namespace Ogre { |
35 | | |
36 | | //----------------------------------------------------------------------- |
37 | | ConfigFile::ConfigFile() |
38 | 3.39k | { |
39 | 3.39k | } |
40 | | //----------------------------------------------------------------------- |
41 | | void ConfigFile::clear(void) |
42 | 3.39k | { |
43 | 3.39k | mSettings.clear(); |
44 | 3.39k | mSettingsPtr.clear(); |
45 | 3.39k | } |
46 | | //----------------------------------------------------------------------- |
47 | | void ConfigFile::load(const String& filename, const String& separators, bool trimWhitespace) |
48 | 3.39k | { |
49 | 3.39k | loadDirect(filename, separators, trimWhitespace); |
50 | 3.39k | } |
51 | | //----------------------------------------------------------------------- |
52 | | void ConfigFile::load(const String& filename, const String& resourceGroup, |
53 | | const String& separators, bool trimWhitespace) |
54 | 0 | { |
55 | 0 | loadFromResourceSystem(filename, resourceGroup, separators, trimWhitespace); |
56 | 0 | } |
57 | | //----------------------------------------------------------------------- |
58 | | void ConfigFile::loadDirect(const String& filename, const String& separators, |
59 | | bool trimWhitespace) |
60 | 3.39k | { |
61 | 3.39k | load(_openFileStream(filename, std::ios::in | std::ios::binary), separators, trimWhitespace); |
62 | 3.39k | } |
63 | | //----------------------------------------------------------------------- |
64 | | void ConfigFile::loadFromResourceSystem(const String& filename, |
65 | | const String& resourceGroup, const String& separators, bool trimWhitespace) |
66 | 0 | { |
67 | 0 | DataStreamPtr stream = |
68 | 0 | ResourceGroupManager::getSingleton().openResource(filename, resourceGroup); |
69 | 0 | load(stream, separators, trimWhitespace); |
70 | 0 | } |
71 | | //----------------------------------------------------------------------- |
72 | | void ConfigFile::load(const DataStreamPtr& stream, const String& separators, |
73 | | bool trimWhitespace) |
74 | 3.39k | { |
75 | | /* Clear current settings map */ |
76 | 3.39k | clear(); |
77 | | |
78 | 3.39k | String currentSection = BLANKSTRING; |
79 | 3.39k | SettingsMultiMap* currentSettings = &mSettings[currentSection]; |
80 | 3.39k | mSettingsPtr[currentSection] = currentSettings; |
81 | | |
82 | | |
83 | | /* Process the file line for line */ |
84 | 3.39k | String line, optName, optVal; |
85 | 6.79k | while (!stream->eof()) |
86 | 3.39k | { |
87 | 3.39k | line = stream->getLine(); |
88 | | /* Ignore comments & blanks */ |
89 | 3.39k | if (line.length() > 0 && line.at(0) != '#' && line.at(0) != '@') |
90 | 0 | { |
91 | 0 | if (line.at(0) == '[' && line.at(line.length()-1) == ']') |
92 | 0 | { |
93 | | // Section |
94 | 0 | currentSection = line.substr(1, line.length() - 2); |
95 | 0 | currentSettings = &mSettings[currentSection]; |
96 | 0 | mSettingsPtr[currentSection] = currentSettings; |
97 | 0 | } |
98 | 0 | else |
99 | 0 | { |
100 | | /* Find the first separator character and split the string there */ |
101 | 0 | Ogre::String::size_type separator_pos = line.find_first_of(separators, 0); |
102 | 0 | if (separator_pos != Ogre::String::npos) |
103 | 0 | { |
104 | 0 | optName = line.substr(0, separator_pos); |
105 | | /* Find the first non-separator character following the name */ |
106 | 0 | Ogre::String::size_type nonseparator_pos = line.find_first_not_of(separators, separator_pos); |
107 | | /* ... and extract the value */ |
108 | | /* Make sure we don't crash on an empty setting (it might be a valid value) */ |
109 | 0 | optVal = (nonseparator_pos == Ogre::String::npos) ? "" : line.substr(nonseparator_pos); |
110 | 0 | if (trimWhitespace) |
111 | 0 | { |
112 | 0 | StringUtil::trim(optVal); |
113 | 0 | StringUtil::trim(optName); |
114 | 0 | } |
115 | 0 | currentSettings->emplace(optName, optVal); |
116 | 0 | } |
117 | 0 | } |
118 | 0 | } |
119 | 3.39k | } |
120 | 3.39k | } |
121 | | //----------------------------------------------------------------------- |
122 | | String ConfigFile::getSetting(const String& key, const String& section, const String& defaultValue) const |
123 | 0 | { |
124 | | |
125 | 0 | SettingsBySection_::const_iterator seci = mSettings.find(section); |
126 | 0 | if (seci == mSettings.end()) |
127 | 0 | { |
128 | 0 | return defaultValue; |
129 | 0 | } |
130 | 0 | else |
131 | 0 | { |
132 | 0 | SettingsMultiMap::const_iterator i = seci->second.find(key); |
133 | 0 | if (i == seci->second.end()) |
134 | 0 | { |
135 | 0 | return defaultValue; |
136 | 0 | } |
137 | 0 | else |
138 | 0 | { |
139 | 0 | return i->second; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | //----------------------------------------------------------------------- |
145 | | StringVector ConfigFile::getMultiSetting(const String& key, const String& section) const |
146 | 0 | { |
147 | 0 | StringVector ret; |
148 | |
|
149 | 0 | SettingsBySection_::const_iterator seci = mSettings.find(section); |
150 | 0 | if (seci != mSettings.end()) |
151 | 0 | { |
152 | 0 | SettingsMultiMap::const_iterator i; |
153 | |
|
154 | 0 | i = seci->second.find(key); |
155 | | // Iterate over matches |
156 | 0 | while (i != seci->second.end() && i->first == key) |
157 | 0 | { |
158 | 0 | ret.push_back(i->second); |
159 | 0 | ++i; |
160 | 0 | } |
161 | 0 | } |
162 | 0 | return ret; |
163 | | |
164 | |
|
165 | 0 | } |
166 | | //----------------------------------------------------------------------- |
167 | | ConfigFile::SettingsIterator ConfigFile::getSettingsIterator(const String& section) |
168 | 0 | { |
169 | 0 | SettingsBySection_::iterator seci = mSettings.find(section); |
170 | 0 | if (seci == mSettings.end()) |
171 | 0 | { |
172 | 0 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
173 | 0 | "Cannot find section " + section, |
174 | 0 | "ConfigFile::getSettingsIterator"); |
175 | 0 | } |
176 | | |
177 | 0 | return SettingsIterator(seci->second.begin(), seci->second.end()); |
178 | 0 | } |
179 | | //----------------------------------------------------------------------- |
180 | | const ConfigFile::SettingsMultiMap& ConfigFile::getSettings(const String& section) const |
181 | 0 | { |
182 | 0 | SettingsBySection_::const_iterator seci = mSettings.find(section); |
183 | 0 | if (seci == mSettings.end()) |
184 | 0 | { |
185 | 0 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
186 | 0 | "Cannot find section " + section, |
187 | 0 | "ConfigFile::getSettings"); |
188 | 0 | } |
189 | | |
190 | 0 | return seci->second; |
191 | 0 | } |
192 | | //----------------------------------------------------------------------- |
193 | | ConfigFile::SectionIterator ConfigFile::getSectionIterator(void) |
194 | 0 | { |
195 | 0 | return SectionIterator(mSettingsPtr.begin(), mSettingsPtr.end()); |
196 | 0 | } |
197 | | |
198 | | } |