runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
ConfigManager.cpp
Go to the documentation of this file.
1 #include <ConfigManager.h>
2 
3 #include <iostream>
4 #include <fstream>
5 #include <sstream>
6 
7 #include <utils.h>
8 
10  static ConfigManager instance;
11  return instance;
12 }
13 
14 const Json::Value & ConfigManager::operator[](const std::string & name) const {
15  for(auto & root: roots) {
16  const Json::Value & ret = getValue(root, name);
17  std::cout << "searched for " << name << std::endl;
18  std::cout << "got: " << ret << std::endl;
19  if(ret == Json::Value::null)
20  continue;
21  return ret;
22  }
23  return Json::Value::null;
24 }
25 
26 const Json::Value & ConfigManager::getValue(const Json::Value & root, const std::string & name) const {
27  auto dot = name.find_first_of(".");
28  if(dot == std::string::npos)
29  return root[name];
30  return getValue(root[name.substr(0, dot)], name.substr(dot+1));
31 }
32 
33 void ConfigManager::addConfigDir(const std::string & dirname) {
34  std::stringstream ss;
35  ss << dirname << "/capture.json";
36  addConfigFile(ss.str());
37 }
38 
39 void ConfigManager::addConfigFile(const std::string & filename) {
40  Json::Value root;
41  Json::Reader reader;
42  std::ifstream ifs(filename);
43  if(!reader.parse(ifs, root)) {
44  std::cout << "Error parsing " << filename << "!" << std::endl;
45  std::cout << reader.getFormattedErrorMessages() << std::endl;
46  ifs.close();
47  return;
48  }
49  ifs.close();
50  roots.push_front(root);
51 }
52 
53 // PRIVATE
54 
55 ConfigManager::ConfigManager() {
56  std::stringstream captureConfig;
57  captureConfig << getHome() << "/.macs/capture.json";
58  init({captureConfig.str()});
59 }
60 
61 void ConfigManager::init(const std::list<std::string> & config_files) {
62  for(auto filename: config_files) {
63  addConfigFile(filename);
64  }
65 }
66 
67 ConfigManager::~ConfigManager() {
68 }
std::string getHome()
Definition: utils.cpp:227
void addConfigDir(const std::string &dirname)
void addConfigFile(const std::string &filename)
const Json::Value & operator[](const std::string &value_name) const
static ConfigManager & getInstance()