]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.h
f0ef9f6b28a243bff65ea0a2e55d779dcaff6a33
[dragonfireclient.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 enum ValueType
32 {
33         VALUETYPE_STRING,
34         VALUETYPE_FLAG // Doesn't take any arguments
35 };
36
37 struct ValueSpec
38 {
39         ValueSpec(ValueType a_type, const char *a_help=NULL)
40         {
41                 type = a_type;
42                 help = a_help;
43         }
44         ValueType type;
45         const char *help;
46 };
47
48
49 class Settings
50 {
51 public:
52         Settings() {}
53
54         Settings & operator += (const Settings &other);
55         Settings & operator = (const Settings &other);
56
57
58         /***********************
59          * Reading and writing *
60          ***********************/
61
62         // Read configuration file.  Returns success.
63         bool readConfigFile(const char *filename);
64         //Updates configuration file.  Returns success.
65         bool updateConfigFile(const char *filename);
66         // NOTE: Types of allowed_options are ignored.  Returns success.
67         bool parseCommandLine(int argc, char *argv[],
68                         std::map<std::string, ValueSpec> &allowed_options);
69         bool parseConfigLines(std::istream &is, const std::string &end = "");
70         void writeLines(std::ostream &os) const;
71
72
73         /***********
74          * Getters *
75          ***********/
76
77         std::string get(const std::string &name) const;
78         bool getBool(const std::string &name) const;
79         u16 getU16(const std::string &name) const;
80         s16 getS16(const std::string &name) const;
81         s32 getS32(const std::string &name) const;
82         u64 getU64(const std::string &name) const;
83         float getFloat(const std::string &name) const;
84         v2f getV2F(const std::string &name) const;
85         v3f getV3F(const std::string &name) const;
86         u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
87                         u32 *flagmask) const;
88         // N.B. if getStruct() is used to read a non-POD aggregate type,
89         // the behavior is undefined.
90         bool getStruct(const std::string &name, const std::string &format,
91                         void *out, size_t olen) const;
92
93         // return all keys used
94         std::vector<std::string> getNames() const;
95         bool exists(const std::string &name) const;
96
97
98         /***************************************
99          * Getters that don't throw exceptions *
100          ***************************************/
101
102         bool getNoEx(const std::string &name, std::string &val) const;
103         bool getFlag(const std::string &name) const;
104         bool getU16NoEx(const std::string &name, u16 &val) const;
105         bool getS16NoEx(const std::string &name, s16 &val) const;
106         bool getS32NoEx(const std::string &name, s32 &val) const;
107         bool getU64NoEx(const std::string &name, u64 &val) const;
108         bool getFloatNoEx(const std::string &name, float &val) const;
109         bool getV2FNoEx(const std::string &name, v2f &val) const;
110         bool getV3FNoEx(const std::string &name, v3f &val) const;
111         // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
112         // val must be initialized before using getFlagStrNoEx().  The intention of
113         // this is to simplify modifying a flags field from a default value.
114         bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
115
116
117         /***********
118          * Setters *
119          ***********/
120
121         void set(const std::string &name, std::string value);
122         void set(const std::string &name, const char *value);
123         void setDefault(const std::string &name, std::string value);
124         void setBool(const std::string &name, bool value);
125         void setS16(const std::string &name, s16 value);
126         void setS32(const std::string &name, s32 value);
127         void setU64(const std::string &name, u64 value);
128         void setFloat(const std::string &name, float value);
129         void setV2F(const std::string &name, v2f value);
130         void setV3F(const std::string &name, v3f value);
131         void setFlagStr(const std::string &name, u32 flags,
132                 const FlagDesc *flagdesc, u32 flagmask);
133         // N.B. if setStruct() is used to write a non-POD aggregate type,
134         // the behavior is undefined.
135         bool setStruct(const std::string &name, const std::string &format, void *value);
136
137         // remove a setting
138         bool remove(const std::string &name);
139         void clear();
140         void updateValue(const Settings &other, const std::string &name);
141         void update(const Settings &other);
142
143
144 private:
145         /***********************
146          * Reading and writing *
147          ***********************/
148
149         bool parseConfigObject(std::istream &is,
150                         std::string &name, std::string &value);
151         bool parseConfigObject(std::istream &is,
152                         std::string &name, std::string &value,
153                         const std::string &end, bool &end_found);
154         /*
155          * Reads a configuration object from stream (usually a single line)
156          * and adds it to dst.
157          * Preserves comments and empty lines.
158          * Setting names that were added to dst are also added to updated.
159          */
160         void getUpdatedConfigObject(std::istream &is,
161                         std::list<std::string> &dst,
162                         std::set<std::string> &updated,
163                         bool &changed);
164
165
166         void updateNoLock(const Settings &other);
167         void clearNoLock();
168
169
170         std::map<std::string, std::string> m_settings;
171         std::map<std::string, std::string> m_defaults;
172         // All methods that access m_settings/m_defaults directly should lock this.
173         mutable JMutex m_mutex;
174 };
175
176 #endif
177