Explorar el Código

Using a namespace for the config

Jonas Pflanzer hace 5 años
padre
commit
dac6177da4
Se han modificado 2 ficheros con 20 adiciones y 17 borrados
  1. 7 8
      daemon/include/config.h
  2. 13 9
      daemon/src/config.cpp

+ 7 - 8
daemon/include/config.h

@@ -1,19 +1,18 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 
-#include <map>
 #include <fstream>
+#include <map>
 #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;
-};
+namespace Config {
+bool init(const std::string &filename);
+std::string getValue(const std::string &key);
+
+extern std::map<std::string, std::string> storage;
+}; // namespace Config
 
 #endif

+ 13 - 9
daemon/src/config.cpp

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