-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
98 lines (93 loc) · 3.14 KB
/
config.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
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
#include "config.hpp"
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
#ifdef DEBUG_BUILD
const bool debug = true;
#else
const bool debug = false;
#endif
int testFunc() {
return 69;
}
bool trim(std::string &str) {
int first = str.find_first_not_of(' ');
int last = str.find_last_not_of(' ');
try {
str = str.substr(first, last - first + 1);
return true;
} catch (std::exception &err ) {
std::cout << "Failed to parse string: " << str << std::endl;
std::cout << err.what() << std::endl;
return false;
}
}
bool truthy(const std::string &str) {
return (str == "1" || str == "true");
}
bool falsey(const std::string &str) {
return (str == "0" || str == "false");
}
Config::Config() {
// use default values;
}
Config::Config(std::string filename) {
std::ifstream file;
file.open(filename);
if (file.fail()) {
std::cout << "Failed to open file" << std::endl;
failed = true;
} else {
std::string line;
int line_num = 1;
while (std::getline(file, line)) {
if (line.size() > 0 && line[0] != '#') { // ignore comments
std::string option;
std::string val;
try {
int x = line.find(delim);
option = line.substr(0, x);
option.erase(std::remove_if(option.begin(), option.end(), isspace), option.end()); // removes whitespace probably
val = line.substr(x + 1);
++line_num;
} catch(std::exception &err) {
std::cout << "Failed to parse line " << line_num << ": " << err.what() << std::endl;
failed = true;
};
if (option == "fps") {
try {
int framerate = std::stoi(val);
this->fps = framerate;
} catch (std::exception &err) {
std::cout << "Failed to parse value on line " << line_num << ": " << err.what() << std::endl;
std::cout << "Option: " << option << ", Value: " << val << std::endl;
failed = true;
}
} else if (option == "logo") { // other values
if (trim(val)) {
this->logo_path = val;
} else {
failed = true;
}
} else if (option == "saver_mode") {
if (!trim(val)) {
failed = true;
} else {
if (truthy(val)) {
this->saver_mode = true;
} else if (falsey(val)) {
this->saver_mode = false;
} else {
std::cout << "Failed to understand value: " << val << " for saver_mode" << std::endl;
failed = true;
}
}
}
}
}
}
}
bool Config::hasLoaded() {
return !failed;
}