forked from hrkfdn/mpdas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
79 lines (64 loc) · 1.38 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
#include "mpdas.h"
CConfig* Config = 0;
void
CConfig::ParseLine(std::string line)
{
std::vector<std::string> tokens;
char* pstr = 0;
char* szline = new char[line.size()+1];
strncpy(szline, line.c_str(), line.size()+1);
pstr = strtok(szline, " :=\t");
while(pstr) {
tokens.push_back(pstr);
pstr = strtok(NULL, " :=\t");
}
delete[] szline;
if(tokens.size() > 1) {
if(tokens[0] == "username")
_lusername = tokens[1];
else if(tokens[0] == "password")
_lpassword = tokens[1];
else if(tokens[0] == "host")
_mhost = tokens[1];
else if(tokens[0] == "mpdpassword")
_mpassword = tokens[1];
else if(tokens[0] == "port")
_mport = atoi(tokens[1].c_str());
else if(tokens[0] == "runas")
_runninguser = tokens[1];
else if(tokens[0] == "debug") {
if(tokens[1] == "1" || tokens[1] == "true")
_debug = true;
}
}
}
void
CConfig::LoadConfig(std::string path)
{
std::string line = "";
std::ifstream ifs(path.c_str(), std::ios::in);
if(!ifs.good()) {
iprintf("Config file (%s) does not exist or is not readable.", path.c_str());
return;
}
while(ifs.good()) {
getline(ifs, line);
ParseLine(line);
}
}
CConfig::CConfig(char* cfg)
{
/* Set optional settings to default */
_mhost = "localhost";
_mport = 6600;
_debug = false;
std::string path = "";
if(!cfg) {
path = CONFDIR;
path.append("/mpdasrc");
}
else {
path = cfg;
}
LoadConfig(path);
}