]> git.vomp.tv Git - vompclient.git/blob - config.cc
Start work on local JSON config system. Start using it with Input classes
[vompclient.git] / 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
9 #include "config.h"
10
11 Config* Config::instance = NULL;
12
13 Config::Config()
14 : jconfigro(jconfig)
15 {
16   instance = this;
17   applyDefaults();
18 }
19
20 Config::~Config()
21 {
22   instance = NULL;
23 }
24
25 Config* Config::getInstance()
26 {
27   return instance;
28 }
29
30 void Config::applyDefaults()
31 {
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
36   insertBool("main", "debug", false);
37   insertBool("main", "daemonize", true);
38
39   insertBool("log", "enabled", true);
40   insertString("log", "filename", "stdout");
41   insertString("log", "level", "debug");
42
43   #ifdef VOMP_PLATFORM_RASPBERRY
44   insertBool("input", "mod_cec_enabled", true);
45   #else
46   insertBool("input", "mod_cec_enabled", false);
47   #endif
48
49   insertBool("input", "mod_udp_enabled", true);
50   insertBool("input", "mod_lirc_enabled", false);
51 }
52
53 bool Config::loadFile()
54 {
55   jconfig.clear();
56
57   std::ifstream configFile("config.json");
58
59   Json::CharReaderBuilder builder;
60   builder["collectComments"] = false;
61   builder["allowTrailingCommas"] = true;
62   std::string errs;
63
64   bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
65
66   std::cout << errs << std::endl;
67
68   if (!ok) return false;
69 //  std::cout << jconfig << std::endl;
70 //  std::cout << errs << std::endl;
71
72
73   applyDefaults();
74
75   dump();
76
77   return true;
78 }
79
80 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
81 {
82   Json::Value v = jconfigro[section][key];
83   if (!v.isString()) return false;
84   out = v.asString();
85   return true;
86 }
87
88 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
89 {
90   Json::Value v = jconfigro[section][key];
91   if (!v.isInt64()) return false;
92   out = v.asInt64();
93   return true;
94 }
95
96 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
97 {
98   Json::Value v = jconfigro[section][key];
99   if (!v.isBool()) return false;
100   out = v.asBool();
101   return true;
102 }
103 /*
104 bool Config::getBool(const char* section, const char* 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 void Config::dump() const
113 {
114   std::cout << jconfigro << std::endl;
115 }
116
117 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
118 {
119   const Json::Value& v = jconfigro[section][key];
120   if (!v.isArray()) return false;
121
122   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
123   {
124     const Json::Value& jv = *i;
125     if (jv.isString())
126       callback(jv.asString());
127   }
128
129   return true;
130 }
131
132 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
133 {
134   const Json::Value& v = jconfigro[section][key];
135   if (!v.isObject()) return false;
136
137   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
138   {
139     const Json::Value& jv = *i;
140     if (jv.isString())
141       callback(i.key().asString(), jv.asString());
142   }
143
144   return true;
145 }
146