3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef SETTINGS_HEADER
21 #define SETTINGS_HEADER
23 #include "irrlichttypes_bloated.h"
24 #include "util/string.h"
25 #include "threading/mutex.h"
35 extern Settings *g_settings;
36 extern std::string g_settings_path;
38 // Type for a settings changed callback function
39 typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
43 SettingsChangedCallback,
46 > SettingsCallbackList;
48 typedef std::map<std::string, SettingsCallbackList> SettingsCallbackMap;
52 VALUETYPE_FLAG // Doesn't take any arguments
55 enum SettingsParseEvent {
66 ValueSpec(ValueType a_type, const char *a_help=NULL)
76 struct SettingsEntry {
83 SettingsEntry(const std::string &value_)
90 SettingsEntry(Settings *group_)
106 Settings & operator += (const Settings &other);
107 Settings & operator = (const Settings &other);
109 /***********************
110 * Reading and writing *
111 ***********************/
113 // Read configuration file. Returns success.
114 bool readConfigFile(const char *filename);
115 //Updates configuration file. Returns success.
116 bool updateConfigFile(const char *filename);
117 // NOTE: Types of allowed_options are ignored. Returns success.
118 bool parseCommandLine(int argc, char *argv[],
119 std::map<std::string, ValueSpec> &allowed_options);
120 bool parseConfigLines(std::istream &is, const std::string &end = "");
121 void writeLines(std::ostream &os, u32 tab_depth=0) const;
123 SettingsParseEvent parseConfigObject(const std::string &line,
124 const std::string &end, std::string &name, std::string &value);
125 bool updateConfigObject(std::istream &is, std::ostream &os,
126 const std::string &end, u32 tab_depth=0);
128 static bool checkNameValid(const std::string &name);
129 static bool checkValueValid(const std::string &value);
130 static std::string sanitizeName(const std::string &name);
131 static std::string sanitizeValue(const std::string &value);
132 static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
133 static void printEntry(std::ostream &os, const std::string &name,
134 const SettingsEntry &entry, u32 tab_depth=0);
140 const SettingsEntry &getEntry(const std::string &name) const;
141 Settings *getGroup(const std::string &name) const;
142 std::string get(const std::string &name) const;
143 bool getBool(const std::string &name) const;
144 u16 getU16(const std::string &name) const;
145 s16 getS16(const std::string &name) const;
146 s32 getS32(const std::string &name) const;
147 u64 getU64(const std::string &name) const;
148 float getFloat(const std::string &name) const;
149 v2f getV2F(const std::string &name) const;
150 v3f getV3F(const std::string &name) const;
151 u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
152 u32 *flagmask) const;
153 // N.B. if getStruct() is used to read a non-POD aggregate type,
154 // the behavior is undefined.
155 bool getStruct(const std::string &name, const std::string &format,
156 void *out, size_t olen) const;
157 bool getNoiseParams(const std::string &name, NoiseParams &np) const;
158 bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
159 bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
161 // return all keys used
162 std::vector<std::string> getNames() const;
163 bool exists(const std::string &name) const;
166 /***************************************
167 * Getters that don't throw exceptions *
168 ***************************************/
170 bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
171 bool getGroupNoEx(const std::string &name, Settings *&val) const;
172 bool getNoEx(const std::string &name, std::string &val) const;
173 bool getFlag(const std::string &name) const;
174 bool getU16NoEx(const std::string &name, u16 &val) const;
175 bool getS16NoEx(const std::string &name, s16 &val) const;
176 bool getS32NoEx(const std::string &name, s32 &val) const;
177 bool getU64NoEx(const std::string &name, u64 &val) const;
178 bool getFloatNoEx(const std::string &name, float &val) const;
179 bool getV2FNoEx(const std::string &name, v2f &val) const;
180 bool getV3FNoEx(const std::string &name, v3f &val) const;
181 // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
182 // val must be initialized before using getFlagStrNoEx(). The intention of
183 // this is to simplify modifying a flags field from a default value.
184 bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
191 // N.B. Groups not allocated with new must be set to NULL in the settings
192 // tree before object destruction.
193 bool setEntry(const std::string &name, const void *entry,
194 bool set_group, bool set_default);
195 bool set(const std::string &name, const std::string &value);
196 bool setDefault(const std::string &name, const std::string &value);
197 bool setGroup(const std::string &name, Settings *group);
198 bool setGroupDefault(const std::string &name, Settings *group);
199 bool setBool(const std::string &name, bool value);
200 bool setS16(const std::string &name, s16 value);
201 bool setU16(const std::string &name, u16 value);
202 bool setS32(const std::string &name, s32 value);
203 bool setU64(const std::string &name, u64 value);
204 bool setFloat(const std::string &name, float value);
205 bool setV2F(const std::string &name, v2f value);
206 bool setV3F(const std::string &name, v3f value);
207 bool setFlagStr(const std::string &name, u32 flags,
208 const FlagDesc *flagdesc, u32 flagmask);
209 bool setNoiseParams(const std::string &name, const NoiseParams &np,
210 bool set_default=false);
211 // N.B. if setStruct() is used to write a non-POD aggregate type,
212 // the behavior is undefined.
213 bool setStruct(const std::string &name, const std::string &format, void *value);
216 bool remove(const std::string &name);
218 void clearDefaults();
219 void updateValue(const Settings &other, const std::string &name);
220 void update(const Settings &other);
222 void registerChangedCallback(const std::string &name,
223 SettingsChangedCallback cbf, void *userdata = NULL);
224 void deregisterChangedCallback(const std::string &name,
225 SettingsChangedCallback cbf, void *userdata = NULL);
228 void updateNoLock(const Settings &other);
230 void clearDefaultsNoLock();
232 void doCallbacks(const std::string &name) const;
234 std::map<std::string, SettingsEntry> m_settings;
235 std::map<std::string, SettingsEntry> m_defaults;
237 SettingsCallbackMap m_callbacks;
239 mutable Mutex m_callback_mutex;
241 // All methods that access m_settings/m_defaults directly should lock this.
242 mutable Mutex m_mutex;