]> git.vomp.tv Git - vompclient.git/blob - src/config.cc
Type updates:
[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   // Not in config.json.sample - VDR login always sets this
59   insertInt("subtitles", "default", 0);
60 }
61
62 bool Config::loadFile()
63 {
64   jconfig.clear();
65
66   std::ifstream configFile("config.json");
67   if (configFile.is_open())
68   {
69     Json::CharReaderBuilder builder;
70     builder["collectComments"] = false;
71     builder["allowTrailingCommas"] = true; // FIXME
72     std::string errs;
73
74     bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
75     if (!ok)
76     {
77       std::cout << errs << std::endl;
78       return false;
79     }
80   }
81
82   applyDefaults();
83   return true;
84 }
85
86 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
87 {
88   Json::Value v = jconfigro[section][key];
89   if (!v.isString()) return false;
90   out = v.asString();
91   return true;
92 }
93
94 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
95 {
96   Json::Value v = jconfigro[section][key];
97   if (!v.isInt64()) return false;
98   out = toi4(v.asInt64());
99   return true;
100 }
101
102 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
103 {
104   Json::Value v = jconfigro[section][key];
105   if (!v.isBool()) return false;
106   out = v.asBool();
107   return true;
108 }
109 /*
110 bool Config::getBool(const char* section, const char* key, bool& out) const
111 {
112   Json::Value v = jconfigro[section][key];
113   if (!v.isBool()) return false;
114   out = v.asBool();
115   return true;
116 }*/
117
118 void Config::dump() const
119 {
120   std::cout << jconfigro << std::endl;
121 }
122
123 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
124 {
125   const Json::Value& v = jconfigro[section][key];
126   if (!v.isArray()) return false;
127
128   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
129   {
130     const Json::Value& jv = *i;
131     if (jv.isString())
132       callback(jv.asString());
133   }
134
135   return true;
136 }
137
138 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
139 {
140   const Json::Value& v = jconfigro[section][key];
141   if (!v.isObject()) return false;
142
143   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
144   {
145     const Json::Value& jv = *i;
146     if (jv.isString())
147       callback(i.key().asString(), jv.asString());
148   }
149
150   return true;
151 }
152
153 void Config::set(const std::string& section, const std::string& key, const std::string& value)
154 {
155   jconfig[section][key] = value;
156 }
157
158 void Config::set(const std::string& section, const std::string& key, bool value)
159 {
160   jconfig[section][key] = value;
161 }
162
163 void Config::set(const std::string& section, const std::string& key, const char* value)
164 {
165   jconfig[section][key] = value;
166 }
167
168 void Config::set(const std::string& section, const std::string& key, int value)
169 {
170   jconfig[section][key] = value;
171 }