Browse Source

begin hosts config data access

develop
Robin Thoni 9 years ago
parent
commit
18e028d91a
2 changed files with 69 additions and 0 deletions
  1. 46
    0
      src/DataAccess/HostsConfig.cpp
  2. 23
    0
      src/DataAccess/HostsConfig.h

+ 46
- 0
src/DataAccess/HostsConfig.cpp View File

@@ -0,0 +1,46 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#include <json/json.h>
6
+#include <fstream>
7
+#include "HostsConfig.h"
8
+
9
+HostsConfig::HostsConfig(const std::string &filePath)
10
+    : _filePath(filePath)
11
+{
12
+}
13
+
14
+bool HostsConfig::readConfig()
15
+{
16
+    std::ifstream file(_filePath);
17
+    if (!file)
18
+        return false;
19
+    Json::Value root;
20
+    try
21
+    {
22
+        file >> root;
23
+        file.close();
24
+    }
25
+    catch (...)
26
+    {
27
+        file.close();
28
+        return false;
29
+    }
30
+    auto defaults = root["defaults"];
31
+    auto actions = root["actions"];
32
+    for (Json::ArrayIndex i = 0; i < actions.size(); ++i)
33
+    {
34
+        auto action = actions[i];
35
+        auto action_type = readValue(action, defaults, "action");
36
+    }
37
+    return true;
38
+}
39
+
40
+Json::Value HostsConfig::readValue(const Json::Value &value, const Json::Value &defaults, const std::string &name)
41
+{
42
+    auto v = value[name];
43
+    if (!v)
44
+        return defaults[name];
45
+    return v;
46
+}

+ 23
- 0
src/DataAccess/HostsConfig.h View File

@@ -0,0 +1,23 @@
1
+//
2
+// Created by robin on 8/8/15.
3
+//
4
+
5
+#ifndef PDNS_SLAVE_HOSTSCONFIG_H
6
+#define PDNS_SLAVE_HOSTSCONFIG_H
7
+
8
+# include <string>
9
+
10
+class HostsConfig
11
+{
12
+public:
13
+    HostsConfig(const std::string& filePath);
14
+
15
+    bool readConfig();
16
+
17
+private:
18
+    Json::Value readValue(const Json::Value &value, const Json::Value &defaults, const std::string& name);
19
+    std::string _filePath;
20
+};
21
+
22
+
23
+#endif //PDNS_SLAVE_HOSTSCONFIG_H

Loading…
Cancel
Save