forked from Pizzabelly/EasyRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
86 lines (79 loc) · 3.68 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
#include "config.hpp"
#include "discord.hpp"
#include <algorithm>
#include <fstream>
#include <limits.h>
#include <sstream>
#define CONFIG_PATH "config.ini"
template <class T> T setVar(T val, config_t *c) {
c->changed = true;
return val;
}
// check and set the global presence config
void config_t::update() {
// open config file
std::ifstream config_file(CONFIG_PATH);
if (config_file.fail()) {
printf("config file not found\n");
Shutdown(1);
}
// "parse" config file
// this is super specific and is NOT proper ini parsing
// but it works, saves memory and avoids massive dependencies
for (std::string line; std::getline(config_file, line);) {
// if line is ini comment (;), whitespace, or '[', skip it
char first = line.front();
if (first == ';' || first == ' ' || first == '[')
continue;
std::istringstream line_stream;
line_stream.str(line);
std::string key;
if (std::getline(line_stream, key, '=')) {
key.erase(std::remove_if(key.begin(), key.end(), ::isspace), key.end());
std::string value;
if (!std::getline(line_stream, value))
value = "";
if (isspace(value.front()) && isspace(value.back()))
value.erase(0, 1);
if (key == "ClientID" && value.compare(this->client_id) != 0) {
this->client_id = setVar<std::string>(value, this);
} else if (key == "State" && value.compare(this->state) != 0) {
this->state = setVar<std::string>(value, this);
} else if (key == "Details" && value.compare(this->details) != 0) {
this->details = setVar<std::string>(value, this);
} else if (key == "LargeImage" && value.compare(this->large_img.key) != 0) {
this->large_img.key = setVar<std::string>(value, this);
} else if (key == "SmallImage" && value.compare(this->small_img.key) != 0) {
this->small_img.key = setVar<std::string>(value, this);
} else if (key == "LargeImageTooltip" &&
value.compare(this->large_img.text) != 0) {
this->large_img.text = setVar<std::string>(value, this);
} else if (key == "SmallImageTooltip" &&
value.compare(this->small_img.text) != 0) {
this->small_img.text = setVar<std::string>(value, this);
}
// special conditions for timestamps to avoid bad values
else if (key == "StartTimestamp") {
long long num_value = std::strtoll(value.c_str(), NULL, 10);
if (num_value != this->start_time)
this->start_time = setVar<long long>(num_value, this);
} else if (key == "EndTimestamp") {
long long num_value = std::strtoll(value.c_str(), NULL, 10);
if (num_value != this->end_time)
this->end_time = setVar<long long>(num_value, this);
}
}
}
}
// print values for the current settings from the config file
void config_t::print() {
printf("\nCurrent Presence (%s) :", this->client_id.c_str());
printf("\nState: %s", this->state.c_str());
printf("\nDetails: %s", this->details.c_str());
printf("\nLarge Image: '%s' with toolip, '%s'", this->large_img.key.c_str(),
this->large_img.text.c_str());
printf("\nSmall Image: '%s' with toolip, '%s'", this->small_img.key.c_str(),
this->small_img.text.c_str());
printf("\nStart Time: %lld", this->start_time);
printf("\nEnd Time: %lld\n", this->end_time);
}