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; };
36 insertBool("main", "debug", false);
37 insertBool("main", "daemonize", true);
39 insertBool("log", "enabled", true);
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);
53 bool Config::loadFile()
57 std::ifstream configFile("config.json");
59 Json::CharReaderBuilder builder;
60 builder["collectComments"] = false;
61 builder["allowTrailingCommas"] = true;
64 bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
66 std::cout << errs << std::endl;
68 if (!ok) return false;
69 // std::cout << jconfig << std::endl;
70 // std::cout << errs << std::endl;
80 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
82 Json::Value v = jconfigro[section][key];
83 if (!v.isString()) return false;
88 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
90 Json::Value v = jconfigro[section][key];
91 if (!v.isInt64()) return false;
96 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
98 Json::Value v = jconfigro[section][key];
99 if (!v.isBool()) return false;
104 bool Config::getBool(const char* section, const char* key, bool& out) const
106 Json::Value v = jconfigro[section][key];
107 if (!v.isBool()) return false;
112 void Config::dump() const
114 std::cout << jconfigro << std::endl;
117 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
119 const Json::Value& v = jconfigro[section][key];
120 if (!v.isArray()) return false;
122 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
124 const Json::Value& jv = *i;
126 callback(jv.asString());
132 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
134 const Json::Value& v = jconfigro[section][key];
135 if (!v.isObject()) return false;
137 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
139 const Json::Value& jv = *i;
141 callback(i.key().asString(), jv.asString());