summaryrefslogtreecommitdiff
path: root/src/config/config.cpp
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2013-11-10 20:47:11 +0100
committerFlorent Le Coz <louiz@louiz.org>2013-11-10 20:47:11 +0100
commitf0d9273da61ce154dbe460cf58c98de851d30615 (patch)
treebbca8a7adca39ced153a7f4d04d3964692bd4f5d /src/config/config.cpp
parent10d528717723a72dd3240c634980a461cf9fa2df (diff)
downloadbiboumi-f0d9273da61ce154dbe460cf58c98de851d30615.tar.gz
biboumi-f0d9273da61ce154dbe460cf58c98de851d30615.tar.bz2
biboumi-f0d9273da61ce154dbe460cf58c98de851d30615.tar.xz
biboumi-f0d9273da61ce154dbe460cf58c98de851d30615.zip
Add a Config module, and use it to get the password from a file
Diffstat (limited to 'src/config/config.cpp')
-rw-r--r--src/config/config.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/config/config.cpp b/src/config/config.cpp
new file mode 100644
index 0000000..cad8216
--- /dev/null
+++ b/src/config/config.cpp
@@ -0,0 +1,129 @@
+#include <utils/make_unique.hpp>
+#include <config/config.hpp>
+
+#include <iostream>
+#include <sstream>
+
+std::string Config::filename = "./biboumi.cfg";
+bool Config::file_must_exist = false;
+
+std::string Config::get(const std::string& option, const std::string& def)
+{
+ Config* self = Config::instance().get();
+ auto it = self->values.find(option);
+
+ if (it == self->values.end())
+ return def;
+ return it->second;
+}
+
+int Config::get_int(const std::string& option, const int& def)
+{
+ Config* self = Config::instance().get();
+ std::string res = self->get(option, "");
+ if (!res.empty())
+ return atoi(res.c_str());
+ else
+ return def;
+}
+
+void Config::set_int(const std::string& option, const int& value, bool save)
+{
+ std::ostringstream os;
+ os << value;
+ Config::set(option, os.str(), save);
+}
+
+void Config::set(const std::string& option, const std::string& value, bool save)
+{
+ Config* self = Config::instance().get();
+ self->values[option] = value;
+ if (save)
+ {
+ self->save_to_file();
+ self->trigger_configuration_change();
+ }
+}
+
+void Config::connect(t_config_changed_callback callback)
+{
+ Config* self = Config::instance().get();
+ self->callbacks.push_back(callback);
+}
+
+void Config::close()
+{
+ Config* self = Config::instance().get();
+ self->save_to_file();
+ self->values.clear();
+ Config::instance().reset();
+}
+
+/**
+ * Private methods
+ */
+
+void Config::trigger_configuration_change()
+{
+ std::vector<t_config_changed_callback>::iterator it;
+ for (it = this->callbacks.begin(); it < this->callbacks.end(); ++it)
+ (*it)();
+}
+
+std::unique_ptr<Config>& Config::instance()
+{
+ static std::unique_ptr<Config> instance;
+
+ if (!instance)
+ {
+ instance = std::make_unique<Config>();
+ instance->read_conf();
+ }
+ return instance;
+}
+
+bool Config::read_conf()
+{
+ std::ifstream file;
+ file.open(filename.data());
+ if (!file.is_open())
+ {
+ if (Config::file_must_exist)
+ {
+ perror(("Error while opening file " + filename + " for reading.").c_str());
+ file.exceptions(std::ifstream::failbit);
+ }
+ return false;
+ }
+
+ 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);
+ this->values[option] = value;
+ }
+ return true;
+}
+
+void Config::save_to_file() const
+{
+ std::ofstream file(this->filename.data());
+ if (file.fail())
+ {
+ std::cerr << "Could not save config file." << std::endl;
+ return ;
+ }
+ for (auto& it: this->values)
+ file << it.first << "=" << it.second << std::endl;
+ file.close();
+}