-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cpp
115 lines (86 loc) · 2.66 KB
/
Config.cpp
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
#include "Config.h"
#include "ConfigFiles.h"
ConfigFile::ConfigFile(const std::string & filename) {
_isLoaded = false;
Load(filename);
}
ConfigFile::~ConfigFile() {
// delete all void pointers
for (std::map<std::string, ConfigEntry>::iterator i = _entries.begin();
i != _entries.end(); i++) {
delete i->second.Value;
}
}
void ConfigFile::Save() {
if (!_isLoaded) return;
std::ofstream outfile(_filename.c_str());
if (!outfile) {
return;
}
for (std::map<std::string, ConfigEntry>::iterator i = _entries.begin();
i != _entries.end(); i++) {
outfile << i->first << " ";
switch(i->second.Type) {
case Int:
outfile << *((int *) i->second.Value);
break;
case Float:
outfile << *((float *) i->second.Value);
break;
case Bool:
if (*((bool *) i->second.Value))
outfile << "true";
else
outfile << "false";
break;
case String:
outfile << *((std::string *) i->second.Value);
break;
}
outfile << std::endl;
}
outfile.close();
}
void ConfigFile::Load(const std::string & filename) {
// if this has a file loaded release it first
if (_isLoaded) {
for (std::map<std::string, ConfigEntry>::iterator i = _entries.begin();
i != _entries.end(); i++) {
delete i->second.Value;
}
}
std::ifstream infile(filename.c_str());
std::string name = "", val = "";
if (!infile) {
_isLoaded = false;
return;
}
// read in all the settings
while (!infile.eof()) {
infile >> name;
infile.ignore();
std::getline(infile, val);
//val.erase(--val.end()); // erase newline
// figure out the type of data
if (atoi(val.c_str()) != 0) {
// INTEGER
_entries[name] = ConfigEntry(Int, (void *) new int(atoi(val.c_str())) );
} else if (atof(val.c_str()) != 0.0f) {
// FLOAT
_entries[name] = ConfigEntry(Float, (void *) new float(atof(val.c_str())) );
} else if (val.find(std::string("true")) == 0 ||
val.find(std::string("false")) == 0) {
// BOOL
if (val.find(std::string("true")) == 0)
_entries[name] = ConfigEntry(Bool, (void *) new bool(true));
else
_entries[name] = ConfigEntry(Bool, (void *) new bool(false));
} else {
// STRING
_entries[name] = ConfigEntry(String, (void *) new std::string(val));
}
}
infile.close();
_filename = filename;
_isLoaded = true;
}