Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
Functions
config_loader Namespace Reference

Functions

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 parse_and_init_str (std::string &dest, char *str, IniFormat format, const char *default_val)
 
int load_main_config_ini (const char *cfg_path)
 This funciton loads main configuration. _ini symobolizes that the expected format is .ini. More...
 
int exist_opt_config (const char *cfg_path)
 Checks if optional config exists. More...
 
int load_opt_config (const char *cfg_path)
 Loads the optional config. More...
 
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. More...
 

Function Documentation

◆ exist_opt_config()

int config_loader::exist_opt_config ( const char *  cfg_path)

Checks if optional config exists.

Parameters
cfg_pathconfig file path
Returns
int 0 on succes

Definition at line 286 of file config_loader.cpp.

286  {
287  std::fstream config(cfg_path, std::ios::in);
288  if (config.is_open()) {
289  config.close();
290  return 0;
291  } else
292  return 1;
293  }

◆ load_main_config_ini()

int config_loader::load_main_config_ini ( const char *  cfg_path)

This funciton loads main configuration. _ini symobolizes that the expected format is .ini.

Parameters
cfg_pathconfig file path
Returns
int 0 on success

Definition at line 268 of file config_loader.cpp.

268  {
269  app_config *cfg = app_workspace::get_instance()->main_config.get();
270 
271  if (load_ini_path(
272  cfg_path,
273  INI_DEFAULT_FORMAT,
274  NULL,
275  parse_and_init,
276  cfg))
277  {
278  spdlog::critical("config_loader.cpp - Failed to read main configuration file (app_config.conf)");
279  return 1;
280  }
281 
282  return 0;
283  }
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
Structure that holds the values of the application configuration file.

◆ load_opt_config()

int config_loader::load_opt_config ( const char *  cfg_path)

Loads the optional config.

Parameters
cfg_pathconfig file path
Returns
int 0 on succes

Definition at line 295 of file config_loader.cpp.

295  {
296  app_config *cfg = app_workspace::get_instance()->main_config.get();
297  // std::fstream config(cfg->opt_conf_path, std::ios::in);
298  std::fstream config(cfg_path, std::ios::in);
299 
300  if (!config.is_open()) {
301  spdlog::error("config_loader.cpp - Failed to open optional config file. Cannot read config.");
302  return 1;
303  }
304 
305  std::string line;
306  size_t sep_index;
307 
308  while (std::getline(config, line)) {
309  sep_index = line.find_first_of('#');
310  if (sep_index != line.npos) { // if the line contains comment trim line only up to #
311  line = line.substr(0, sep_index);
312  }
313 
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})",
317  line.c_str());
318  continue;
319  }
320 
321  std::string key = line.substr(0, sep_index);
322  trim(key);
323  std::string val = line.substr(sep_index + 1, line.length());
324  trim(val);
325 
326  try {
327  // warning! KEY MUST BE in lower case, not doing tolower()
328  if (!key.compare(OPT_CFG_REF_UNIT_KEY)) { // is ref_unit
329  spdlog::debug("config_loader.cpp - Optional config found {0}", key.c_str());
330  cfg->hx_conf.ref_unit = std::stoi(val);
331  } else if (!key.compare(OPT_CFG_OFFSET_KEY)) { // is offset
332  spdlog::debug("config_loader.cpp - Optional config found {0}", key.c_str());
333  cfg->hx_conf.offset = std::stoi(val);
334  } else { // is nothing else
335  spdlog::error("config_loader.cpp - Optional config: Unknown key: {0}", key.c_str());
336  }
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());
343  }
344 
345  spdlog::debug("config_loader.cpp - Optional config: {0} - {1}", key.c_str(), val.c_str());
346  }
347 
348  config.close();
349  return 0;
350  }
#define OPT_CFG_REF_UNIT_KEY
Definition: config_loader.h:6
#define OPT_CFG_OFFSET_KEY
Definition: config_loader.h:7
app_workspace_ns::hx711_config hx_conf

◆ parse_and_init_int()

int config_loader::parse_and_init_int ( const char *  str,
int *  dest,
int  default_val,
int  max_val = INT_MAX 
)

calls parse_int. On success dest contains parsing result on failure @default_val is put to dest ** added @max_val to prevent overflows return 0 on success

Definition at line 83 of file config_loader.cpp.

83  {
84  if (parse_int(str, dest)) {
85  spdlog::info("config_loader.cpp - Initializing int with default value.");
86  *dest = default_val;
87  return 1;
88  } // no else needed - parse_int() already assigns result to dest
89 
90  if (*dest > max_val) {
91  spdlog::info("config_loader.cpp - Initializing int with default value due to overflow.");
92  *dest = default_val;
93  return 1;
94  }
95 
96  return 0;
97  }
int parse_int(const char *str, int *res)

◆ parse_and_init_str()

int config_loader::parse_and_init_str ( std::string &  dest,
char *  str,
IniFormat  format,
const char *  default_val 
)

Parses string and if parset string isn't valid (size <= 0) then inits defalt value. @dest is result of parsing. @str is parsed string, @format is library data format, @default_val...

Definition at line 103 of file config_loader.cpp.

103  {
104  int len = ini_string_parse(str, format);
105  if (len <= 0) {
106  spdlog::info("config_loader.cpp - Initializing string with default value.");
107  dest = default_val;
108  } else {
109  dest = str;
110  }
111 
112  return 0;
113  }

◆ parse_int()

int config_loader::parse_int ( const char *  str,
int *  res 
)

parses @str to @res and returns 0 on success, other values on error

Definition at line 51 of file config_loader.cpp.

51  {
52  char *tmpend;
53  long val;
54 
55  errno = 0;
56  val = strtol(str, &tmpend, 10);
57 
58  if (errno != 0) {// over/under flow and other error
59  spdlog::error("config_loader.cpp - Int parsing error. Over/Under-flow?");
60  return 1;
61  }
62  if (str == tmpend) {// no digits
63  spdlog::error("config_loader.cpp - Int parsing error. No digits in given string!");
64  return 1;
65  }
66  if (*tmpend != '\0') {
67  spdlog::error("config_loader.cpp - Int parsing error. String contains chars other than digits!");
68  return 1;
69  }
70  if (val < INT_MIN || val > INT_MAX) {
71  spdlog::error("config_loader.cpp - Int parsing error. Parsed number is LONG but not INT.");
72  return 1;
73  }
74 
75  *res = val;
76  return 0;
77  }

◆ replace_opt_in_opt_config()

int config_loader::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.

Parameters
cfg_pathconfig file path
keywhich value is changed
valueto be set
Returns
int 0 on success, 1 if config couldn't be opened

Definition at line 352 of file config_loader.cpp.

352  {
353  std::fstream config(cfg_path, std::ios::in);
354  std::vector<std::string> lines;
355 
356  if (!config.is_open()) {
357  spdlog::error("config_loader.cpp - Failed to open optional config file. Cannot read config.");
358  return 1;
359  }
360 
361  std::string line;
362 
363  while (std::getline(config, line)) {
364  lines.push_back(line);
365  }
366  config.close();
367 
368  std::ofstream new_config(cfg_path, std::ios::trunc);
369  size_t sep_index;
370 
371  for (size_t i = 0; i < lines.size(); i++) {
372  sep_index = lines.at(i).find_first_of('=');
373 
374  if (lines.at(i)[0] == '#' || sep_index == lines.at(i).npos) { // lines a comment or doesnt have key
375  new_config << lines.at(i) << "\n";
376  continue;
377  }
378 
379  std::string ex_key = lines.at(i).substr(0, sep_index);
380  trim(ex_key);
381 
382  if (!ex_key.compare(key)) {
383  std::string new_line = key + "=" + value;
384  new_config << new_line << "\n";
385  } else {
386  new_config << lines.at(i) << "\n";
387  }
388  }
389 
390  new_config.flush();
391  new_config.close();
392 
393  return 0;
394  }