File: config.cpp

package info (click to toggle)
mpdas 0.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 188 kB
  • sloc: cpp: 1,455; makefile: 34
file content (70 lines) | stat: -rw-r--r-- 1,446 bytes parent folder | download
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);
}