-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbConfig.cpp
More file actions
211 lines (185 loc) · 5 KB
/
fbConfig.cpp
File metadata and controls
211 lines (185 loc) · 5 KB
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
207
208
209
210
211
/* $Id: fbConfig.cpp,v 1.13 2008/04/23 00:41:11 ctubbsii Exp $ */
/*
* Copyright (c) 2008 Chris Tubbs
* All rights reserved.
* Do whatever you want with this code.
*
*/
#include "fbConfig.h"
/**
* fbConfig
*
* Provides an interface for loading and saving configuration data.
* @author Christopher Tubbs
* @date March 18, 2008
*/
fbConfig::fbConfig(fbErrorLogger* err): port(0), errlog(err), dirty(true)
{
loadDefaults(); /* assume defaults */
if (load() < 0)
{
errlog->warn(CONFIGFAILEDTOLOAD, "using defaults");
dirty = true;
}
else
dirty = false;
errlog->debug(NONE, "fbConfig.this");
}
fbConfig::fbConfig(fbErrorLogger* err, const char* filename): port(0), errlog(err), dirty(true)
{
loadDefaults(); /* assume defaults */
if (load(filename) < 0)
{
errlog->warn(CONFIGFAILEDTOLOAD, "using defaults");
}
dirty = true; /* save to default config file regardless */
errlog->debug(NONE, "fbConfig.this");
}
fbConfig::~fbConfig()
{
/* we can try to save here, but don't want to rely on it */
/* also, should we save to the default file or to the one that
initially provided the settings? */
if (dirty) save();
errlog->debug(NONE, "fbConfig.~this");
}
void fbConfig::loadDefaults()
{
addr = FBCONFIG_DEFAULT_ADDR;
port = FBCONFIG_DEFAULT_PORT;
webroot = FBCONFIG_DEFAULT_WEBROOT;
dbpath = FBCONFIG_DEFAULT_DBPATH;
}
int fbConfig::load()
{
return load(FBCONFIG_DEFAULT_CONFIGFILE);
}
int fbConfig::load(const char* filename)
{
string s;
ifstream myfile;
errlog->debug(NONE, "Loading config file: %s", filename);
myfile.open(filename, ios::in);
if (myfile.fail())
{
errlog->warn(NONE, "Cannot open config file: %s", filename);
return -1;
}
while (!myfile.eof() && !myfile.fail())
{
string::size_type i;
getline(myfile, s, '\n');
if (myfile.fail()) break;
if ((i = s.find("WebServerAddr=",0) != string::npos))
{
setWebServerAddr(s.substr((int)i+13));
errlog->debug(NONE, "Loaded WebServerAddr=%s", s.substr((int)i+13).c_str());
}
else if ((i = s.find("WebServerPort=",0) != string::npos))
{
setWebServerPort(s.substr((int)i+13));
errlog->debug(NONE, "Loaded WebServerPort=%s", s.substr((int)i+13).c_str());
}
else if ((i = s.find("WebServerRootPath=",0) != string::npos))
{
setWebServerRootPath(s.substr((int)i+17));
errlog->debug(NONE, "Loaded WebServerRootPath=%s", s.substr((int)i+17).c_str());
}
else if ((i = s.find("DBPath=",0) != string::npos))
{
setDBPath(s.substr((int)i+6));
errlog->debug(NONE, "Loaded DBPath=%s", s.substr((int)i+6).c_str());
}
else
{
errlog->warn(CONFIGUNKOWNVALUE, s);
}
}
if (myfile.fail() && !myfile.eof())
{
myfile.close();
errlog->warn(CONFIGFAILEDTOLOAD, "Cannot load from config file: %s", filename);
return -1;
}
myfile.close();
/* successfully loaded some settings. remaining settings from defaults */
errlog->msg(NONE, "Successfully loaded settings from config file: %s", filename);
return 0;
}
int fbConfig::save()
{
int i = 0;
if ((i = save(FBCONFIG_DEFAULT_CONFIGFILE)))
dirty = false;
return i;
}
int fbConfig::save(const char* filename)
{
ofstream myfile;
myfile.open(filename, ios::out);
errlog->debug(NONE, "Attempting to save config file: %s", filename);
if (myfile.fail())
{
errlog->warn(CONFIGFAILEDTOSAVE, "error opening config file to save");
return -1;
}
myfile << "WebServerAddr=" << addr << endl
<< "WebServerPort=" << port << endl
<< "WebServerRootPath=" << webroot << endl
<< "DBPath=" << dbpath << endl;
if (myfile.fail())
{
errlog->warn(CONFIGFAILEDTOSAVE, "problem writing to config file");
myfile.close();
return -1;
}
myfile.close();
errlog->debug(NONE, "Saved config file: %s", filename);
return 0;
}
void fbConfig::setWebServerAddr(const string& strAddr)
{
dirty = true;
addr = strAddr;
}
void fbConfig::setWebServerPort(const string& strPort)
{
/* istringstream s(strPort);
s >> port; */
port = atol(strPort.c_str());
dirty = true;
}
void fbConfig::setWebServerPort(int intPort)
{
dirty = true;
if (intPort>1024 && intPort<65536)
port = intPort;
else
errlog->warn(NONE, "Attempt to set port outside of valid range: %d", intPort);
}
void fbConfig::setWebServerRootPath(const string& strPath)
{
dirty = true;
webroot = strPath;
}
void fbConfig::setDBPath(const string& strPath)
{
dirty = true;
dbpath = strPath;
}
string& fbConfig::getWebServerAddr()
{
return addr;
}
int fbConfig::getWebServerPort()
{
return port;
}
string& fbConfig::getWebServerRootPath()
{
return webroot;
}
string& fbConfig::getDBPath()
{
return dbpath;
}