Rewrite fs:GetDirListing(file) by kahrl
[oweals/minetest.git] / src / settings.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef SETTINGS_HEADER
21 #define SETTINGS_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "util/string.h"
25 #include "jthread/jmutex.h"
26 #include <string>
27 #include <map>
28 #include <list>
29 #include <set>
30
31 class Settings;
32 struct NoiseParams;
33
34 /** function type to register a changed callback */
35 typedef void (*setting_changed_callback)(const std::string);
36
37 enum ValueType {
38         VALUETYPE_STRING,
39         VALUETYPE_FLAG // Doesn't take any arguments
40 };
41
42 enum SettingsParseEvent {
43         SPE_NONE,
44         SPE_INVALID,
45         SPE_COMMENT,
46         SPE_KVPAIR,
47         SPE_END,
48         SPE_GROUP,
49         SPE_MULTILINE,
50 };
51
52 struct ValueSpec {
53         ValueSpec(ValueType a_type, const char *a_help=NULL)
54         {
55                 type = a_type;
56                 help = a_help;
57         }
58         ValueType type;
59         const char *help;
60 };
61
62 /** function type to register a changed callback */
63
64 struct SettingsEntry {
65         SettingsEntry()
66         {
67                 group = NULL;
68         }
69
70         SettingsEntry(const std::string &value_)
71         {
72                 value = value_;
73                 group = NULL;
74         }
75
76         SettingsEntry(Settings *group_)
77         {
78                 group = group_;
79         }
80
81         SettingsEntry(const std::string &value_, Settings *group_)
82         {
83                 value = value_;
84                 group = group_;
85         }
86
87         std::string value;
88         Settings *group;
89 };
90
91
92 class Settings {
93 public:
94         Settings() {}
95         ~Settings();
96
97         Settings & operator += (const Settings &other);
98         Settings & operator = (const Settings &other);
99
100
101         /***********************
102          * Reading and writing *
103          ***********************/
104
105         // Read configuration file.  Returns success.
106         bool readConfigFile(const char *filename);
107         //Updates configuration file.  Returns success.
108         bool updateConfigFile(const char *filename);
109         // NOTE: Types of allowed_options are ignored.  Returns success.
110         bool parseCommandLine(int argc, char *argv[],
111                         std::map<std::string, ValueSpec> &allowed_options);
112         bool parseConfigLines(std::istream &is, const std::string &end = "");
113         void writeLines(std::ostream &os, u32 tab_depth=0) const;
114
115         SettingsParseEvent parseConfigObject(const std::string &line,
116                 const std::string &end, std::string &name, std::string &value);
117         void getNamesPresent(std::istream &is, const std::string &end,
118                 std::set<std::string> &present_values,
119                 std::set<std::string> &present_groups);
120         bool updateConfigObject(std::istream &is, std::ostream &os,
121                 const std::string &end, u32 tab_depth=0);
122
123         static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
124         static std::string sanitizeString(const std::string &value);
125         static bool printEntry(std::ostream &os, const std::string &name,
126                 const SettingsEntry &entry, u32 tab_depth=0);
127         static void printValue(std::ostream &os, const std::string &name,
128                 const std::string &value, u32 tab_depth=0);
129         static void printGroup(std::ostream &os, const std::string &name,
130                 const Settings *group, u32 tab_depth=0);
131
132         /***********
133          * Getters *
134          ***********/
135
136         const SettingsEntry &getEntry(const std::string &name) const;
137         Settings *getGroup(const std::string &name) const;
138         std::string get(const std::string &name) const;
139         bool getBool(const std::string &name) const;
140         u16 getU16(const std::string &name) const;
141         s16 getS16(const std::string &name) const;
142         s32 getS32(const std::string &name) const;
143         u64 getU64(const std::string &name) const;
144         float getFloat(const std::string &name) const;
145         v2f getV2F(const std::string &name) const;
146         v3f getV3F(const std::string &name) const;
147         u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
148                         u32 *flagmask) const;
149         // N.B. if getStruct() is used to read a non-POD aggregate type,
150         // the behavior is undefined.
151         bool getStruct(const std::string &name, const std::string &format,
152                         void *out, size_t olen) const;
153         bool getNoiseParams(const std::string &name, NoiseParams &np) const;
154         bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
155         bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
156
157         // return all keys used
158         std::vector<std::string> getNames() const;
159         bool exists(const std::string &name) const;
160
161
162         /***************************************
163          * Getters that don't throw exceptions *
164          ***************************************/
165
166         bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
167         bool getGroupNoEx(const std::string &name, Settings *&val) const;
168         bool getNoEx(const std::string &name, std::string &val) const;
169         bool getFlag(const std::string &name) const;
170         bool getU16NoEx(const std::string &name, u16 &val) const;
171         bool getS16NoEx(const std::string &name, s16 &val) const;
172         bool getS32NoEx(const std::string &name, s32 &val) const;
173         bool getU64NoEx(const std::string &name, u64 &val) const;
174         bool getFloatNoEx(const std::string &name, float &val) const;
175         bool getV2FNoEx(const std::string &name, v2f &val) const;
176         bool getV3FNoEx(const std::string &name, v3f &val) const;
177         // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
178         // val must be initialized before using getFlagStrNoEx().  The intention of
179         // this is to simplify modifying a flags field from a default value.
180         bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
181
182
183         /***********
184          * Setters *
185          ***********/
186
187         // N.B. Groups not allocated with new must be set to NULL in the settings
188         // tree before object destruction.
189         void set(const std::string &name, const std::string &value);
190         void setGroup(const std::string &name, Settings *group);
191         void setDefault(const std::string &name, const std::string &value);
192         void setGroupDefault(const std::string &name, Settings *group);
193         void setBool(const std::string &name, bool value);
194         void setS16(const std::string &name, s16 value);
195         void setU16(const std::string &name, u16 value);
196         void setS32(const std::string &name, s32 value);
197         void setU64(const std::string &name, u64 value);
198         void setFloat(const std::string &name, float value);
199         void setV2F(const std::string &name, v2f value);
200         void setV3F(const std::string &name, v3f value);
201         void setFlagStr(const std::string &name, u32 flags,
202                 const FlagDesc *flagdesc, u32 flagmask);
203         void setNoiseParams(const std::string &name, const NoiseParams &np);
204         // N.B. if setStruct() is used to write a non-POD aggregate type,
205         // the behavior is undefined.
206         bool setStruct(const std::string &name, const std::string &format, void *value);
207
208         // remove a setting
209         bool remove(const std::string &name);
210         void clear();
211         void updateValue(const Settings &other, const std::string &name);
212         void update(const Settings &other);
213         void registerChangedCallback(std::string name, setting_changed_callback cbf);
214
215 private:
216
217         void updateNoLock(const Settings &other);
218         void clearNoLock();
219
220         void doCallbacks(std::string name);
221
222         std::map<std::string, SettingsEntry> m_settings;
223         std::map<std::string, SettingsEntry> m_defaults;
224         std::map<std::string, std::vector<setting_changed_callback> > m_callbacks;
225         // All methods that access m_settings/m_defaults directly should lock this.
226         mutable JMutex m_mutex;
227 };
228
229 #endif
230