summaryrefslogtreecommitdiff
path: root/louloulibs/config
diff options
context:
space:
mode:
authorVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
committerVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
commit4e4de7284e6e4d89d182ea459823bbec1e408842 (patch)
tree47e0ed5216b48649b138f168f61fddca2b0c076a /louloulibs/config
parentdfb3a6edfacf2f16a8a63690b3e8058b6295d1a3 (diff)
parenteda4b75b1cff83336e87da90efca9fd6b4ced2c7 (diff)
downloadbiboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.gz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.bz2
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.xz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.zip
Updated version 3.0 from 'upstream/3.0'
with Debian dir 0f18938e98f5a466f36719f60cef0490163ab845
Diffstat (limited to 'louloulibs/config')
-rw-r--r--louloulibs/config/config.cpp104
-rw-r--r--louloulibs/config/config.hpp94
2 files changed, 198 insertions, 0 deletions
diff --git a/louloulibs/config/config.cpp b/louloulibs/config/config.cpp
new file mode 100644
index 0000000..417981d
--- /dev/null
+++ b/louloulibs/config/config.cpp
@@ -0,0 +1,104 @@
+#include <config/config.hpp>
+#include <logger/logger.hpp>
+
+#include <cstring>
+#include <sstream>
+
+#include <cstdlib>
+
+std::string Config::filename{};
+std::map<std::string, std::string> Config::values{};
+std::vector<t_config_changed_callback> Config::callbacks{};
+
+std::string Config::get(const std::string& option, const std::string& def)
+{
+ auto it = Config::values.find(option);
+
+ if (it == Config::values.end())
+ return def;
+ return it->second;
+}
+
+int Config::get_int(const std::string& option, const int& def)
+{
+ std::string res = Config::get(option, "");
+ if (!res.empty())
+ return std::atoi(res.c_str());
+ else
+ return def;
+}
+
+void Config::set(const std::string& option, const std::string& value, bool save)
+{
+ Config::values[option] = value;
+ if (save)
+ {
+ Config::save_to_file();
+ Config::trigger_configuration_change();
+ }
+}
+
+void Config::connect(t_config_changed_callback callback)
+{
+ Config::callbacks.push_back(callback);
+}
+
+void Config::clear()
+{
+ Config::values.clear();
+}
+
+/**
+ * Private methods
+ */
+void Config::trigger_configuration_change()
+{
+ std::vector<t_config_changed_callback>::iterator it;
+ for (it = Config::callbacks.begin(); it < Config::callbacks.end(); ++it)
+ (*it)();
+}
+
+bool Config::read_conf(const std::string& name)
+{
+ if (!name.empty())
+ Config::filename = name;
+
+ std::ifstream file(Config::filename.data());
+ if (!file.is_open())
+ {
+ log_error("Error while opening file ", filename, " for reading: ", strerror(errno));
+ return false;
+ }
+
+ Config::clear();
+
+ std::string line;
+ size_t pos;
+ std::string option;
+ std::string value;
+ while (file.good())
+ {
+ std::getline(file, line);
+ if (line == "" || line[0] == '#')
+ continue ;
+ pos = line.find('=');
+ if (pos == std::string::npos)
+ continue ;
+ option = line.substr(0, pos);
+ value = line.substr(pos+1);
+ Config::values[option] = value;
+ }
+ return true;
+}
+
+void Config::save_to_file()
+{
+ std::ofstream file(Config::filename.data());
+ if (file.fail())
+ {
+ log_error("Could not save config file.");
+ return ;
+ }
+ for (const auto& it: Config::values)
+ file << it.first << "=" << it.second << '\n';
+}
diff --git a/louloulibs/config/config.hpp b/louloulibs/config/config.hpp
new file mode 100644
index 0000000..6728df8
--- /dev/null
+++ b/louloulibs/config/config.hpp
@@ -0,0 +1,94 @@
+/**
+ * Read the config file and save all the values in a map.
+ * Also, a singleton.
+ *
+ * Use Config::filename = "bla" to set the filename you want to use.
+ *
+ * If you want to exit if the file does not exist when it is open for
+ * reading, set Config::file_must_exist = true.
+ *
+ * Config::get() can then be used to access the values in the conf.
+ *
+ * Use Config::close() when you're done getting/setting value. This will
+ * save the config into the file.
+ */
+
+#pragma once
+
+
+#include <functional>
+#include <fstream>
+#include <memory>
+#include <vector>
+#include <string>
+#include <map>
+
+typedef std::function<void()> t_config_changed_callback;
+
+class Config
+{
+public:
+ Config() = default;
+ ~Config() = default;
+ Config(const Config&) = delete;
+ Config& operator=(const Config&) = delete;
+ Config(Config&&) = delete;
+ Config& operator=(Config&&) = delete;
+
+ /**
+ * returns a value from the config. If it doesn’t exist, use
+ * the second argument as the default.
+ */
+ static std::string get(const std::string&, const std::string&);
+ /**
+ * returns a value from the config. If it doesn’t exist, use
+ * the second argument as the default.
+ */
+ static int get_int(const std::string&, const int&);
+ /**
+ * Set a value for the given option. And write all the config
+ * in the file from which it was read if save is true.
+ */
+ static void set(const std::string&, const std::string&, bool save = false);
+ /**
+ * Adds a function to a list. This function will be called whenever a
+ * configuration change occurs (when set() is called, or when the initial
+ * conf is read)
+ */
+ static void connect(t_config_changed_callback);
+ /**
+ * Destroy the instance, forcing it to be recreated (with potentially
+ * different parameters) the next time it’s needed.
+ */
+ static void clear();
+ /**
+ * Read the configuration file at the given path.
+ */
+ static bool read_conf(const std::string& name="");
+ /**
+ * Get the filename
+ */
+ static const std::string& get_filename()
+ { return Config::filename; }
+
+private:
+ /**
+ * Set the value of the filename to use, before calling any method.
+ */
+ static std::string filename;
+ /**
+ * Write all the config values into the configuration file
+ */
+ static void save_to_file();
+ /**
+ * Call all the callbacks previously registered using connect().
+ * This is used to notify any class that a configuration change occured.
+ */
+ static void trigger_configuration_change();
+
+ static std::map<std::string, std::string> values;
+ static std::vector<t_config_changed_callback> callbacks;
+
+};
+
+