Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
ConfigFile.cpp
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
28#include "ConfigFile.h"
29
30#include "Application.h"
31#include "Console.h"
32#include "Utils.h"
33
34#include <OgreConfigFile.h>
35#include <OgreString.h>
36#include <OgreStringConverter.h>
37
38using namespace RoR;
39
40float ConfigFile::getFloat(Ogre::String const& key, Ogre::String const& section, float defaultValue)
41{
42 return Ogre::StringConverter::parseReal(Ogre::ConfigFile::getSetting(key, section), defaultValue);
43}
44
45Ogre::ColourValue ConfigFile::getColourValue(Ogre::String const& key, Ogre::String const& section, Ogre::ColourValue const& defaultValue)
46{
47 Ogre::ColourValue result;
48 Ogre::String value = Ogre::ConfigFile::getSetting(key, section);
49 if (Ogre::StringConverter::parse(value, result))
50 {
51 return result;
52 }
53 else
54 {
55 this->logMessage(
56 fmt::format("Could not parse '{}/{}' ({}) as color, format must be 'R G B A' or 'R G B', falling back to '{}'",
57 section, key, value, Ogre::StringConverter::toString(defaultValue)));
58 return defaultValue;
59 }
60}
61
62Ogre::Vector3 ConfigFile::getVector3(Ogre::String const& key, Ogre::String const& section, Ogre::Vector3 const& defaultValue)
63{
64 Ogre::Vector3 result;
65 Ogre::String value = Ogre::ConfigFile::getSetting(key, section);
66 if (Ogre::StringConverter::parse(value, result))
67 {
68 return result;
69 }
70 else
71 {
72 this->logMessage(
73 fmt::format("Could not parse '{}/{}' ({}) as vector3, format must be 'X Y Z', falling back to '{}'",
74 section, key, value, Ogre::StringConverter::toString(defaultValue)));
75 return defaultValue;
76 }
77}
78
79int ConfigFile::getInt(Ogre::String const& key, Ogre::String const& section, int defaultValue)
80{
81 return Ogre::StringConverter::parseInt(Ogre::ConfigFile::getSetting(key, section), defaultValue);
82}
83
84bool ConfigFile::getBool(Ogre::String const& key, Ogre::String const& section, bool defaultValue)
85{
86 return Ogre::StringConverter::parseBool(Ogre::ConfigFile::getSetting(key, section), defaultValue);
87}
88
89Ogre::String ConfigFile::getString(Ogre::String const& key, Ogre::String const& section, Ogre::String const& defaultValue)
90{
91 auto setting = Ogre::ConfigFile::getSetting(key, section);
92 if (setting.empty())
93 {
94 return defaultValue;
95 }
96 return SanitizeUtf8String(setting);
97}
98
99void ConfigFile::SetString(Ogre::String key, Ogre::String value, Ogre::String section /* = Ogre::BLANKSTRING */)
100{
101 SettingsMultiMap* set = mSettingsPtr[section];
102 if (!set)
103 {
104 // new section
105 set = new SettingsMultiMap();
106 mSettingsPtr[section] = set;
107 }
108 if (set->count(key))
109 {
110 // known key, delete old first
111 set->erase(key);
112 }
113 // add key
114 set->insert(std::multimap<Ogre::String, Ogre::String>::value_type(key, value));
115}
116
117bool ConfigFile::HasSection(std::string const & name)
118{
119 // This is the only way to check existence of section
120 // without either an OGRE exception being logged or using deprecated API.
121 return this->getSettingsBySection().find(name) != this->getSettingsBySection().end();
122}
123
124bool ConfigFile::HasSetting(std::string const& name, std::string const& key)
125{
126 auto section = this->getSettingsBySection().find(name);
127 if (section == this->getSettingsBySection().end())
128 {
129 return false;
130 }
131 return section->second.find(key) != section->second.end();
132}
133
134void ConfigFile::logMessage(std::string const & msg)
135{
136 // If filename is specified, log "filename: message", otherwise just "message".
137 LOG(fmt::format("{}{}{}", m_log_filename, (m_log_filename == "" ? "" : ": "), msg));
138}
Central state/object manager and communications hub.
void LOG(const char *msg)
Legacy alias - formerly a macro.
bool getBool(Ogre::String const &key, bool defaultValue=false)
Definition ConfigFile.h:57
float getFloat(Ogre::String const &key, float defaultValue=0.f)
Definition ConfigFile.h:50
void logMessage(std::string const &msg)
bool HasSection(std::string const &name)
std::string m_log_filename
Definition ConfigFile.h:92
void SetString(Ogre::String key, Ogre::String value, Ogre::String section=Ogre::BLANKSTRING)
Ogre::String getString(Ogre::String const &key, Ogre::String const &section, Ogre::String const &defaultValue="")
Ogre::Vector3 getVector3(Ogre::String const &key, Ogre::String const &section, Ogre::Vector3 const &defaultValue=Ogre::Vector3::ZERO)
int getInt(Ogre::String const &key, int defaultValue=0)
Definition ConfigFile.h:64
Ogre::ColourValue getColourValue(Ogre::String const &key, Ogre::ColourValue const &defaultValue=Ogre::ColourValue())
Definition ConfigFile.h:41
bool HasSetting(std::string const &section, std::string const &key)
std::string SanitizeUtf8String(std::string const &str_in)
Definition Utils.cpp:120