~singpolyma/biboumi

ref: 46ff73662cc94220c5ee962b591c8ee327de6f85 biboumi/louloulibs/config/config.cpp -rw-r--r-- 2.1 KiB
46ff7366 — louiz’ Clean the Config module, use static things instead of a stupid singleton 6 years ago
                                                                                
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
e1a7114c Florent Le Coz
46ff7366 louiz’
cef238e7 louiz’
e1a7114c Florent Le Coz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <config/config.hpp>

#include <iostream>
#include <sstream>

#include <stdlib.h>

std::string Config::filename{};
std::map<std::string, std::string> Config::values{};
std::vector<t_config_changed_callback> Config::callbacks{};

std::string Config::get(const std::string& option, const std::string& def)
{
  auto it = Config::values.find(option);

  if (it == Config::values.end())
    return def;
  return it->second;
}

int Config::get_int(const std::string& option, const int& def)
{
  std::string res = Config::get(option, "");
  if (!res.empty())
    return atoi(res.c_str());
  else
    return def;
}

void Config::set(const std::string& option, const std::string& value, bool save)
{
  Config::values[option] = value;
  if (save)
    {
      Config::save_to_file();
      Config::trigger_configuration_change();
    }
}

void Config::connect(t_config_changed_callback callback)
{
    Config::callbacks.push_back(callback);
}

void Config::clear()
{
  Config::values.clear();
}

/**
 * Private methods
 */
void Config::trigger_configuration_change()
{
  std::vector<t_config_changed_callback>::iterator it;
  for (it = Config::callbacks.begin(); it < Config::callbacks.end(); ++it)
      (*it)();
}

bool Config::read_conf(const std::string& name)
{
  if (!name.empty())
    Config::filename = name;

  std::ifstream file(Config::filename.data());
  if (!file.is_open())
    {
      perror(("Error while opening file " + filename + " for reading.").c_str());
      return false;
    }

  Config::clear();

  std::string line;
  size_t pos;
  std::string option;
  std::string value;
  while (file.good())
    {
      std::getline(file, line);
      if (line == "" || line[0] == '#')
        continue ;
      pos = line.find('=');
      if (pos == std::string::npos)
        continue ;
      option = line.substr(0, pos);
      value = line.substr(pos+1);
      Config::values[option] = value;
    }
}

void Config::save_to_file()
{
  std::ofstream file(Config::filename.data());
  if (file.fail())
    {
      std::cerr << "Could not save config file." << std::endl;
      return ;
    }
  for (const auto& it: Config::values)
    file << it.first << "=" << it.second << '\n';
}