-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_parser.cpp
68 lines (54 loc) · 1.81 KB
/
config_parser.cpp
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
#include <glib.h>
#include "./config_parser.hpp"
std::vector<Keybind> parse_file(const std::string &path) {
std::vector<Keybind> keybinds;
std::ifstream file;
file.open(path);
if (file.is_open()) {
while (file) {
Keybind *parsed_keybind = parse_keybind(&file);
if (parsed_keybind) {
keybinds.push_back(*parsed_keybind);
delete parsed_keybind;
}
}
} else {
g_error("Could not open file at %s\n", path.c_str());
}
return keybinds;
}
void parse_line(const std::string &line, Keybind *keybind) {
if (line[0] == '#') { // Comment
// Only copy what's after the hashtags and spaces
for (size_t i = 0; i < line.size(); i++) {
if (line[i] != '#' && line[i] != ' ') {
keybind->comment = line.substr(i);
return;
}
}
} else if (!isspace(line[0])) { // True if the line is not a comment nor an action
keybind->shortcut = line;
return;
}
}
Keybind *parse_keybind(std::ifstream *file) {
Keybind *keybind = new Keybind(); // Create an empty keybind
while (true) {
std::string line;
std::getline(*file, line);
parse_line(line, keybind);
if (file->eof() || keybind->shortcut.size() > 0) // Allow shortcuts with no comments
break;
}
if (keybind->shortcut.size() == 0 && keybind->comment.size() == 0)
return nullptr; // Return nullptr if we have parsed nothing
if (!keybind->is_complete())
g_warning("Parsed an incomplete keybind:\n Description: %s\n Shortcut: %s\n", keybind->comment.c_str(),
keybind->shortcut.c_str());
return keybind;
}
Keybind::Keybind(std::string _comment, std::string _shortcut) : comment{_comment}, shortcut{_shortcut} {}
bool Keybind::is_complete() {
return this->comment.size() > 0 && this->shortcut.size() > 0;
}
Keybind::Keybind() : comment{}, shortcut{} {}