Read a configuration file: Difference between revisions

Content added Content deleted
(→‎C: Added example)
Line 614: Line 614:
#include <confini.h>
#include <confini.h>


#define bool unsigned char
#define rosetta_uint8_t unsigned char


#define FALSE 0
#define FALSE 0
#define TRUE 1
#define TRUE 1


#define CONFIGS_MEMBERS 5
#define CONFIGS_TO_READ 5
#define INI_ARRAY_DELIMITER ','
#define INI_ARRAY_DELIMITER ','


Line 626: Line 626:
char *fullname;
char *fullname;
char *favouritefruit;
char *favouritefruit;
bool needspeeling;
rosetta_uint8_t needspeeling;
bool seedsremoved;
rosetta_uint8_t seedsremoved;
char **otherfamily;
char **otherfamily;
size_t otherfamily_len;
size_t otherfamily_len;
Line 671: Line 671:
}
}


if (!confs->fullname && ini_string_match_si("FULLNAME", this->data, this->format)) {
/* Remove all quotes, if any */
this->d_len = ini_string_parse(this->data, this->format);

if (ini_string_match_ss("FULLNAME", this->data, this->format)) {


this->v_len = ini_string_parse(this->value, this->format); /* Remove all quotes, if any */
this->v_len = ini_string_parse(this->value, this->format); /* Remove all quotes, if any */
Line 680: Line 677:
confs->_configs_left_--;
confs->_configs_left_--;


} else if (ini_string_match_ss("FAVOURITEFRUIT", this->data, this->format)) {
} else if (!confs->favouritefruit && ini_string_match_si("FAVOURITEFRUIT", this->data, this->format)) {


this->v_len = ini_string_parse(this->value, this->format); /* Remove all quotes, if any */
this->v_len = ini_string_parse(this->value, this->format); /* Remove all quotes, if any */
Line 686: Line 683:
confs->_configs_left_--;
confs->_configs_left_--;


} else if (ini_string_match_ss("NEEDSPEELING", this->data, this->format)) {
} else if ((confs->needspeeling & 0x80) && ini_string_match_si("NEEDSPEELING", this->data, this->format)) {


confs->needspeeling = ini_get_bool(this->value, TRUE);
confs->needspeeling = ini_get_bool(this->value, TRUE);
confs->_configs_left_--;
confs->_configs_left_--;


} else if (ini_string_match_ss("SEEDSREMOVED", this->data, this->format)) {
} else if ((confs->seedsremoved & 0x80) && ini_string_match_si("SEEDSREMOVED", this->data, this->format)) {


confs->seedsremoved = ini_get_bool(this->value, TRUE);
confs->seedsremoved = ini_get_bool(this->value, TRUE);
confs->_configs_left_--;
confs->_configs_left_--;


} else if (ini_string_match_ss("OTHERFAMILY", this->data, this->format)) {
} else if (!confs->otherfamily && ini_string_match_si("OTHERFAMILY", this->data, this->format)) {


this->v_len = ini_array_collapse(this->value, INI_ARRAY_DELIMITER, this->format); /* Save memory (not strictly needed) */
this->v_len = ini_array_collapse(this->value, INI_ARRAY_DELIMITER, this->format); /* Save memory (not strictly needed) */
Line 717: Line 714:
.hash_marker = INI_IGNORE,
.hash_marker = INI_IGNORE,
.multiline_nodes = INI_NO_MULTILINE,
.multiline_nodes = INI_NO_MULTILINE,
.case_sensitive = TRUE,
.case_sensitive = FALSE,
.no_spaces_in_names = TRUE,
.no_spaces_in_names = TRUE,
.no_single_quotes = FALSE,
.no_single_quotes = FALSE,
Line 728: Line 725:
};
};


*confs = (struct configs) { NULL, NULL, FALSE, FALSE, NULL, 0, CONFIGS_MEMBERS };
*confs = (struct configs) { NULL, NULL, 0x80, 0x80, NULL, 0, CONFIGS_TO_READ };


if (load_ini_path("rosetta.conf", config_format, NULL, configs_member_handler, confs) & CONFINI_ERROR) {
if (load_ini_path("rosetta.conf", config_format, NULL, configs_member_handler, confs) & CONFINI_ERROR) {
Line 747: Line 744:
ini_global_set_implicit_value("YES", 0);
ini_global_set_implicit_value("YES", 0);
populate_configs(&confs);
populate_configs(&confs);
confs.needspeeling &= 0x7F;
confs.seedsremoved &= 0x7F;


/* Print the configurations parsed */
/* Print the configurations parsed */