aboutsummaryrefslogtreecommitdiff
path: root/include/configparser.h
blob: 485d01782c5f647b8d3f534e3eea26711658fe5a (plain) (blame)
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
#ifndef NEWSBOAT_CONFIGPARSER_H_
#define NEWSBOAT_CONFIGPARSER_H_

#include <functional>
#include <map>

#include "configactionhandler.h"

namespace newsboat {

enum class ActionHandlerStatus {
	INVALID_PARAMS,
	TOO_FEW_PARAMS,
	TOO_MANY_PARAMS,
	INVALID_COMMAND,
	FILENOTFOUND
};

class ConfigParser : public ConfigActionHandler {
public:
	ConfigParser();
	~ConfigParser() override;
	void register_handler(const std::string& cmd,
		ConfigActionHandler& handler);
	void handle_action(const std::string& action,
		const std::vector<std::string>& params) override;
	void dump_config(std::vector<std::string>&) const override
	{
		/* nothing because ConfigParser itself only handles include */
	}

	/// Processes configuration commands from a file at \a filename.
	///
	/// Returns:
	/// - `false` if the file couldn't be opened;
	/// - `true` if the whole file was processed completely.
	///
	/// If the file contains any errors, throws `ConfigException` with
	/// a message explaining the problem.
	bool parse_file(const std::string& filename);

	void parse_line(const std::string& line, const std::string& location);
	static std::string evaluate_backticks(std::string token);

private:
	static std::string evaluate_cmd(const std::string& cmd);
	std::vector<std::vector<std::string>> parsed_content;
	std::map<std::string, std::reference_wrapper<ConfigActionHandler>>
		action_handlers;
	std::vector<std::string> included_files;
};

} // namespace newsboat

#endif /* NEWSBOAT_CONFIGPARSER_H_ */