summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/config.cpp81
-rw-r--r--common/config.h48
2 files changed, 129 insertions, 0 deletions
diff --git a/common/config.cpp b/common/config.cpp
new file mode 100644
index 0000000..1649e23
--- /dev/null
+++ b/common/config.cpp
@@ -0,0 +1,81 @@
+#include "config.h"
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+
+Config::Config() {
+ path = "aotenjou.conf";
+ load_conf();
+}
+
+Config::Config(std::string path) {
+ path = path;
+ load_conf();
+}
+
+Config::~Config() {
+ save_conf();
+}
+
+/*
+ Adds a config-element to the list
+ */
+void Config::add_element(std::string keyword, std::string value) {
+ config[keyword] = value;
+}
+
+void Config::add_element(std::string keyword, int value) {
+ std::stringstream out;
+ out << value;
+ config[keyword] = out.str();
+}
+
+/*
+Load a value from the config.
+ */
+std::string Config::get_element(std::string keyword) {
+ return config[keyword];
+}
+
+/*
+ Initializes and loads the config from disk if it exists
+ */
+void Config::load_conf(){
+
+ std::ifstream config_file(path.c_str());
+
+ if(config_file.is_open()) {
+ while(config_file.good()) {
+ std::string config_line;
+ std::getline(config_file, config_line);
+ std::string id = config_line.substr(0, config_line.find("=") - 1);
+ std::string value = config_line.substr(config_line.find("=") + 2, config_line.length()-1);
+ config[id] = value;
+ }
+
+ config_file.close();
+
+ } else {
+ std::cerr << "Unable to open configuration file..." << std::endl;
+ }
+}
+
+/*
+ Saves the current config to disk
+ */
+void Config::save_conf(){
+
+ std::ofstream config_file(path.c_str(), std::ios::out | std::ios::trunc);
+
+ if(config_file.is_open()) {
+ for(std::map<std::string, std::string>::iterator it = config.begin(); it != config.end(); ++it) {
+ config_file << it->first << " = " << it->second << std::endl;
+ }
+
+ config_file.close();
+ } else {
+ std::cerr << "Unable to save to configuration file..." << std::endl;
+ }
+}
+
diff --git a/common/config.h b/common/config.h
new file mode 100644
index 0000000..87e9765
--- /dev/null
+++ b/common/config.h
@@ -0,0 +1,48 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <string>
+#include <map>
+
+//! Utility class for handling configuration file
+//! Supported format in config file is:
+//! keyword = value
+//! example: nick = xyz
+class Config{
+ private:
+ //! Map in the format <id, value>
+ std::map<std::string, std::string> config;
+
+ //! The path to the configuration file
+ std::string path;
+
+ //! Loads configuration from file
+ void load_conf();
+
+ //! Saves configuration to file
+ void save_conf();
+
+ public:
+ //! Constructs a object with the default configuration path
+ Config();
+ //! Constructs a object with a custom configuration path
+ Config(std::string path);
+ //! Deletes the object, saving the configuration to a file
+ ~Config();
+
+ //! Adds a std::string value to the configuration
+ //! \param keyword Keyword associated with the value inserted
+ //! \param value The value for the associated keyword
+ void add_element(std::string keyword, std::string value);
+
+ //! Adds a int value to the configuration
+ //! \param keyword Keyword associated with the value inserted
+ //! \param value The value for the associated keyword
+ void add_element(std::string keyword, const int value);
+
+ //! Gets a element from the configuration
+ //! \param keyword The keyword you want the value from
+ std::string get_element(std::string keyword);
+};
+
+#endif