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);
55 bool Config::loadFile()
59 std::ifstream configFile("config.json");
60 if (configFile.is_open())
62 Json::CharReaderBuilder builder;
63 builder["collectComments"] = false;
64 builder["allowTrailingCommas"] = true; // FIXME
67 bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
70 std::cout << errs << std::endl;
79 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
81 Json::Value v = jconfigro[section][key];
82 if (!v.isString()) return false;
87 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
89 Json::Value v = jconfigro[section][key];
90 if (!v.isInt64()) return false;
95 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
97 Json::Value v = jconfigro[section][key];
98 if (!v.isBool()) return false;
103 bool Config::getBool(const char* section, const char* key, bool& out) const
105 Json::Value v = jconfigro[section][key];
106 if (!v.isBool()) return false;
111 void Config::dump() const
113 std::cout << jconfigro << std::endl;
116 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
118 const Json::Value& v = jconfigro[section][key];
119 if (!v.isArray()) return false;
121 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
123 const Json::Value& jv = *i;
125 callback(jv.asString());
131 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
133 const Json::Value& v = jconfigro[section][key];
134 if (!v.isObject()) return false;
136 for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
138 const Json::Value& jv = *i;
140 callback(i.key().asString(), jv.asString());
146 void Config::set(const std::string& section, const std::string& key, const std::string& value)
148 jconfig[section][key] = value;
151 void Config::set(const std::string& section, const std::string& key, bool value)
153 jconfig[section][key] = value;
156 void Config::set(const std::string& section, const std::string& key, const char* value)
158 jconfig[section][key] = value;
161 void Config::set(const std::string& section, const std::string& key, int value)
163 jconfig[section][key] = value;