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