-
Notifications
You must be signed in to change notification settings - Fork 12
/
Config.h
executable file
·97 lines (84 loc) · 2.16 KB
/
Config.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
#ifndef CONFIG_H
#define CONFIG_H
#include <map>
using std::map;
#include <string>
using std::string;
#include "Util.h"
class Config
{
private:
map<string, vector<string> > config;
string configfilename;
struct CombiKey
{
string base;
size_t id;
string prop;
CombiKey(){id=-1;}
~CombiKey(){}
};
//returns default (base "", id -1, prop "") if key is not a CombiKey
CombiKey GetCombiKey(string keyname)
{
CombiKey cs;
size_t dotplace = keyname.find('.');
if (dotplace == string::npos || dotplace == keyname.length()-1)
return cs;
string base_plus_id = keyname.substr(0,dotplace);
string prop = keyname.substr(dotplace+1);
if (base_plus_id == "" || prop == "")
return cs;
size_t idplace = base_plus_id.find_last_not_of("0123456789");
if (idplace == base_plus_id.length()-1)
return cs;
string base = base_plus_id.substr(0,idplace+1);
if (base == "")
return cs;
size_t id = FromString<uint>(base_plus_id.substr(idplace+1));
cs.base = base;
cs.id = id;
cs.prop = prop;
return cs;
}
public:
//function disabled because of "include" functionality
//void Save(string filename);
void Load(string filename, vector<string> included_already = vector<string>());
void Clear();
template<typename T>
void SetValue(string key, uint index, T val)
{
if (config[key].size() < index)
return;
if (config[key].size() == index)
config[key].push_back(ToString(val));
else
config[key][index] = ToString(val);
}
template<typename T>
void SetCombiValue(string base, size_t id, string prop, uint index, T val)
{
string key = base + ToString(id) + "." + prop;
SetValue<T>(key, index, val);
}
template<typename T>
T GetValue(string key, uint index = 0)
{
if (config[key].size() <= index)
return T();
return FromString<T>(config[key][index]);
}
template<typename T>
T GetCombiValue(string base, size_t id, string prop, uint index = 0)
{
string key = base + ToString(id) + "." + prop;
return GetValue<T>(key, index);
}
uint GetValueCount(string key)
{
return (uint)config[key].size();
}
};
extern Config config;
#endif