Sfoglia il codice sorgente

added configuration class

marius.rescheleit 5 anni fa
parent
commit
faeafa8d93
2 ha cambiato i file con 50 aggiunte e 0 eliminazioni
  1. 19 0
      daemon/include/config.h
  2. 31 0
      daemon/src/config.cpp

+ 19 - 0
daemon/include/config.h

@@ -0,0 +1,19 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <map>
+#include <fstream>
+#include <sstream>
+#include <vector>
+
+// loads configurationdata from config.txt
+
+class Config {
+public:
+  bool init();
+  std::string getValue(std::string key);
+private:
+  std::map<std::string, std::string> storage;
+};
+
+#endif

+ 31 - 0
daemon/src/config.cpp

@@ -0,0 +1,31 @@
+#include "../include/config.h"
+
+bool Config::init() {
+  std::ifstream ifile("config.txt");
+  std::string line;
+  if(ifile) {
+    while(getline(ifile,line)) {
+      std::stringstream ss(line);
+      std::string segment;
+      std::vector<std::string> v;
+      while(getline(ss, segment, '=')) {
+        v.push_back(segment);
+      }
+      if(v.size()!=2) {
+        return false;
+      }
+      storage.insert(std::pair<std::string,std::string>(v.at(0),v.at(1)));
+    }
+    return true;
+  }
+  return false;
+}
+
+std::string Config::getValue(std::string key) {
+  auto it = storage.find(key);
+  if(it != storage.end()) {
+    return it->second;
+  }
+
+  return "";
+}