6 // http://open-source-parsers.github.io/jsoncpp-docs/doxygen/namespacemembers.html
11 Config* Config::instance = NULL;
25 Config* Config::getInstance()
30 void Config::applyDefaults()
32 // Insert the value only if it doesn't already exist
33 auto insertBool = [&] (const char* s, const char* k, bool v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
34 auto insertString = [&] (const char* s, const char* k, const char* v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
35 auto insertInt = [&] (const char* s, const char* k, int v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
37 insertBool("main", "daemonize", true);
39 insertBool("log", "enabled", false);
40 insertString("log", "filename", "stdout");
41 insertString("log", "level", "debug");
43 #ifdef VOMP_PLATFORM_RASPBERRY
44 insertBool("input", "mod_cec_enabled", true);
46 insertBool("input", "mod_cec_enabled", false);
49 insertBool("input", "mod_udp_enabled", true);
50 insertBool("input", "mod_lirc_enabled", false);
52 insertInt("server-discovery", "prefer-ipv", 6);
54 // Not in config.json.sample - VDR login always sets this
55 insertInt("subtitles", "default", 0);
58 bool Config::loadFile()
62 std::ifstream configFile("config.json");
63 if (configFile.is_open())
65 Json::CharReaderBuilder builder;
66 builder["collectComments"] = false;
67 builder["allowTrailingCommas"] = true; // FIXME
70 bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
73 std::cout << errs << std::endl;
82 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
84 Json::Value v = jconfigro[section][key];
85 if (!v.isString()) return false;
90 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
92 Json::Value v = jconfigro[section][key];
93 if (!v.isInt64()) return false;
98 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
100 Json::Value v = jconfigro[section][key];
101 if (!v.isBool()) return false;
106 bool Config::getBool(const char* section, const char* key, bool& out) const
108 Json::Value v = jconfigro[section][key];
109 if (!v.isBool()) return false;
114 void Config::dump() const
116 std::cout << jconfigro << std::endl;
119 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
121 const Json::Value& v = jconfigro[section][key];
122 if (!v.isArray()) return false;
124 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
126 const Json::Value& jv = *i;
128 callback(jv.asString());
134 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
136 const Json::Value& v = jconfigro[section][key];
137 if (!v.isObject()) return false;
139 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
141 const Json::Value& jv = *i;
143 callback(i.key().asString(), jv.asString());
149 void Config::set(const std::string& section, const std::string& key, const std::string& value)
151 jconfig[section][key] = value;
154 void Config::set(const std::string& section, const std::string& key, bool value)
156 jconfig[section][key] = value;
159 void Config::set(const std::string& section, const std::string& key, const char* value)
161 jconfig[section][key] = value;
164 void Config::set(const std::string& section, const std::string& key, int value)
166 jconfig[section][key] = value;