]> git.vomp.tv Git - vompclient.git/blob - config.cc
Implement unix sockets in 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", "daemonize", true);
37
38   insertBool("log", "enabled", false);
39   insertString("log", "filename", "stdout");
40   insertString("log", "level", "debug");
41
42   #ifdef VOMP_PLATFORM_RASPBERRY
43   insertBool("input", "mod_cec_enabled", true);
44   #else
45   insertBool("input", "mod_cec_enabled", false);
46   #endif
47
48   insertBool("input", "mod_udp_enabled", true);
49   insertBool("input", "mod_lirc_enabled", false);
50 }
51
52 bool Config::loadFile()
53 {
54   jconfig.clear();
55
56   std::ifstream configFile("config.json");
57   if (configFile.is_open())
58   {
59     Json::CharReaderBuilder builder;
60     builder["collectComments"] = false;
61     builder["allowTrailingCommas"] = true; // FIXME
62     std::string errs;
63
64     bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
65     if (!ok)
66     {
67       std::cout << errs << std::endl;
68       return false;
69     }
70   }
71
72   applyDefaults();
73   return true;
74 }
75
76 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
77 {
78   Json::Value v = jconfigro[section][key];
79   if (!v.isString()) return false;
80   out = v.asString();
81   return true;
82 }
83
84 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
85 {
86   Json::Value v = jconfigro[section][key];
87   if (!v.isInt64()) return false;
88   out = v.asInt64();
89   return true;
90 }
91
92 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
93 {
94   Json::Value v = jconfigro[section][key];
95   if (!v.isBool()) return false;
96   out = v.asBool();
97   return true;
98 }
99 /*
100 bool Config::getBool(const char* section, const char* key, bool& out) const
101 {
102   Json::Value v = jconfigro[section][key];
103   if (!v.isBool()) return false;
104   out = v.asBool();
105   return true;
106 }*/
107
108 void Config::dump() const
109 {
110   std::cout << jconfigro << std::endl;
111 }
112
113 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
114 {
115   const Json::Value& v = jconfigro[section][key];
116   if (!v.isArray()) return false;
117
118   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
119   {
120     const Json::Value& jv = *i;
121     if (jv.isString())
122       callback(jv.asString());
123   }
124
125   return true;
126 }
127
128 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
129 {
130   const Json::Value& v = jconfigro[section][key];
131   if (!v.isObject()) return false;
132
133   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
134   {
135     const Json::Value& jv = *i;
136     if (jv.isString())
137       callback(i.key().asString(), jv.asString());
138   }
139
140   return true;
141 }
142
143 void Config::set(const std::string& section, const std::string& key, const std::string& value)
144 {
145   jconfig[section][key] = value;
146 }
147
148 void Config::set(const std::string& section, const std::string& key, bool value)
149 {
150   jconfig[section][key] = value;
151 }
152
153 void Config::set(const std::string& section, const std::string& key, const char* value)
154 {
155   jconfig[section][key] = value;
156 }