-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.hpp
45 lines (39 loc) · 1.53 KB
/
config.hpp
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
/*
* Copyright (c) 2012 Vasily i. Redkin <[email protected]>
* License: MIT (See LICENSE.txt or http://www.opensource.org/licenses/MIT)
*/
#ifndef CONFIG_HPP_INCLUDED
#define CONFIG_HPP_INCLUDED
#include <string>
#include <fstream>
#include <map>
#include <sstream> // for to_int
class ConfTarget
{
public:
virtual ~ConfTarget() { }
virtual bool configure(const std::string & var, const std::string & value)=0;
virtual ConfTarget * confcontext(const std::string & ctx, bool brackets);
};
class Config
{
private:
std::map<std::string, ConfTarget *> m_contexts;
std::string m_curctx;
ConfTarget * m_curtarget;
ConfTarget * m_deftarget;
public:
Config():m_curtarget(0), m_deftarget(0) { }
void register_context(const std::string & context, ConfTarget * target) { m_contexts[context] = target; }
void unregister_context(const std::string & context) { m_contexts.erase(context); }
void default_context(ConfTarget * target) { m_deftarget = target; if(!m_curtarget) m_curtarget = m_deftarget; }
bool load_confdir(const std::string & path);
bool read_file(const std::string & fname);
bool parse_line(std::string line);
bool change_context(const std::string & context, bool autocreatefamily = false);
void reset_context() { m_curtarget = m_deftarget; m_curctx.clear(); }
static bool to_bool(const std::string & s) { return s == "true" || s == "yes" || s == "on" || s == "1"; }
static int to_int(const std::string & s) { int r; std::stringstream ss(s); ss >> r; return r; }
};
extern Config config;
#endif /* CONFIG_HPP_INCLUDED */