-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.cpp
116 lines (100 loc) · 2.76 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
116
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include "config.h"
#include "resources.h"
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
using namespace std;
EMBED(defaultConfig, default.cfg);
Config* Config::instance = NULL;
Config::Value Config::createNullValue()
{
Value v;
v.i = 0;
v.f = 0.0f;
return v;
}
Config::Config()
{
ifstream ifile(CONFIG_NAME);
//check if config opened
if (!ifile.is_open())
{
//write default config
int len = RESOURCELEN(defaultConfig);
if (len > 0)
{
cout << "Note: Config file " << CONFIG_NAME << " does not exist. A default one is being created." << endl;
ofstream ofile(CONFIG_NAME, ios::binary);
//printf("%i %s\n", len, RESOURCE(defaultConfig));
ofile.write(RESOURCE(defaultConfig), len);
ofile.close();
}
//try again
ifile.open(CONFIG_NAME);
if (!ifile.is_open())
{
cout << "Error: Config file " << CONFIG_NAME << " does not exist and cannot be created." << endl;
return;
}
}
string line;
while (getline(ifile, line))
{
//extract key/value lines
int p = (int)line.find_first_of(":");
if (p < 0)
continue;
//get the key
string key = line.substr(0, p);
++p;
//strip whitespace AND STUPID WINDOWS CARRIAGE RETURNS
while (p < (int)line.size() && line[p] == ' ')
++p;
int e = (int)line.size() - 1;
while (line[e] == ' ' || line[e] == '\r')
--e;
line = line.substr(p, e - p + 1);
Value value;
value.str = line;
stringstream reader(line);
reader >> value.i; reader.seekg(0);
reader >> value.f; reader.seekg(0);
values[key] = value;
}
}
Config& Config::getSingleton()
{
if (!instance)
instance = new Config();
return *instance;
}
int Config::get(std::string name)
{
ValueMap::iterator it = getSingleton().values.find(name);
if (it != getSingleton().values.end())
return it->second.i;
cout << "Error: Missing config value for " << name << endl;
getSingleton().values[name] = createNullValue(); //don't print error again
return 0;
}
float Config::getFloat(std::string name)
{
ValueMap::iterator it = getSingleton().values.find(name);
if (it != getSingleton().values.end())
return it->second.f;
cout << "Error: Missing config value for " << name << endl;
getSingleton().values[name] = createNullValue(); //don't print error again
return 0.0f;
}
std::string Config::getString(std::string name)
{
ValueMap::iterator it = getSingleton().values.find(name);
if (it != getSingleton().values.end())
return it->second.str;
cout << "Error: Missing config value for " << name << endl;
getSingleton().values[name] = createNullValue(); //don't print error again
return "";
}