Parent: [327004] (diff)

Download this file

config.cpp    71 lines (57 with data), 1.4 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
#include "mpdas.h"
int IniHandler(void* param, const char* section, const char* name, const char* value)
{
CConfig* config = (CConfig*)param;
std::string val = std::string(value);
// strip quotes if they exist to allow passwords to begin with a whitespace
if(val.length() >= 2 && val[0] == '\"' && val[val.length()-1] == '\"') {
val.erase(0, 1);
val.erase(val.length() - 1);
}
config->Set(name, val);
return 1;
}
void CConfig::LoadConfig(std::string path)
{
if(ini_parse(path.c_str(), &IniHandler, this) < 0) {
iprintf("Cannot parse config file (%s).", path.c_str());
return;
}
}
std::string CConfig::Get(std::string name)
{
if(_configuration.find(name) == _configuration.end()) {
return "";
}
return _configuration.find(name)->second;
}
bool CConfig::GetBool(std::string name)
{
std::string value = Get(name);
return value == "1" || value == "true";
}
int CConfig::GetInt(std::string name)
{
return atoi(Get(name).c_str());
}
ScrobblingService CConfig::getService()
{
return Get("service") == "librefm" ? LibreFm : LastFm;
}
CConfig::CConfig(char* cfg)
{
/* Set optional settings to default */
Set("host", "localhost");
Set("port", "6600");
Set("debug", "false");
Set("service", "lastfm");
std::string path = "";
if(!cfg) {
path = CONFDIR;
path.append("/mpdasrc");
}
else {
path = cfg;
}
LoadConfig(path);
}