68 char** split_result = NULL;
74 int fd = open(filename, O_RDONLY);
87 n_log(
LOG_ERR,
"fdopen failed for %s (fd=%d): %s",
_str(filename), fd, strerror(errno));
107 cfg_file->
filename = strdup(filename);
120 if (!default_section) {
133 n_log(
LOG_ERR,
"couldn't allocate default_section name");
137 Free(default_section);
145 if (!default_section->
entries) {
151 Free(default_section);
159 size_t line_number = 0;
163 char* bufptr = (
char*)&buffer;
167 if (strlen(bufptr) == 0) {
180 if (
skipu(bufptr,
']', &end, 1) != TRUE) {
181 n_log(
LOG_ERR,
"coulnd't find end of section at line %zu of %s (string:%s)", line_number, filename, buffer);
187 bufptr[end - 1] =
'\0';
189 if (strlen(bufptr) == 0) {
190 n_log(
LOG_ERR,
"section without name at line %zu", line_number);
220 section = default_section;
221 n_log(
LOG_DEBUG,
"No section, setting __DEFAULT__ starting line %zu of %s (string:%s)", line_number, filename, bufptr);
225 Malloc(split_result,
char*, 3);
227 n_log(
LOG_ERR,
"allocation failed at line %zu of %s", line_number, filename);
231 split_result[2] = NULL;
233 split_result[1] = strdup((
char*)npcre->
match_list[2]);
239 n_log(
LOG_ERR,
"couldn't find entry separator '=' at line %zu of %s (string:%s)", line_number, filename, bufptr);
244 n_log(
LOG_ERR,
"Invalid split count at line %zu of %s (string:%s, count:%d)", line_number, filename, bufptr,
split_count(split_result));
251 n_log(
LOG_ERR,
"couldn't find entry name at line %zu of %s (string:%s)", line_number, filename, buffer);
257 const char* valptr = NULL;
259 int closing_delimiter_found = 0;
260 int opening_delimiter_found = 0;
262 char delimiter =
'\0';
267 Free(split_result[1]);
273 size_t it = strlen(valptr);
278 if (split_result[1][it] ==
';' || split_result[1][it] ==
'#') {
279 split_result[1][it] =
'\0';
282 if (split_result[1][it] ==
'"' || split_result[1][it] ==
'\'') {
283 delimiter = split_result[1][it];
284 split_result[1][it] =
'\0';
285 closing_delimiter_found = 1;
296 Free(split_result[1]);
301 if (split_result[1]) {
304 while (split_result[1][it] !=
'\0') {
305 if (split_result[1][it] ==
';' || split_result[1][it] ==
'#') {
306 split_result[1][it] =
'\0';
308 if (delimiter !=
'\0' && split_result[1][it] == delimiter) {
309 opening_delimiter_found = 1;
311 valptr = split_result[1] + it;
318 Free(split_result[1]);
324 if (delimiter !=
'\0' && ((opening_delimiter_found && !closing_delimiter_found) || (!opening_delimiter_found && closing_delimiter_found))) {
326 n_log(
LOG_ERR,
"Only one delimiter found at line %zu of %s (string:%s)", line_number, filename, buffer);
332 N_STR* entry_key = NULL;
333 size_t nb_entry_in_sections = 0;
339 nb_entry_in_sections++;
344 strptr = strdup(valptr);
373 int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600);
376 n_log(
LOG_ERR,
"Unable to open '%s' for writing (0600): %s",
_str(filename), strerror(error));
381 out = fdopen(fd,
"w");
384 n_log(
LOG_ERR,
"fdopen failed for '%s' (fd=%d): %s",
_str(filename), fd, strerror(error));
389 char* last_section = NULL;
391 const char *section = NULL, *
key = NULL, *val = NULL;
395 if (!last_section || strcmp(last_section, section) != 0) {
396 fprintf(out,
"[%s]\n", section);
398 last_section = strdup(section);
400 fprintf(out,
"%s=%s\n",
key, val);
419 size_t nb_sections = 0;
447 size_t nb_sections = 0;
448 size_t nb_entry_in_sections = 0;
452 if (nb_sections == section_position) {
454 N_STR* entry_key = NULL;
456 nstrprintf(entry_key,
"%s%zu", entry, nb_entry_in_sections);
461 nb_entry_in_sections++;
469 return nb_entry_in_sections;
492 size_t section_position_it = 0;
497 if (section_position_it == section_position) {
501 section_position_it++;
511 N_STR* entry_key = NULL;
512 nstrprintf(entry_key,
"%s%zu", entry, entry_position);
529 __n_assert(cfg_file && (*cfg_file),
return FALSE);
531 Free((*cfg_file)->filename);
533 if ((*cfg_file)->sections) {
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
HASH_TABLE * entries
hash table of key-value entries
char * section_name
name of the config section
char * filename
path and name of the config file
LIST * sections
list of CONFIG_FILE_SECTION
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
int write_config_file(CONFIG_FILE *cfg_file, char *filename)
write a config file
#define config_endfor
Foreach elements of CONFIG_FILE macro END.
#define CONFIG_SECTION_HASH_TABLE_LEN
size of the hash table of config sections entries
int destroy_config_file(CONFIG_FILE **cfg_file)
Destroy a loaded config file.
#define MAX_CONFIG_LINE_LEN
maximum length of a single config line
char * get_config_section_value(CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry, size_t entry_position)
Function to parse sections and get entries values.
#define config_foreach(__config, __section_name, __key, __val)
Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key ,...
size_t get_nb_config_file_sections(CONFIG_FILE *cfg_file, const char *section_name)
Get the number of config file with section_name.
size_t get_nb_config_file_sections_entries(CONFIG_FILE *cfg_file, const char *section_name, size_t section_position, char *entry)
Get the number of config file with section_name.
Structure of a config file.
Structure of a config section.
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
HASH_TABLE * new_ht(size_t size)
Create a hash table with the given size.
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr), void *(*duplicator)(void *ptr))
put an arbitrary pointer value with given key in the targeted hash table
structure of a hash table
size_t nb_items
number of item currently in the list
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
char * trim_nocopy(char *s)
trim and zero end the string, WARNING: keep and original pointer to delete the string correctly
size_t NSTRBYTE
N_STR base unit.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
int split_count(char **split_result)
Count split elements.
int skipu(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
char * str_replace(const char *string, const char *substr, const char *replacement)
Replace "substr" by "replacement" inside string taken from http://coding.debuntu.org/c-implementing-s...
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
int free_split_result(char ***tab)
Free a split result allocated array.
A box including a string and his lenght.
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
N_PCRE * npcre_new(char *str, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Common headers and low-level functions & define.
void free_no_null(void *ptr)
local free for config entries
void destroy_config_file_section(void *ptr)
Destroy a config file section.
Config file reading and writing.
PCRE helpers for regex matching.