10 #include <spdlog/spdlog.h>
19 #define GEN_DEFAULT_FONTS_PATH "fonts/"
20 #define GEN_DEFAULT_FONT "09809_COURIER.ttf"
21 #define GEN_DEFAULT_LANG_PATH "languages/"
22 #define GEN_DEFAULT_LANG "en"
24 #define KB_DEFAULT_DEV "/dev/spidev0.1"
25 #define KB_DEFAULT_MODE 0
26 #define KB_DEFAULT_BITS 8
27 #define KB_DEFAULT_SPEED 500000
28 #define KB_DEFAULT_DELAY 0
30 #define RFID_DEFAULT_DEV "/dev/spidev0.0"
31 #define RFID_DEFAULT_MODE 0
32 #define RFID_DEFAULT_BITS 8
33 #define RFID_DEFAULT_SPEED 1000000
34 #define RFID_DEFAULT_DELAY 0
36 #define DB_DEFAULT_HOST "localhost"
37 #define DB_DEFAULT_USER "pi"
38 #define DB_DEFAULT_PASSWORD "raspberry"
39 #define DB_DEFAULT_DATABASE "control_system"
40 #define DB_DEFAULT_PORT 3306
42 #define HX_DEFAULT_DATA_PIN 25
43 #define HX_DEFAULT_CLOCK_PIN 18
44 #define HX_DEFAULT_RATE 80
45 #define HX_DEFAULT_REF_UNIT -45
46 #define HX_DEFAULT_OFFSET -254817
56 val = strtol(str, &tmpend, 10);
59 spdlog::error(
"config_loader.cpp - Int parsing error. Over/Under-flow?");
63 spdlog::error(
"config_loader.cpp - Int parsing error. No digits in given string!");
66 if (*tmpend !=
'\0') {
67 spdlog::error(
"config_loader.cpp - Int parsing error. String contains chars other than digits!");
70 if (val < INT_MIN || val > INT_MAX) {
71 spdlog::error(
"config_loader.cpp - Int parsing error. Parsed number is LONG but not INT.");
85 spdlog::info(
"config_loader.cpp - Initializing int with default value.");
90 if (*dest > max_val) {
91 spdlog::info(
"config_loader.cpp - Initializing int with default value due to overflow.");
104 int len = ini_string_parse(str, format);
106 spdlog::info(
"config_loader.cpp - Initializing string with default value.");
123 static int parse_and_init(IniDispatch *
const disp,
void *
const v_other) {
129 if (disp->type == INI_UNKNOWN) {
131 spdlog::error(
"config_loader.cpp - Couldn't determine type of config line. Key: {0}, Value: {1}",
137 if (disp->type == INI_KEY) {
148 #define conf ((app_config*) v_other)
149 #define IS_SECTION(SECTION) ini_array_match(SECTION, disp->append_to, '.', disp->format)
150 #define IS_KEY(KEY) ini_string_match_si(KEY, disp->data, disp->format)
152 if (disp->at_len == 0) {
156 if (
IS_KEY(
"debug_screens")) {
160 conf->debug_screens = ini_get_bool_i(disp->value, 0, disp->format);
161 }
else if (
IS_KEY(
"optional_config_path")) {
162 int len = ini_string_parse(disp->value, disp->format);
164 conf->opt_conf_path = disp->value;
166 }
else if (
IS_KEY(
"init_failure_quit")) {
167 conf->init_fail_quit = ini_get_bool_i(disp->value, 0, disp->format);
168 }
else if (
IS_KEY(
"fonts_path")) {
170 if (
conf->fonts_path.back() !=
'/')
171 conf->fonts_path.append(
"/");
176 }
else if (
IS_KEY(
"log_debug")) {
177 conf->log_debug = ini_get_bool_i(disp->value, 0, disp->format);
178 }
else if (
IS_KEY(
"font")) {
180 }
else if (
IS_KEY(
"lang_path")) {
182 if (
conf->lang_path.back() !=
'/')
183 conf->lang_path.append(
"/");
184 }
else if (
IS_KEY(
"available_langs")) {
185 int size = ini_array_get_length(disp->value,
',', disp->format);
187 for (
int i = 0; i < size; i++) {
188 lang = ini_array_release(&(disp->value),
',', disp->format);
189 ini_string_parse(lang, disp->format);
190 conf->lang_options.push_back(lang);
192 }
else if (
IS_KEY(
"default_lang")) {
194 conf->lang_current =
conf->lang_default;
202 }
else if (
IS_KEY(
"mode")) {
204 }
else if (
IS_KEY(
"bits")) {
206 }
else if (
IS_KEY(
"speed")) {
208 }
else if (
IS_KEY(
"delay")) {
217 }
else if (
IS_KEY(
"mode")) {
219 }
else if (
IS_KEY(
"bits")) {
221 }
else if (
IS_KEY(
"speed")) {
223 }
else if (
IS_KEY(
"delay")) {
230 }
else if (
IS_KEY(
"user")) {
232 }
else if (
IS_KEY(
"password")) {
234 }
else if (
IS_KEY(
"database")) {
236 }
else if (
IS_KEY(
"port")) {
243 }
else if (
IS_KEY(
"clock_pin")) {
245 }
else if (
IS_KEY(
"rate")) {
278 spdlog::critical(
"config_loader.cpp - Failed to read main configuration file (app_config.conf)");
287 std::fstream config(cfg_path, std::ios::in);
288 if (config.is_open()) {
298 std::fstream config(cfg_path, std::ios::in);
300 if (!config.is_open()) {
301 spdlog::error(
"config_loader.cpp - Failed to open optional config file. Cannot read config.");
308 while (std::getline(config, line)) {
309 sep_index = line.find_first_of(
'#');
310 if (sep_index != line.npos) {
311 line = line.substr(0, sep_index);
314 sep_index = line.find_first_of(
'=');
315 if (sep_index == line.npos) {
316 spdlog::debug(
"config_loader.cpp - Config line doesn't have a key! Skipping line. (Line: {0})",
321 std::string key = line.substr(0, sep_index);
323 std::string val = line.substr(sep_index + 1, line.length());
329 spdlog::debug(
"config_loader.cpp - Optional config found {0}", key.c_str());
332 spdlog::debug(
"config_loader.cpp - Optional config found {0}", key.c_str());
335 spdlog::error(
"config_loader.cpp - Optional config: Unknown key: {0}", key.c_str());
337 }
catch (std::invalid_argument &ex) {
338 spdlog::error(
"config_loader.cpp - Optional config: {0} - {1} IS NOT a number!",
339 key.c_str(), val.c_str());
340 }
catch (std::out_of_range &ex) {
341 spdlog::error(
"config_loader.cpp - Optional config: {0} - {1} value is OUT OF RANGE",
342 key.c_str(), val.c_str());
345 spdlog::debug(
"config_loader.cpp - Optional config: {0} - {1}", key.c_str(), val.c_str());
353 std::fstream config(cfg_path, std::ios::in);
354 std::vector<std::string> lines;
356 if (!config.is_open()) {
357 spdlog::error(
"config_loader.cpp - Failed to open optional config file. Cannot read config.");
363 while (std::getline(config, line)) {
364 lines.push_back(line);
368 std::ofstream new_config(cfg_path, std::ios::trunc);
371 for (
size_t i = 0; i < lines.size(); i++) {
372 sep_index = lines.at(i).find_first_of(
'=');
374 if (lines.at(i)[0] ==
'#' || sep_index == lines.at(i).npos) {
375 new_config << lines.at(i) <<
"\n";
379 std::string ex_key = lines.at(i).substr(0, sep_index);
382 if (!ex_key.compare(key)) {
383 std::string new_line = key +
"=" + value;
384 new_config << new_line <<
"\n";
386 new_config << lines.at(i) <<
"\n";
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
#define GEN_DEFAULT_FONTS_PATH
#define HX_DEFAULT_CLOCK_PIN
#define RFID_DEFAULT_MODE
#define GEN_DEFAULT_LANG_PATH
#define DB_DEFAULT_DATABASE
#define IS_SECTION(SECTION)
#define RFID_DEFAULT_DELAY
#define HX_DEFAULT_OFFSET
#define RFID_DEFAULT_BITS
#define DB_DEFAULT_PASSWORD
#define RFID_DEFAULT_SPEED
#define HX_DEFAULT_DATA_PIN
#define HX_DEFAULT_REF_UNIT
#define OPT_CFG_REF_UNIT_KEY
#define OPT_CFG_OFFSET_KEY
int replace_opt_in_opt_config(const char *cfg_path, std::string key, std::string value)
This is used to replace a values in optional config.
int load_main_config_ini(const char *cfg_path)
This funciton loads main configuration. _ini symobolizes that the expected format is ....
int load_opt_config(const char *cfg_path)
Loads the optional config.
int parse_int(const char *str, int *res)
int parse_and_init_int(const char *str, int *dest, int default_val, int max_val=INT_MAX)
int exist_opt_config(const char *cfg_path)
Checks if optional config exists.
int parse_and_init_str(std::string &dest, char *str, IniFormat format, const char *default_val)
Structure that holds the values of the application configuration file.
app_workspace_ns::hx711_config hx_conf
Structure holding SPI device and SPI config which is a library structure "spi_config_t".