This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
/
configparser.h
48 lines (40 loc) · 1.75 KB
/
configparser.h
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
#ifndef NEWSBEUTER_CONFIGPARSER__H
#define NEWSBEUTER_CONFIGPARSER__H
#include <vector>
#include <string>
#include <map>
#include <set>
namespace newsbeuter {
enum action_handler_status { AHS_OK = 0, AHS_INVALID_PARAMS, AHS_TOO_FEW_PARAMS, AHS_INVALID_COMMAND, AHS_FILENOTFOUND };
struct config_action_handler {
virtual void handle_action(const std::string& action, const std::vector<std::string>& params) = 0;
virtual void dump_config(std::vector<std::string>& config_output) = 0;
config_action_handler() { }
virtual ~config_action_handler() { }
};
class configparser : public config_action_handler {
public:
configparser();
virtual ~configparser();
void register_handler(const std::string& cmd, config_action_handler * handler);
void unregister_handler(const std::string& cmd);
virtual void handle_action(const std::string& action, const std::vector<std::string>& params);
virtual void dump_config(std::vector<std::string>& ) { /* nothing because configparser itself only handles include */ }
bool parse(const std::string& filename, bool double_include = true);
static std::string evaluate_backticks(std::string token);
private:
void evaluate_backticks(std::vector<std::string>& tokens);
static std::string evaluate_cmd(const std::string& cmd);
std::vector<std::vector<std::string>> parsed_content;
std::map<std::string,config_action_handler *> action_handlers;
std::set<std::string> included_files;
};
class null_config_action_handler : public config_action_handler {
public:
null_config_action_handler() { }
virtual ~null_config_action_handler() { }
virtual void handle_action(const std::string& , const std::vector<std::string>& ) { }
virtual void dump_config(std::vector<std::string>& ) { }
};
}
#endif