Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
ImprovedConfigFile.h
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2016 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#pragma once
23
24#include "Application.h"
25#include "ConfigFile.h"
26
27#include <OgreConfigFile.h>
28#include <OgreDataStream.h>
29#include <OgreException.h>
30#include <OgreString.h>
31#include <OgreResourceGroupManager.h>
32
33#include <cstdio>
34
37{
38public:
40 {
41 ConfigFile();
42 }
43
47
48 void loadImprovedCfg(std::string const& filename, std::string const& resource_group_name)
49 {
50 ConfigFile::load(filename, resource_group_name, this->separators, /*trimWhitespace*/true);
51 }
52
53 bool hasSetting(Ogre::String key, Ogre::String section = "")
54 {
55 return (mSettingsPtr.find(section) != mSettingsPtr.end() && mSettingsPtr[section]->find(key) != mSettingsPtr[section]->end());
56 }
57
58 bool saveImprovedCfg(std::string const& filename, std::string const& resource_group_name)
59 {
60 Ogre::DataStreamPtr stream
61 = Ogre::ResourceGroupManager::getSingleton().createResource(
62 filename, resource_group_name, /*overwrite=*/true);
63
64 const size_t BUF_LEN = 2000;
65 char buf[BUF_LEN];
66 SettingsBySection::iterator secIt;
67 for (secIt = mSettingsPtr.begin(); secIt != mSettingsPtr.end(); secIt++)
68 {
69 if (secIt->first.size() > 0)
70 {
71 int num_chars = std::snprintf(buf, BUF_LEN, "[%s]\n", secIt->first.c_str());
72 stream->write(buf, num_chars);
73 }
74
75 SettingsMultiMap::iterator setIt;
76 for (setIt = secIt->second->begin(); setIt != secIt->second->end(); setIt++)
77 {
78 int num_chars = std::snprintf(buf, BUF_LEN, "%s%c%s\n", setIt->first.c_str(), separators[0], setIt->second.c_str());
79 stream->write(buf, num_chars);
80 }
81 }
82 return true;
83 }
84
85 void setSetting(Ogre::String key, Ogre::String value, Ogre::String section = Ogre::BLANKSTRING)
86 {
87 SettingsMultiMap* set = mSettingsPtr[section];
88 if (!set)
89 {
90 // new section
91 set = new SettingsMultiMap();
92 mSettingsPtr[section] = set;
93 }
94 if (set->count(key))
95 // known key, delete old first
96 set->erase(key);
97 // add key
98 set->insert(std::multimap<Ogre::String, Ogre::String>::value_type(key, value));
99 }
100
101 // type specific implementations
102 Ogre::Radian getSettingRadian(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
103 {
104 return Ogre::StringConverter::parseAngle(getString(key, section));
105 }
106
107 void setSetting(Ogre::String key, Ogre::Radian value, Ogre::String section = Ogre::BLANKSTRING)
108 {
109 setSetting(key, TOSTRING(value), section);
110 }
111
112 bool getSettingBool(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
113 {
114 return Ogre::StringConverter::parseBool(getString(key, section));
115 }
116
117 void setSetting(Ogre::String key, bool value, Ogre::String section = Ogre::BLANKSTRING)
118 {
119 setSetting(key, TOSTRING(value), section);
120 }
121
122 Ogre::Real getSettingReal(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
123 {
124 return Ogre::StringConverter::parseReal(getString(key, section));
125 }
126
127 void setSetting(Ogre::String key, Ogre::Real value, Ogre::String section = Ogre::BLANKSTRING)
128 {
129 setSetting(key, TOSTRING(value), section);
130 }
131
132 int getSettingInt(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
133 {
134 return Ogre::StringConverter::parseInt(getString(key, section));
135 }
136
137 void setSetting(Ogre::String key, int value, Ogre::String section = Ogre::BLANKSTRING)
138 {
139 setSetting(key, TOSTRING(value), section);
140 }
141
142 unsigned int getSettingUnsignedInt(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
143 {
144 return Ogre::StringConverter::parseUnsignedInt(getString(key, section));
145 }
146
147 void setSetting(Ogre::String key, unsigned int value, Ogre::String section = Ogre::BLANKSTRING)
148 {
149 setSetting(key, TOSTRING(value), section);
150 }
151
152 long getSettingLong(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
153 {
154 return Ogre::StringConverter::parseLong(getString(key, section));
155 }
156
157 void setSetting(Ogre::String key, long value, Ogre::String section = Ogre::BLANKSTRING)
158 {
159 setSetting(key, TOSTRING(value), section);
160 }
161
162 unsigned long getSettingUnsignedLong(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
163 {
164 return Ogre::StringConverter::parseUnsignedLong(getString(key, section));
165 }
166
167 void setSetting(Ogre::String key, unsigned long value, Ogre::String section = Ogre::BLANKSTRING)
168 {
169 setSetting(key, TOSTRING(value), section);
170 }
171
172 Ogre::Vector3 getSettingVector3(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
173 {
174 return Ogre::StringConverter::parseVector3(getString(key, section));
175 }
176
177 void setSetting(Ogre::String key, Ogre::Vector3 value, Ogre::String section = Ogre::BLANKSTRING)
178 {
179 setSetting(key, TOSTRING(value), section);
180 }
181
182 Ogre::Matrix3 getSettingMatrix3(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
183 {
184 return Ogre::StringConverter::parseMatrix3(getString(key, section));
185 }
186
187 void setSetting(Ogre::String key, Ogre::Matrix3 value, Ogre::String section = Ogre::BLANKSTRING)
188 {
189 setSetting(key, TOSTRING(value), section);
190 }
191
192 Ogre::Matrix4 getSettingMatrix4(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
193 {
194 return Ogre::StringConverter::parseMatrix4(getString(key, section));
195 }
196
197 void setSetting(Ogre::String key, Ogre::Matrix4 value, Ogre::String section = Ogre::BLANKSTRING)
198 {
199 setSetting(key, TOSTRING(value), section);
200 }
201
202 Ogre::Quaternion getSettingQuaternion(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
203 {
204 return Ogre::StringConverter::parseQuaternion(getString(key, section));
205 }
206
207 void setSetting(Ogre::String key, Ogre::Quaternion value, Ogre::String section = Ogre::BLANKSTRING)
208 {
209 setSetting(key, TOSTRING(value), section);
210 }
211
212 Ogre::ColourValue getSettingColorValue(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
213 {
214 return Ogre::StringConverter::parseColourValue(getString(key, section));
215 }
216
217 void setSetting(Ogre::String key, Ogre::ColourValue value, Ogre::String section = Ogre::BLANKSTRING)
218 {
219 setSetting(key, TOSTRING(value), section);
220 }
221
222 Ogre::StringVector getSettingStringVector(Ogre::String key, Ogre::String section = Ogre::BLANKSTRING)
223 {
224 return Ogre::StringConverter::parseStringVector(getString(key, section));
225 }
226
227 void setSetting(Ogre::String key, Ogre::StringVector value, Ogre::String section = Ogre::BLANKSTRING)
228 {
229 setSetting(key, TOSTRING(value), section);
230 }
231
232protected:
233 Ogre::String separators;
234};
Central state/object manager and communications hub.
#define TOSTRING(x)
Definition Application.h:57
Used by AngelScript local storage.
unsigned long getSettingUnsignedLong(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void loadImprovedCfg(std::string const &filename, std::string const &resource_group_name)
void setSetting(Ogre::String key, Ogre::StringVector value, Ogre::String section=Ogre::BLANKSTRING)
long getSettingLong(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::Radian value, Ogre::String section=Ogre::BLANKSTRING)
bool saveImprovedCfg(std::string const &filename, std::string const &resource_group_name)
Ogre::Vector3 getSettingVector3(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Real getSettingReal(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Matrix3 getSettingMatrix3(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, long value, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Quaternion getSettingQuaternion(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
unsigned int getSettingUnsignedInt(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::ColourValue getSettingColorValue(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, bool value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::Quaternion value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::Vector3 value, Ogre::String section=Ogre::BLANKSTRING)
int getSettingInt(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, unsigned long value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::String value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::Matrix3 value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::Real value, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Radian getSettingRadian(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Ogre::Matrix4 getSettingMatrix4(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
bool hasSetting(Ogre::String key, Ogre::String section="")
void setSetting(Ogre::String key, Ogre::Matrix4 value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, int value, Ogre::String section=Ogre::BLANKSTRING)
bool getSettingBool(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, Ogre::ColourValue value, Ogre::String section=Ogre::BLANKSTRING)
void setSetting(Ogre::String key, unsigned int value, Ogre::String section=Ogre::BLANKSTRING)
Ogre::StringVector getSettingStringVector(Ogre::String key, Ogre::String section=Ogre::BLANKSTRING)
Adds direct parsing of custom types.
Definition ConfigFile.h:38
Ogre::String getString(Ogre::String const &key, Ogre::String const &section, Ogre::String const &defaultValue="")