forked from OpenApoc/OpenApoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.h
128 lines (103 loc) · 3.67 KB
/
configfile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include "library/sp.h"
#include "library/strings.h"
#include <boost/any.hpp>
#include <map>
#include <vector>
namespace OpenApoc
{
class ConfigFileImpl;
class ConfigOption;
class ConfigFile
{
private:
up<ConfigFileImpl> pimpl;
ConfigFile();
public:
~ConfigFile();
bool save();
// Returns true if the settings have been read
bool loaded() const;
int getInt(const UString &key);
bool getBool(const UString &key);
UString getString(const UString &key);
float getFloat(const UString &key);
bool get(const UString &key); // returns true if the option was specified
UString describe(const UString §ion, const UString &name);
void set(const UString &key, const bool value);
void set(const UString &key, const int value);
void set(const UString &key, const UString value);
void set(const UString &key, const float value);
void addOptionString(const UString section, const UString longName, const UString shortName,
const UString description, const UString defaultValue);
void addOptionInt(const UString section, const UString longName, const UString shortName,
const UString description, const int defaultValue);
void addOptionBool(const UString section, const UString longName, const UString shortName,
const UString description, const bool defaultValue);
void addOptionFloat(const UString section, const UString longName, const UString shortName,
const UString description, const float defaultValue);
void addOption(const UString section, const UString longName, const UString shortName,
const UString description);
void addPositionalArgument(const UString name, const UString description);
// returns 'true' if the program should exit (invalid option/'--help' specified)
bool parseOptions(int argc, char *argv[]);
// Prints out the help to stdout, used if the running program has decided some argument is
// invalid
void showHelp();
std::map<UString, std::vector<ConfigOption>> getOptions();
static ConfigFile &getInstance();
};
class ConfigOption
{
private:
UString section;
UString name;
UString description;
public:
ConfigOption(const UString section, const UString name, const UString description);
UString getName() const { return name; }
UString getDescription() const { return description; }
UString getKey() const;
};
class ConfigOptionString : public ConfigOption
{
private:
UString defaultValue;
public:
ConfigOptionString(const UString section, const UString name, const UString description,
const UString defaultValue = "");
UString get() const;
};
class ConfigOptionInt : public ConfigOption
{
private:
int defaultValue;
public:
ConfigOptionInt(const UString section, const UString name, const UString description,
const int defaultValue = 0);
int get() const;
};
class ConfigOptionBool : public ConfigOption
{
private:
bool defaultValue;
public:
ConfigOptionBool(const UString section, const UString name, const UString description,
const bool defaultValue = false);
bool get() const;
};
class ConfigOptionFloat : public ConfigOption
{
private:
float defaultValue;
public:
ConfigOptionFloat(const UString section, const UString name, const UString description,
const float defaultValue = 0.0f);
bool get() const;
};
static inline ConfigFile &config() { return ConfigFile::getInstance(); }
// validate overload required by boost::program_options for UString
// boost should find this through ADL.
// this is required for string values with spaces
void validate(boost::any &v, const std::vector<std::string> &values, UString *, int);
}; // namespace OpenApoc