This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
settings.cpp
206 lines (166 loc) · 6.62 KB
/
settings.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* p4sync
* http://snarfed.org/p4sync
*
* Copyright 2005
* Ryan Barrett <p4@ryanb.org>
*
* @file settings.cpp
*
* Implements the csettings class. For more information, see csettings.h.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "settings.h"
#include "sys.h"
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <algorithm>
using namespace std;
/*****************************************************************************
* PUBLIC METHODS
*****************************************************************************/
struct print_setting {
print_setting(ostream &ostr) : ostr(ostr) {}
void operator() (const pair<string, string> &elem) {
ostr.width(35);
ostr.fill(' ');
ostr.flags(ios::left);
ostr << elem.first.c_str();
ostr << elem.second << endl;
}
private:
ostream &ostr;
};
ostream &operator<<(ostream &ostr, const csettings &cs)
{
for_each(cs.settings.begin(), cs.settings.end(), print_setting(ostr));
return ostr;
}
void csettings::write()
{
// TODO: preserve order and comments in settings file
ofstream file(kfilename.c_str());
if (!file.is_open())
return;
for (map<string, string>::iterator it = settings.begin();
it != settings.end(); it++)
file << it->first << " = " << it->second << endl;
}
/*****************************************************************************
* PRIVATE METHODS
*****************************************************************************/
// explicit template specializations for defvalue(). (no default definition!)
template <> string csettings::defvalue() { return ""; }
template <> bool csettings::defvalue() { return false; }
template <> uint csettings::defvalue() { return 0; }
template <> ushort csettings::defvalue() { return defvalue<uint>(); }
template <> ulong csettings::defvalue() { return defvalue<uint>(); }
template <> int csettings::defvalue() { return defvalue<uint>(); }
template <> float csettings::defvalue() { return defvalue<uint>(); }
template <> double csettings::defvalue() { return defvalue<uint>(); }
// ctor
csettings::csettings() : kfilename("p4sync.cfg")
{
// these constants are here, instead of a static constant in the csettings
// class, because it's used in other static initialization that happens
// before the csettings class's static initialization
static const char kcomment_char = '#', kequals_char = '=';
// p4sync default settings. if a settings file is found, its values will
// override these values
settings["trace.master"] = "true";
settings["trace.client"] = "true";
settings["trace.server"] = "true";
settings["trace.player"] = "true";
settings["trace.xmms"] = "false";
settings["trace.zinf"] = "false";
settings["trace.mpc"] = "true";
settings["run_server"] = "false";
settings["sync_to_host"] = "localhost";
settings["sync_to_port"] = "9044";
settings["sync_freq_ms"] = "30000";
settings["server_port"] = "9044";
settings["serve_freq_ms"] = "100";
ifstream file(kfilename.c_str());
if (!file.is_open())
return;
file.setf(ios::skipws); // skip whitespace
while (file.good()) {
string key;
file >> key;
if (key[0] == kcomment_char) { // file comment
const int bufsize = 256;
char buf[256];
file.getline(buf, bufsize);
continue;
}
char equals;
string value;
file >> equals >> value; // new setting to add!
if (equals == kequals_char && !key.empty() && !value.empty())
settings[key] = value;
}
}
csettings::~csettings()
{
// cerr << "csettings dtor()" << endl;
}
/*****************************************************************************
* TEST MAIN
*****************************************************************************/
// this line shows how a static constant could be initialized w/csettings
//
// static const int kexample = csettings::instance().get<int>("setting");
/*
int main(int argc, char **argv)
{
csettings &s = csettings::instance();
const string a = "a", b = "b", c = "c";
s.set(a, string("asdf"));
s.set(b, 1.23f);
s.set(c, 4);
cout.setf(ios::boolalpha); // this requires gcc 3 or greater
cout << "a's value is " << s.get<string>(a) << endl;
cout << "b's value is " << s.get<float>(b) << endl;
cout << "c's value is " << s.get<int>(c) << endl;
cout << "d's value is " << s.get<string>("d") << endl;
cout << "a's value as an int is " << s.get<int>(a) << endl;
cout << "d's value as an int is " << s.get<int>("d") << endl;
cout << "c's value as a bool is " << s.get<bool>("c") << endl;
cout << "setting c's value to false..." << endl;
s.set(c, false);
cout << "c's value as a string is " << s.get<string>(c) << endl;
cout << "c's value as a bool is " << s.get<bool>(c) << endl;
cout << endl << "looking up values in dotfile \".p4\"..." << endl;
cout << "test1's value is " << s.get<string>("test1") << endl;
cout << "test2's value is " << s.get<string>("test2") << endl;
cout << "test3's value is " << s.get<string>("test3") << endl;
// cout << "a's value with sget is " << sget<string>(a) << endl;
cout << "result of cout << csettings::instance() :" << endl
<< csettings::instance();
cout << "unknown string setting: " << s.get<string>("foo") << endl;
cout << "unknown bool setting: " << s.get<bool>("foo") << endl;
cout << "unknown uint setting: " << s.get<uint>("foo") << endl;
cout << "unknown ushort setting: " << s.get<ushort>("foo") << endl;
cout << "unknown ulong setting: " << s.get<ulong>("foo") << endl;
cout << "unknown int setting: " << s.get<int>("foo") << endl;
cout << "unknown float setting: " << s.get<float>("foo") << endl;
cout << "unknown double setting: " << s.get<double>("foo") << endl;
bool bar = s.get<bool>("foo");
cout << (bar ? "true!" : "false!") << endl;
s.write();
return 0;
}*/