]> git.vomp.tv Git - vompclient.git/blob - src/config.cc
Have build.sh deal with cmake
[vompclient.git] / src / config.cc
1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <streambuf>
5
6 // http://open-source-parsers.github.io/jsoncpp-docs/doxygen/namespacemembers.html
7
8 #include "defines.h"
9
10 #include "config.h"
11
12 Config* Config::instance = NULL;
13
14 Config::Config()
15 : jconfigro(jconfig)
16 {
17   instance = this;
18   applyDefaults();
19 }
20
21 Config::~Config()
22 {
23   instance = NULL;
24 }
25
26 Config* Config::getInstance()
27 {
28   return instance;
29 }
30
31 void Config::applyDefaults()
32 {
33   // Insert the value only if it doesn't already exist
34   auto insertBool   = [&] (const char* s, const char* k, bool v)        { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
35   auto insertString = [&] (const char* s, const char* k, const char* v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
36   auto insertInt    = [&] (const char* s, const char* k, int v)         { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
37
38   insertBool("main", "daemonize", true);
39   insertInt("main", "start_to_live_tv", 0);
40
41   insertBool("log", "enabled", false);
42   insertString("log", "filename", "stdout");
43   insertString("log", "level", "debug");
44
45   #ifdef VOMP_PLATFORM_RASPBERRY
46   insertBool("input", "mod_cec_enabled", true);
47   #else
48   insertBool("input", "mod_cec_enabled", false);
49   #endif
50
51   insertBool("input", "mod_udp_enabled", true);
52   insertBool("input", "mod_lirc_enabled", false);
53
54   insertInt("input_udp", "port", 2000);
55
56   insertInt("server-discovery", "prefer-ipv", 6);
57
58   insertBool("videoomx", "disable-hdmi-modechange", false);
59
60   // Not in config.json.sample - VDR login always sets this
61   insertInt("subtitles", "default", 0);
62 }
63
64 bool Config::loadFile()
65 {
66   jconfig.clear();
67
68   std::ifstream configFile("config.json");
69   if (configFile.is_open())
70   {
71     Json::CharReaderBuilder builder;
72     builder["collectComments"] = false;
73     builder["allowTrailingCommas"] = true; // FIXME
74     std::string errs;
75
76     bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
77     if (!ok)
78     {
79       std::cout << errs << std::endl;
80       return false;
81     }
82   }
83
84   applyDefaults();
85   return true;
86 }
87
88 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
89 {
90   Json::Value v = jconfigro[section][key];
91   if (!v.isString()) return false;
92   out = v.asString();
93   return true;
94 }
95
96 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
97 {
98   Json::Value v = jconfigro[section][key];
99   if (!v.isInt64()) return false;
100   out = toi4(v.asInt64());
101   return true;
102 }
103
104 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
105 {
106   Json::Value v = jconfigro[section][key];
107   if (!v.isBool()) return false;
108   out = v.asBool();
109   return true;
110 }
111 /*
112 bool Config::getBool(const char* section, const char* key, bool& out) const
113 {
114   Json::Value v = jconfigro[section][key];
115   if (!v.isBool()) return false;
116   out = v.asBool();
117   return true;
118 }*/
119
120 void Config::dump() const
121 {
122   std::cout << jconfigro << std::endl;
123 }
124
125 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
126 {
127   const Json::Value& v = jconfigro[section][key];
128   if (!v.isArray()) return false;
129
130   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
131   {
132     const Json::Value& jv = *i;
133     if (jv.isString())
134       callback(jv.asString());
135   }
136
137   return true;
138 }
139
140 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
141 {
142   const Json::Value& v = jconfigro[section][key];
143   if (!v.isObject()) return false;
144
145   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
146   {
147     const Json::Value& jv = *i;
148     if (jv.isString())
149       callback(i.key().asString(), jv.asString());
150   }
151
152   return true;
153 }
154
155 void Config::set(const std::string& section, const std::string& key, const std::string& value)
156 {
157   jconfig[section][key] = value;
158 }
159
160 void Config::set(const std::string& section, const std::string& key, bool value)
161 {
162   jconfig[section][key] = value;
163 }
164
165 void Config::set(const std::string& section, const std::string& key, const char* value)
166 {
167   jconfig[section][key] = value;
168 }
169
170 void Config::set(const std::string& section, const std::string& key, int value)
171 {
172   jconfig[section][key] = value;
173 }