libconfini
Yet another INI parser
Macros | Functions | Variables
confini.c File Reference

libconfini functions More...

#include <stdlib.h>
#include "confini.h"

Macros

#define CONFINI_IO_FLAVOR   CONFINI_STANDARD
 The I/O API to use (possibly overridden via -DCONFINI_IO_FLAVOR=[FLAVOR]) More...
 

Functions

static unsigned char is_some_space (const char chr, const int_least8_t depth)
 Check whether a character is a space. More...
 
static size_t ltrim_s (const char *const str, const size_t offs, const int_least8_t depth)
 Soft left trim – does not change the buffer. More...
 
static size_t ltrim_h (char *const str, const size_t offs, const int_least8_t depth)
 Hard left trim – does change the buffer. More...
 
static size_t ltrim_hh (char *const str, const size_t offs, const int_least8_t depth)
 Shifting left trim – does change the buffer. More...
 
static size_t rtrim_s (const char *const str, const size_t len, const int_least8_t depth)
 Soft right trim – does not change the buffer. More...
 
static size_t rtrim_h (char *const str, const size_t len, const int_least8_t depth)
 Hard right trim – does change the buffer. More...
 
static size_t urtrim_s (const char *const str, const size_t len)
 Unparsed soft right trim (right trim of /(?:\s|\\[\n\r])+$/) – does not change the buffer. More...
 
static void string_tolower (char *const str)
 Convert an ASCII string to lower case. More...
 
static size_t qultrim_h (char *const srcstr, const size_t offs, const IniFormat format)
 Unparsed hard left trim (left trim of /^(?:\s+|\\[\n\r]|''|"")+/) – does change the buffer. More...
 
static size_t dqultrim_s (const char *const srcstr, const size_t offs, const IniFormat format)
 Soft left trim within an unparsed disabled entry (left trim of /(?:(?:^|\\?[\n\r])[ \t\v\f]*(?:#(?:[ \t\v\f]|''|"")*)?)+/) – does not change the buffer. More...
 
static size_t getn_metachar_pos (const char *const str, const char chr, const size_t len, const IniFormat format)
 Get the position of the first occurrence out of quotes of a given character, stopping after a given number of charcters. More...
 
static size_t get_metachar_pos (const char *const str, const char chr, const IniFormat format)
 Get the position of the first occurrence out of quotes of a given character. More...
 
static size_t unescape_cr_lf (char *const srcstr, const size_t len, const unsigned char is_disabled, const IniFormat format)
 Replace /\\(\n\r?|\r\n?)[\t \v\f]*[#;]/ or /\\(\n\r?|\r\n?)/ with "$1" More...
 
static size_t sanitize_section_path (char *const secpath, const IniFormat format)
 Sanitize a section path. More...
 
static size_t collapse_everything (char *const ini_string, const IniFormat format)
 Out of quotes similar to ECMAScript ini_string.replace(/''|""/g, "").replace(/^[\n\r]\s*|\s+/g, " ") More...
 
static size_t collapse_spaces (char *const ini_string, const IniFormat format)
 Out of quotes similar to ECMAScript ini_string.replace(/\s+/g, " ") More...
 
static size_t collapse_empty_quotes (char *const str, const IniFormat format)
 Similar to ECMAScript str.replace(/''|""/g, "") More...
 
static size_t uncomment (char *const srcstr, size_t len, const IniFormat format)
 Remove all comment initializers (# and/or ;) from the beginning of each line of a comment. More...
 
static uint_least8_t get_type_as_active (const char *const srcstr, const size_t len, const unsigned char allow_implicit, const IniFormat format)
 Try to determine the type of a member assuming it is active More...
 
static size_t further_cuts (char *const srcstr, const IniFormat format)
 Examine a (single-/multi-line) segment and check whether it contains more than just one node. More...
 
int strip_ini_cache (register char *const ini_source, const size_t ini_length, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
 Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback. More...
 
int load_ini_file (FILE *const ini_file, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
 Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument. More...
 
int load_ini_path (const char *const path, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
 Parse an INI file and dispatch its content to a custom callback using a path as argument. More...
 
bool ini_string_match_ss (const char *const simple_string_a, const char *const simple_string_b, const IniFormat format)
 Compare two simple strings and check whether they match. More...
 
bool ini_string_match_si (const char *const simple_string, const char *const ini_string, const IniFormat format)
 Compare a simple string and an INI string and and check whether they match. More...
 
bool ini_string_match_ii (const char *const ini_string_a, const char *const ini_string_b, const IniFormat format)
 Compare two INI strings and check whether they match. More...
 
bool ini_array_match (const char *const ini_string_a, const char *const ini_string_b, const char delimiter, const IniFormat format)
 Compare two INI arrays and check whether they match. More...
 
size_t ini_unquote (char *const ini_string, const IniFormat format)
 Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered metacharacters in respect to the format given) More...
 
size_t ini_string_parse (char *const ini_string, const IniFormat format)
 Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered metacharacters in respect to the format given); if the format allows it, sequences of one or more spaces out of quotes will be collapsed. More...
 
size_t ini_array_get_length (const char *const ini_string, const char delimiter, const IniFormat format)
 Get the length of a stringified INI array in number of members. More...
 
int ini_array_foreach (const char *const ini_string, const char delimiter, const IniFormat format, const IniSubstrHandler f_foreach, void *const user_data)
 Call a custom function for each member of a stringified INI array, without modifying the content of the buffer – useful for read-only (const) stringified arrays. More...
 
size_t ini_array_shift (const char **const ini_strptr, const char delimiter, const IniFormat format)
 Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the content of the buffer), or to NULL if the INI array has no more members – useful for read-only (const) stringified arrays. More...
 
size_t ini_array_collapse (char *const ini_string, const char delimiter, const IniFormat format)
 Compress the distribution of the data in a stringified INI array by removing all the white spaces that surround its delimiters, empty quotes, collapsable spaces, etc. More...
 
char * ini_array_break (char *const ini_string, const char delimiter, const IniFormat format)
 Replace the first delimiter found (together with the spaces that surround it) with \0 More...
 
char * ini_array_release (char **const ini_strptr, const char delimiter, const IniFormat format)
 Replace the first delimiter found (together with the spaces that surround it) with \0, then shifts the location pointed by ini_strptr to the next member of the INI array, or to NULL if the INI array has no more members. More...
 
int ini_array_split (char *const ini_string, const char delimiter, const IniFormat format, const IniStrHandler f_foreach, void *const user_data)
 Split a stringified INI array into NUL-separated members and call a custom function for each member. More...
 
void ini_global_set_lowercase_mode (const bool lowercase)
 Set the value of the global variable INI_GLOBAL_LOWERCASE_MODE. More...
 
void ini_global_set_implicit_value (char *const implicit_value, const size_t implicit_v_len)
 Set the value to be to be assigned to implicit keys. More...
 
IniFormatNum ini_fton (const IniFormat source)
 Calculate the IniFormatNum of an IniFormat. More...
 
IniFormat ini_ntof (const IniFormatNum format_num)
 Construct a new IniFormat according to an IniFormatNum. More...
 
int ini_get_bool (const char *const simple_string, const int when_fail)
 Check whether a simple string matches one of the booleans listed in the private constant INI_BOOLEANS (case-insensitive) More...
 
int ini_get_bool_i (const char *const ini_string, const int when_fail, const IniFormat format)
 Check whether an INI string matches one of the booleans listed in the private constant INI_BOOLEANS (case-insensitive) More...
 

Variables

static const char *const INI_BOOLEANS [][2]
 A list of possible string representations of boolean pairs. More...
 
int(*const ini_get_int )(const char *ini_string) = atoi
 Pointer to atoi() More...
 
long int(*const ini_get_lint )(const char *ini_string) = atol
 Pointer to atol() More...
 
long long int(*const ini_get_llint )(const char *ini_string) = atoll
 Pointer to atoll() More...
 
double(*const ini_get_double )(const char *ini_string) = atof
 Pointer to atof() More...
 
double(*const ini_get_float )(const char *ini_string) = atof
 Legacy support for parsing a double data type – please do not use this function: use ini_get_double() instead. More...
 
bool INI_GLOBAL_LOWERCASE_MODE = 0
 If set to true, key and section names in case-insensitive INI formats will be dispatched lowercase, verbatim otherwise (default value: false) More...
 
char * INI_GLOBAL_IMPLICIT_VALUE = (char *) 0
 Value to be assigned to implicit keys (default value: NULL) More...
 
size_t INI_GLOBAL_IMPLICIT_V_LEN = 0
 Length of the value assigned to implicit keys (default value: 0) More...
 

Detailed Description

libconfini functions

Author
Stefano Gioffré
Version
1.16.4
Date
2016-2022
See also
https://madmurphy.github.io/libconfini

Macro Definition Documentation

◆ CONFINI_IO_FLAVOR

#define CONFINI_IO_FLAVOR   CONFINI_STANDARD

The I/O API to use (possibly overridden via -DCONFINI_IO_FLAVOR=[FLAVOR])

Possible values are CONFINI_STANDARD and CONFINI_POSIX

Function Documentation

◆ collapse_empty_quotes()

static size_t collapse_empty_quotes ( char *const  str,
const IniFormat  format 
)
static

Similar to ECMAScript str.replace(/''|""/g, "")

Parameters
strThe string to collapse
formatThe format of the INI file
Returns
The new length of the string

◆ collapse_everything()

static size_t collapse_everything ( char *const  ini_string,
const IniFormat  format 
)
static

Out of quotes similar to ECMAScript ini_string.replace(/''|""/g, "").replace(/^[\n\r]\s*|\s+/g, " ")

Parameters
ini_stringThe string to collapse – multi-line escape sequences must be already unescaped at this stage
formatThe format of the INI file
Returns
The new length of the string

◆ collapse_spaces()

static size_t collapse_spaces ( char *const  ini_string,
const IniFormat  format 
)
static

Out of quotes similar to ECMAScript ini_string.replace(/\s+/g, " ")

Parameters
ini_stringThe string to collapse – multi-line escape sequences must be already unescaped at this stage
formatThe format of the INI file
Returns
The new length of the string

◆ dqultrim_s()

static size_t dqultrim_s ( const char *const  srcstr,
const size_t  offs,
const IniFormat  format 
)
inlinestatic

Soft left trim within an unparsed disabled entry (left trim of /(?:(?:^|\\?[\n\r])[ \t\v\f]*(?:#(?:[ \t\v\f]|''|"")*)?)+/) – does not change the buffer.

Parameters
srcstrThe target string (it may contain multi-line escape sequences)
offsThe offset where to start the left trim
formatThe format of the INI file
Returns
The offset of the first non-trivial character

◆ further_cuts()

static size_t further_cuts ( char *const  srcstr,
const IniFormat  format 
)
static

Examine a (single-/multi-line) segment and check whether it contains more than just one node.

Parameters
srcstrSegment to examine (it may contain multi-line escape sequences)
formatThe format of the INI file
Returns
Number of entries found

◆ get_metachar_pos()

static size_t get_metachar_pos ( const char *const  str,
const char  chr,
const IniFormat  format 
)
inlinestatic

Get the position of the first occurrence out of quotes of a given character.

Parameters
strThe string where to search
chrThe character to to search
formatThe format of the INI file
Returns
The offset of the first occurrence of chr or the length of str if chr has not been not found

◆ get_type_as_active()

static uint_least8_t get_type_as_active ( const char *const  srcstr,
const size_t  len,
const unsigned char  allow_implicit,
const IniFormat  format 
)
static

Try to determine the type of a member assuming it is active

Parameters
srcstrString containing an individual node (it may contain multi-line escape sequences)
lenLength of the node
allow_implicitA boolean: true if keys without a key-value delimiter are allowed, false otherwise
formatThe format of the INI file
Returns
The active node type (see confini.h)

◆ getn_metachar_pos()

static size_t getn_metachar_pos ( const char *const  str,
const char  chr,
const size_t  len,
const IniFormat  format 
)
inlinestatic

Get the position of the first occurrence out of quotes of a given character, stopping after a given number of charcters.

Parameters
strThe string where to search
chrThe character to to search
lenThe maximum number of characters to read
formatThe format of the INI file
Returns
The offset of the first occurrence of chr, or len if chr has not been not found

◆ ini_array_break()

char* ini_array_break ( char *const  ini_string,
const char  delimiter,
const IniFormat  format 
)

Replace the first delimiter found (together with the spaces that surround it) with \0

Parameters
ini_stringThe stringified array (it can be NULL)
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
A pointer to the remaining INI array or NULL if the remaining array is empty

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

Similarly to strtok_r() this function can be used only once for a given string.

Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE this function is no-op and will return NULL.
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).
/* examples/topics/ini_array_break.c */
#include <stdio.h>
#include <confini.h>
static int my_ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
"my_array",
dispatch->data,
dispatch->format
)
) {
#define DELIMITER ','
char * part_a, * part_b = dispatch->value;
while ((part_a = part_b)) {
part_b = ini_array_break(part_b, DELIMITER, dispatch->format);
ini_string_parse(part_a, dispatch->format);
printf("%s\n", part_a);
}
#undef DELIMITER
}
return 0;
}
int main () {
if (
"../ini_files/typed_ini.conf",
NULL,
my_ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
libconfini header
int load_ini_path(const char *const path, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
Parse an INI file and dispatch its content to a custom callback using a path as argument.
Definition: confini.c:3274
bool ini_string_match_si(const char *const simple_string, const char *const ini_string, const IniFormat format)
Compare a simple string and an INI string and and check whether they match.
Definition: confini.c:3470
char * ini_array_break(char *const ini_string, const char delimiter, const IniFormat format)
Replace the first delimiter found (together with the spaces that surround it) with \0
Definition: confini.c:5067
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:524
size_t ini_string_parse(char *const ini_string, const IniFormat format)
Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...
Definition: confini.c:4256
Dispatch of a single INI node.
Definition: confini.h:131
char * data
Definition: confini.h:134
char * value
Definition: confini.h:135
const IniFormat format
Definition: confini.h:132

◆ ini_array_collapse()

size_t ini_array_collapse ( char *const  ini_string,
const char  delimiter,
const IniFormat  format 
)

Compress the distribution of the data in a stringified INI array by removing all the white spaces that surround its delimiters, empty quotes, collapsable spaces, etc.

Parameters
ini_stringThe stringified array
delimiterThe delimiter between the array members – if zero (INI_ANY_SPACE) any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
The new length of the stringified array

Out of quotes similar to ECMAScript ini_string.replace(new RegExp("^\\s+|\\s*(?:(" + delimiter + ")\\s*|($))", "g"), "$1$2"). If INI_ANY_SPACE (0) is used as delimiter, one or more different spaces (/[\t \v\f\n\r]+/) will be always collapsed to one space, independently of what the format says.

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

This function can be useful before invoking memcpy() using ini_string as source, when saving memory is a priority.

The format argument is used for the following fields:

  • format.no_single_quotes
  • format.no_double_quotes
  • format.do_not_collapse_values
  • format.preserve_empty_quotes

Examples:

  1. Using comma as delimiter:
    • Before:  first   ,    second   ,   third   ,  etc.  
    • After: first,second,third,etc.
  2. Using INI_ANY_SPACE as delimiter:
    • Before:   first    second    third     etc.   
    • After: first second third etc.
Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE this function is no-op and will only return the value of INI_GLOBAL_IMPLICIT_V_LEN minus the offset of ini_string within INI_GLOBAL_IMPLICIT_VALUE.
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).
/* examples/topics/ini_array_collapse.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
static int populate_strarray (
char * const part,
const size_t part_len,
const size_t idx,
const IniFormat format,
void * const v_array
) {
ini_string_parse(part, format);
((char **) v_array)[idx] = part;
return 0;
}
static int my_ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
"my_array",
dispatch->data,
dispatch->format
)
) {
#define DELIMITER ','
char ** my_array;
size_t my_array_length;
/* Save memory with `ini_array_collapse()` */
dispatch->value,
DELIMITER,
dispatch->format
);
/* Allocate a new array of strings with `malloc()` */
my_array_length = ini_array_get_length(
dispatch->value,
DELIMITER,
dispatch->format
);
my_array = (char **) malloc(my_array_length * sizeof(char *) +
dispatch->v_len + 1);
/* Copy the strings with `memcpy()` */
memcpy(
my_array + my_array_length,
dispatch->value,
dispatch->v_len + 1
);
/* Populate the array */
(char *) (my_array + my_array_length),
DELIMITER,
dispatch->format,
populate_strarray,
my_array
);
#undef DELIMITER
/* Do something with `my_array` */
printf("Array `my_array` has been created.\n\n");
for (size_t idx = 0; idx < my_array_length; idx++) {
printf("my_array[%zu] -> %s\n", idx, my_array[idx]);
}
}
return 0;
}
int main () {
if (
"../ini_files/typed_ini.conf",
NULL,
my_ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
int ini_array_split(char *const ini_string, const char delimiter, const IniFormat format, const IniStrHandler f_foreach, void *const user_data)
Split a stringified INI array into NUL-separated members and call a custom function for each member.
Definition: confini.c:5226
size_t ini_array_get_length(const char *const ini_string, const char delimiter, const IniFormat format)
Get the length of a stringified INI array in number of members.
Definition: confini.c:4472
size_t ini_array_collapse(char *const ini_string, const char delimiter, const IniFormat format)
Compress the distribution of the data in a stringified INI array by removing all the white spaces tha...
Definition: confini.c:4838
size_t v_len
Definition: confini.h:138
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:115
Note
The actual space occupied by the array might get reduced further after each member is parsed by ini_string_parse().

◆ ini_array_foreach()

int ini_array_foreach ( const char *const  ini_string,
const char  delimiter,
const IniFormat  format,
const IniSubstrHandler  f_foreach,
void *const  user_data 
)

Call a custom function for each member of a stringified INI array, without modifying the content of the buffer – useful for read-only (const) stringified arrays.

Parameters
ini_stringThe stringified array (it can be NULL)
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
f_foreachThe function that will be invoked for each array member
user_dataA custom argument, or NULL
Returns
Zero for success, otherwise an error code (see enum ConfiniInterruptNo)

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

The user given function f_foreach (see IniSubstrHandler data type) will be invoked with six arguments: ini_string, memb_offset (the offset of the member in bytes), memb_length (the length of the member in bytes), memb_num (the offset of the member in number of members), format (the format of the INI file), user_data (the custom argument user_data previously passed). If f_foreach returns a non-zero value the function ini_array_foreach() will be interrupted.

Note
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).

Possible return values are: CONFINI_SUCCESS, CONFINI_FEINTR.

/* examples/topics/ini_array_foreach.c */
#include <stdio.h>
#include <confini.h>
static int my_array_fragm_handler (
const char * const ini_array,
const size_t fragm_offset,
const size_t fragm_length,
const size_t fragm_num,
const IniFormat format,
void * const user_data
) {
printf("\"%.*s\"\n", (unsigned int) fragm_length, ini_array + fragm_offset);
return 0;
}
int main () {
"first , second , third",
',',
my_array_fragm_handler,
NULL
);
return 0;
}
int ini_array_foreach(const char *const ini_string, const char delimiter, const IniFormat format, const IniSubstrHandler f_foreach, void *const user_data)
Call a custom function for each member of a stringified INI array, without modifying the content of t...
Definition: confini.c:4593

◆ ini_array_get_length()

size_t ini_array_get_length ( const char *const  ini_string,
const char  delimiter,
const IniFormat  format 
)

Get the length of a stringified INI array in number of members.

Parameters
ini_stringThe stringified array (it can be NULL)
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
The length of the INI array in number of members

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

Note
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).

◆ ini_array_match()

bool ini_array_match ( const char *const  ini_string_a,
const char *const  ini_string_b,
const char  delimiter,
const IniFormat  format 
)

Compare two INI arrays and check whether they match.

Parameters
ini_string_aThe first INI array
ini_string_bThe second INI array
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
A boolean: true if the two arrays match, false otherwise

This function grants that the result of the comparison between two INI arrays will always match the the literal comparison between the individual members of both arrays after these have been parsed, one by one, by ini_string_parse() (with format.do_not_collapse_values set to false).

This function can be used, with '.' as delimiter, to compare section paths.

INI strings are the strings typically dispatched by load_ini_file(), load_ini_path() or strip_ini_cache(), which may contain quotes and the three escape sequences \\, \' and \".

In order to be suitable for both names and values, this function always considers sequences of one or more spaces out of quotes in both strings as collapsed, even when format.do_not_collapse_values is set to true.

The format argument is used for the following fields:

  • format.case_sensitive
  • format.no_double_quotes
  • format.no_single_quotes
  • format.multiline_nodes (INIFORMAT_HAS_NO_ESC())

◆ ini_array_release()

char* ini_array_release ( char **const  ini_strptr,
const char  delimiter,
const IniFormat  format 
)

Replace the first delimiter found (together with the spaces that surround it) with \0, then shifts the location pointed by ini_strptr to the next member of the INI array, or to NULL if the INI array has no more members.

Parameters
ini_strptrThe memory location of the stringified array – it cannot be NULL, but it can point to NULL
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
The array member that has been released

Usually ini_strptr comes from an IniDispatch (but any other string may be used as well).

Similarly to strtok_r() this function can be used only once for a given string.

Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE this function is no-op and will set ini_strptr to NULL.
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).
/* examples/topics/ini_array_release.c */
#include <stdio.h>
#include <confini.h>
static int my_ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
"my_array",
dispatch->data,
dispatch->format
)
) {
#define DELIMITER ','
char * token, * remaining = dispatch->value;
while ((
token = ini_array_release(&remaining, DELIMITER, dispatch->format)
)) {
ini_string_parse(token, dispatch->format);
printf("%s\n", token);
}
#undef DELIMITER
}
return 0;
}
int main () {
if (
"../ini_files/typed_ini.conf",
NULL,
my_ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
char * ini_array_release(char **const ini_strptr, const char delimiter, const IniFormat format)
Replace the first delimiter found (together with the spaces that surround it) with \0,...
Definition: confini.c:5146

◆ ini_array_shift()

size_t ini_array_shift ( const char **const  ini_strptr,
const char  delimiter,
const IniFormat  format 
)

Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the content of the buffer), or to NULL if the INI array has no more members – useful for read-only (const) stringified arrays.

Parameters
ini_strptrThe memory location of the stringified array – it cannot be NULL, but it can point to NULL
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
Returns
The length of the array member that has been left behind

Usually ini_strptr comes from an IniDispatch (but any other string may be used as well).

Note
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).
/* examples/topics/ini_array_shift.c */
#include <stdio.h>
#include <confini.h>
static int my_ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
"my_array",
dispatch->data,
dispatch->format
)
) {
#define DELIMITER ','
size_t length;
char * left_behind, * shifted = dispatch->value;
while ((left_behind = shifted)) {
length = ini_array_shift(
(const char **) &shifted,
DELIMITER,
dispatch->format
);
printf("%.*s\n", (unsigned int) length, left_behind);
}
#undef DELIMITER
}
return 0;
}
int main () {
if (
"../ini_files/typed_ini.conf",
NULL,
my_ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
size_t ini_array_shift(const char **const ini_strptr, const char delimiter, const IniFormat format)
Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the c...
Definition: confini.c:4740

◆ ini_array_split()

int ini_array_split ( char *const  ini_string,
const char  delimiter,
const IniFormat  format,
const IniStrHandler  f_foreach,
void *const  user_data 
)

Split a stringified INI array into NUL-separated members and call a custom function for each member.

Parameters
ini_stringThe stringified array (it cannot be NULL)
delimiterThe delimiter between the array members – if zero (see INI_ANY_SPACE), any space is delimiter (/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/)
formatThe format of the INI file
f_foreachThe function that will be invoked for each array member
user_dataA custom argument, or NULL
Returns
Zero for success, otherwise an error code (see enum ConfiniInterruptNo)

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

The user given function f_foreach (see IniStrHandler data type) will be invoked with five arguments: member (the member of the array), memb_length (the length of the member in bytes), memb_num (the offset of the member in number of members), format (the format of the INI file), user_data (the custom argument user_data previously passed). If f_foreach returns a non-zero value the function ini_array_split() will be interrupted.

Similarly to strtok_r() this function can be used only once for a given string.

Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE or is NULL this function is no-op and will return an error code.
If delimiter matches a metacharacter within the format given ('\\', '\'' or '\"'), its role as metacharacter will have higher priority than its role as delimiter (i.e., the array will have no delimiters and will contain only one member).

Possible return values are: CONFINI_SUCCESS, CONFINI_EROADDR, CONFINI_FEINTR.

/* examples/topics/ini_array_split.c */
#include <stdio.h>
#include <confini.h>
static int my_array_memb_handler (
char * const arr_member,
const size_t memb_length,
const size_t memb_num,
const IniFormat format,
void * const foreach_other
) {
printf("\"%s\"\n", arr_member);
return 0;
}
int main () {
char my_ini_array[] = "first . second . third";
my_ini_array,
'.',
my_array_memb_handler,
NULL
);
return 0;
}

◆ ini_fton()

IniFormatNum ini_fton ( const IniFormat  source)

Calculate the IniFormatNum of an IniFormat.

Parameters
sourceThe IniFormat to compute
Returns
The unique unsigned integer that identifies the format given

◆ ini_get_bool()

int ini_get_bool ( const char *const  simple_string,
const int  when_fail 
)

Check whether a simple string matches one of the booleans listed in the private constant INI_BOOLEANS (case-insensitive)

Parameters
simple_stringA string to check (it can be NULL)
when_failThe value that is returned if no matching boolean is found
Returns
The matching boolean (0 or 1) or when_fail if simple_string does not contain a valid INI boolean
/* examples/miscellanea/typed_ini.c */
/*
The following code will try to read an INI section called `my_section`,
expected to contain the following typed data:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.ini}
[my_section]
my_string = [string]
my_number = [number]
my_boolean = [boolean]
my_implicit_boolean
my_array = [comma-delimited array]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No errors will be generated if any of the data above are absent.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
#include "../utilities/make_strarray.h"
#define MY_ARRAY_DELIMITER ','
/* My stored data */
struct ini_store {
char * my_section_my_string;
int my_section_my_number;
bool my_section_my_boolean;
bool my_section_my_implicit_bool;
char * const * my_section_my_array;
size_t my_section_my_arr_len;
};
static int my_init (IniStatistics * statistics, void * v_store) {
*((struct ini_store *) v_store) = (struct ini_store) {
.my_section_my_string = NULL,
.my_section_my_number = -1,
.my_section_my_boolean = false,
.my_section_my_implicit_bool = false,
.my_section_my_array = NULL,
.my_section_my_arr_len = 0
};
return 0;
}
static int my_handler (IniDispatch * const dsp, void * const v_store) {
#define store ((struct ini_store *) v_store)
#define THEYMATCH(SSTR, ISTR) \
ini_string_match_si(SSTR, ISTR, dsp->format)
if (dsp->type == INI_KEY && THEYMATCH("my_section", dsp->append_to)) {
if (THEYMATCH("my_string", dsp->data)) {
dsp->v_len = ini_string_parse(dsp->value, dsp->format);
/* Free previous duplicate key (if any) */
free(store->my_section_my_string);
/* Allocate the new string */
store->my_section_my_string = strndup(dsp->value, dsp->v_len);
if (!store->my_section_my_string) {
return 1;
}
} else if (THEYMATCH("my_number", dsp->data)) {
store->my_section_my_number = ini_get_int(dsp->value);
} else if (THEYMATCH("my_boolean", dsp->data)) {
store->my_section_my_boolean = ini_get_bool_i(
dsp->value,
0,
dsp->format
);
} else if (THEYMATCH("my_implicit_boolean", dsp->data)) {
store->my_section_my_implicit_bool = ini_get_bool_i(
dsp->value,
1,
dsp->format
);
} else if (THEYMATCH("my_array", dsp->data)) {
/* Save memory (not strictly needed) */
dsp->value,
MY_ARRAY_DELIMITER,
dsp->format
);
/* Free previous duplicate key (if any) */
free((void *) store->my_section_my_array);
/* Allocate a new array of strings */
/* Function in examples/utilities/make_strarray.h */
store->my_section_my_array = make_strarray(
&store->my_section_my_arr_len,
dsp->value,
dsp->v_len,
MY_ARRAY_DELIMITER,
dsp->format
);
if (!store->my_section_my_array) {
return 1;
}
}
}
return 0;
#undef THEYMATCH
#undef store
}
static void print_stored_data (const struct ini_store * const store) {
printf(
"my_string -> %s\n"
"my_number -> %d\n"
"my_boolean -> %s\n"
"my_implicit_boolean -> %s\n"
"my_array[%zu] -> [%s",
store->my_section_my_string,
store->my_section_my_number,
store->my_section_my_boolean ? "True (`1`)" : "False (`0`)",
store->my_section_my_implicit_bool ? "True (`1`)" : "False (`0`)",
store->my_section_my_arr_len,
store->my_section_my_arr_len ? store->my_section_my_array[0] : ""
);
for (size_t idx = 1; idx < store->my_section_my_arr_len; idx++) {
printf("|%s", store->my_section_my_array[idx]);
}
printf("]\n");
}
int main () {
IniFormat my_format;
struct ini_store my_store;
my_format = INI_DEFAULT_FORMAT;
my_format.implicit_is_not_empty = true;
my_format.disabled_can_be_implicit = true;
my_format.semicolon_marker = my_format.hash_marker = INI_IGNORE;
"../ini_files/typed_ini.conf",
my_format,
my_init,
my_handler,
&my_store
)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
print_stored_data(&my_store);
free(my_store.my_section_my_string);
free((void *) my_store.my_section_my_array);
return 0;
}
void ini_global_set_implicit_value(char *const implicit_value, const size_t implicit_v_len)
Set the value to be to be assigned to implicit keys.
Definition: confini.c:5395
@ INI_KEY
Definition: confini.h:442
int ini_get_bool_i(const char *const ini_string, const int when_fail, const IniFormat format)
Check whether an INI string matches one of the booleans listed in the private constant INI_BOOLEANS (...
Definition: confini.c:5550
@ INI_IGNORE
Definition: confini.h:476
int(*const ini_get_int)(const char *ini_string)
Pointer to atoi()
Definition: confini.c:5733
uint_least8_t type
Definition: confini.h:133
const char * append_to
Definition: confini.h:136
unsigned char hash_marker
Definition: confini.h:115
unsigned char semicolon_marker
Definition: confini.h:115
unsigned char disabled_can_be_implicit
Definition: confini.h:115
unsigned char implicit_is_not_empty
Definition: confini.h:115
Global statistics about an INI file.
Definition: confini.h:121

◆ ini_get_bool_i()

int ini_get_bool_i ( const char *const  ini_string,
const int  when_fail,
const IniFormat  format 
)

Check whether an INI string matches one of the booleans listed in the private constant INI_BOOLEANS (case-insensitive)

Parameters
ini_stringA string to check (it can be NULL)
when_failThe value that is returned if no matching boolean is found
formatThe format of the INI file
Returns
The matching boolean (0 or 1) or when_fail if ini_string does not contain a valid INI boolean

Usually ini_string comes from an IniDispatch (but any other string may be used as well).

The format argument is used for the following fields:

  • format.no_double_quotes
  • format.no_single_quotes
/* examples/miscellanea/typed_ini.c */
/*
The following code will try to read an INI section called `my_section`,
expected to contain the following typed data:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.ini}
[my_section]
my_string = [string]
my_number = [number]
my_boolean = [boolean]
my_implicit_boolean
my_array = [comma-delimited array]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No errors will be generated if any of the data above are absent.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
#include "../utilities/make_strarray.h"
#define MY_ARRAY_DELIMITER ','
/* My stored data */
struct ini_store {
char * my_section_my_string;
int my_section_my_number;
bool my_section_my_boolean;
bool my_section_my_implicit_bool;
char * const * my_section_my_array;
size_t my_section_my_arr_len;
};
static int my_init (IniStatistics * statistics, void * v_store) {
*((struct ini_store *) v_store) = (struct ini_store) {
.my_section_my_string = NULL,
.my_section_my_number = -1,
.my_section_my_boolean = false,
.my_section_my_implicit_bool = false,
.my_section_my_array = NULL,
.my_section_my_arr_len = 0
};
return 0;
}
static int my_handler (IniDispatch * const dsp, void * const v_store) {
#define store ((struct ini_store *) v_store)
#define THEYMATCH(SSTR, ISTR) \
ini_string_match_si(SSTR, ISTR, dsp->format)
if (dsp->type == INI_KEY && THEYMATCH("my_section", dsp->append_to)) {
if (THEYMATCH("my_string", dsp->data)) {
dsp->v_len = ini_string_parse(dsp->value, dsp->format);
/* Free previous duplicate key (if any) */
free(store->my_section_my_string);
/* Allocate the new string */
store->my_section_my_string = strndup(dsp->value, dsp->v_len);
if (!store->my_section_my_string) {
return 1;
}
} else if (THEYMATCH("my_number", dsp->data)) {
store->my_section_my_number = ini_get_int(dsp->value);
} else if (THEYMATCH("my_boolean", dsp->data)) {
store->my_section_my_boolean = ini_get_bool_i(
dsp->value,
0,
dsp->format
);
} else if (THEYMATCH("my_implicit_boolean", dsp->data)) {
store->my_section_my_implicit_bool = ini_get_bool_i(
dsp->value,
1,
dsp->format
);
} else if (THEYMATCH("my_array", dsp->data)) {
/* Save memory (not strictly needed) */
dsp->value,
MY_ARRAY_DELIMITER,
dsp->format
);
/* Free previous duplicate key (if any) */
free((void *) store->my_section_my_array);
/* Allocate a new array of strings */
/* Function in examples/utilities/make_strarray.h */
store->my_section_my_array = make_strarray(
&store->my_section_my_arr_len,
dsp->value,
dsp->v_len,
MY_ARRAY_DELIMITER,
dsp->format
);
if (!store->my_section_my_array) {
return 1;
}
}
}
return 0;
#undef THEYMATCH
#undef store
}
static void print_stored_data (const struct ini_store * const store) {
printf(
"my_string -> %s\n"
"my_number -> %d\n"
"my_boolean -> %s\n"
"my_implicit_boolean -> %s\n"
"my_array[%zu] -> [%s",
store->my_section_my_string,
store->my_section_my_number,
store->my_section_my_boolean ? "True (`1`)" : "False (`0`)",
store->my_section_my_implicit_bool ? "True (`1`)" : "False (`0`)",
store->my_section_my_arr_len,
store->my_section_my_arr_len ? store->my_section_my_array[0] : ""
);
for (size_t idx = 1; idx < store->my_section_my_arr_len; idx++) {
printf("|%s", store->my_section_my_array[idx]);
}
printf("]\n");
}
int main () {
IniFormat my_format;
struct ini_store my_store;
my_format = INI_DEFAULT_FORMAT;
my_format.implicit_is_not_empty = true;
my_format.disabled_can_be_implicit = true;
my_format.semicolon_marker = my_format.hash_marker = INI_IGNORE;
"../ini_files/typed_ini.conf",
my_format,
my_init,
my_handler,
&my_store
)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
print_stored_data(&my_store);
free(my_store.my_section_my_string);
free((void *) my_store.my_section_my_array);
return 0;
}

◆ ini_global_set_implicit_value()

void ini_global_set_implicit_value ( char *const  implicit_value,
const size_t  implicit_v_len 
)

Set the value to be to be assigned to implicit keys.

Parameters
implicit_valueThe string to be used as implicit value (usually "YES", "TRUE", or "ON", or any other string; it can be NULL)
implicit_v_lenThe length of implicit_value (without counting the NUL terminator; use 0 for both an empty string and NULL)
Returns
Nothing
Warning
This function changes the value of one or more global variables. In order to be thread-safe this function should be used only once at beginning of execution, or otherwise a mutex logic must be introduced.
/* examples/topics/ini_global_set_implicit_value.c */
#include <stdio.h>
#include <confini.h>
static int ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (dispatch->value == INI_GLOBAL_IMPLICIT_VALUE) {
printf(
"\nDATA: %s\nVALUE: %s\nNODE TYPE: %u\n"
"(This is an implicit key element)\n",
dispatch->data,
dispatch->value,
dispatch->type
);
} else {
printf(
"\nDATA: %s\nVALUE: %s\nNODE TYPE: %u\n",
dispatch->data,
dispatch->value,
dispatch->type
);
}
return 0;
}
int main () {
ini_global_set_implicit_value("[implicit default value]", 24);
/* Without this implicit keys will be considered empty */
my_format.implicit_is_not_empty = true;
my_format.disabled_can_be_implicit = true;
if (
"../ini_files/unix-like.conf",
my_format,
NULL,
ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
}
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:5764
static const IniFormat INI_UNIXLIKE_FORMAT
A model format for Unix-like .conf files (where space characters are delimiters between keys and valu...
Definition: confini.h:532

◆ ini_global_set_lowercase_mode()

void ini_global_set_lowercase_mode ( const bool  lowercase)

Set the value of the global variable INI_GLOBAL_LOWERCASE_MODE.

Deprecated:
Deprecated since version 1.15.0 (it will be removed in version 2.0.0)
Parameters
lowercaseThe new value
Returns
Nothing

If lowercase is true, key and section names in case-insensitive INI formats will be dispatched lowercase, verbatim otherwise (default value: true).

Warning
This function changes the value of one or more global variables. In order to be thread-safe this function should be used only once at beginning of execution, or otherwise a mutex logic must be introduced.

◆ ini_ntof()

IniFormat ini_ntof ( const IniFormatNum  format_num)

Construct a new IniFormat according to an IniFormatNum.

Parameters
format_numThe IniFormatNum to parse
Returns
The new IniFormat constructed
Note
If format_num > 16777215 it will be truncated to 24 bits.

◆ ini_string_match_ii()

bool ini_string_match_ii ( const char *const  ini_string_a,
const char *const  ini_string_b,
const IniFormat  format 
)

Compare two INI strings and check whether they match.

Parameters
ini_string_aThe first INI string unescaped according to format
ini_string_bThe second INI string unescaped according to format
formatThe format of the INI file
Returns
A boolean: true if the two strings match, false otherwise

This function grants that the result of the comparison between two INI strings

printf(
"%s\n",
ini_string_match_ii(my_ini_string_1, my_ini_string_2, format) ?
"They match"
:
"They don't match"
);
bool ini_string_match_ii(const char *const ini_string_a, const char *const ini_string_b, const IniFormat format)
Compare two INI strings and check whether they match.
Definition: confini.c:3653

will always match the result of the literal comparison between the same two INI strings after these have been parsed by ini_string_parse() when format.do_not_collapse_values is set to false.

ini_string_parse(my_ini_string_1, format);
ini_string_parse(my_ini_string_2, format);
printf("%s\n",
ini_string_match_ss(my_ini_string_1, my_ini_string_2, format) ?
"They match"
:
"They don't match"
);
bool ini_string_match_ss(const char *const simple_string_a, const char *const simple_string_b, const IniFormat format)
Compare two simple strings and check whether they match.
Definition: confini.c:3369

INI strings are the strings typically dispatched by load_ini_file(), load_ini_path() or strip_ini_cache(), which may contain quotes and the three escape sequences \\, \' and \".

In order to be suitable for both names and values, this function always considers sequences of one or more spaces out of quotes in both strings as collapsed, even when format.do_not_collapse_values is set to true.

The format argument is used for the following fields:

  • format.case_sensitive
  • format.no_double_quotes
  • format.no_single_quotes
  • format.multiline_nodes (INIFORMAT_HAS_NO_ESC())

◆ ini_string_match_si()

bool ini_string_match_si ( const char *const  simple_string,
const char *const  ini_string,
const IniFormat  format 
)

Compare a simple string and an INI string and and check whether they match.

Parameters
ini_stringThe INI string escaped according to format
simple_stringThe simple string
formatThe format of the INI file
Returns
A boolean: true if the two strings match, false otherwise

This function grants that the result of the comparison between a simple string and an INI string

printf(
"%s\n",
ini_string_match_si(my_simple_string, my_ini_string, format) ?
"They match"
:
"They don't match"
);

will always match the result of the literal comparison between the simple string and the INI string after the latter has been parsed by ini_string_parse() when format.do_not_collapse_values is set to false.

ini_string_parse(my_ini_string, format);
printf(
"%s\n",
ini_string_match_ss(my_simple_string, my_ini_string, format) ?
"They match"
:
"They don't match"
);

INI strings are the strings typically dispatched by load_ini_file(), load_ini_path() or strip_ini_cache(), which may contain quotes and the three escape sequences \\, \' and \". Simple strings are user-given strings or the result of ini_string_parse().

In order to be suitable for both names and values, this function always considers sequences of one or more spaces out of quotes in the INI string as collapsed, even when format.do_not_collapse_values is set to true.

The format argument is used for the following fields:

  • format.case_sensitive
  • format.no_double_quotes
  • format.no_single_quotes
  • format.multiline_nodes (INIFORMAT_HAS_NO_ESC())
/* examples/topics/ini_string_match_si.c */
#include <stdio.h>
#include <confini.h>
static int passfinder (
IniDispatch * const disp,
void * const v_membid
) {
/* Search for `password = "hello world"` in the INI file */
if (
ini_string_match_si("password", disp->data, disp->format) &&
ini_string_match_si("hello world", disp->value, disp->format)
) {
*((size_t *) v_membid) = disp->dispatch_id;
return 1;
}
return 0;
}
int main () {
size_t membid;
/* Load INI file */
int retval = load_ini_path(
"../ini_files/self_explaining.conf",
NULL,
passfinder,
&membid
);
/* Check for errors */
if (retval & CONFINI_ERROR) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
/* Check if parsing has been interrupted by `passfinder()` */
retval == CONFINI_FEINTR ?
printf(
"We found it! It's the INI node number %zu!\n",
membid
)
:
printf("We didn't find it :-(\n");
return 0;
}
#define CONFINI_ERROR
Error mask (flags not present in user-generated interruptions)
Definition: confini.h:398
@ CONFINI_FEINTR
Definition: confini.h:409
size_t dispatch_id
Definition: confini.h:140

◆ ini_string_match_ss()

bool ini_string_match_ss ( const char *const  simple_string_a,
const char *const  simple_string_b,
const IniFormat  format 
)

Compare two simple strings and check whether they match.

Parameters
simple_string_aThe first simple string
simple_string_bThe second simple string
formatThe format of the INI file
Returns
A boolean: true if the two strings match, false otherwise

Simple strings are user-given strings or the result of ini_string_parse(). The format argument is used for the following fields:

  • format.case_sensitive

◆ ini_string_parse()

size_t ini_string_parse ( char *const  ini_string,
const IniFormat  format 
)

Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered metacharacters in respect to the format given); if the format allows it, sequences of one or more spaces out of quotes will be collapsed.

Parameters
ini_stringThe string to be unescaped
formatThe format of the INI file
Returns
The new length of the string

This function is meant to be used to parse values. In order to parse key and section names please use ini_unquote().

If you only need to compare ini_string with another string, consider to use ini_string_match_si() and ini_string_match_ii() instead of parsing the former and perform a simple comparison afterwards. These two functions are in fact able to check directly for equality between unparsed INI strings without actually modifying them.

Usually ini_string comes from an IniDispatch (but any other string may be used as well). If format.do_not_collapse_values is set to non-zero, spaces surrounding empty quotes will be collapsed together with the latter.

Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE this function is no-op and will only return the value of INI_GLOBAL_IMPLICIT_V_LEN minus the offset of ini_string within INI_GLOBAL_IMPLICIT_VALUE.

The format argument is used for the following fields:

  • format.no_single_quotes
  • format.no_double_quotes
  • format.multiline_nodes (INIFORMAT_HAS_NO_ESC())
  • format.do_not_collapse_values
/* examples/topics/ini_string_parse.c */
#include <stdio.h>
#include <confini.h>
static int ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
dispatch->type == INI_KEY || dispatch->type == INI_DISABLED_KEY
) {
ini_unquote(dispatch->data, dispatch->format);
ini_string_parse(dispatch->value, dispatch->format);
}
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
dispatch->data,
dispatch->value,
dispatch->type
);
return 0;
}
int main () {
if (
"../ini_files/self_explaining.conf",
NULL,
ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
@ INI_DISABLED_KEY
Definition: confini.h:446
size_t ini_unquote(char *const ini_string, const IniFormat format)
Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...
Definition: confini.c:4110

◆ ini_unquote()

size_t ini_unquote ( char *const  ini_string,
const IniFormat  format 
)

Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered metacharacters in respect to the format given)

Parameters
ini_stringThe string to be unescaped
formatThe format of the INI file
Returns
The new length of the string

This function is very similar to ini_string_parse(), except that does not bother collapsing the sequences of more than one space that might result from removing empty quotes. Its purpose is to be used to parse key and section names, since these are always dispatched as already collapsed. In order to parse values, or array parts listed in values, please use ini_string_parse().

If you only need to compare ini_string with another string, consider to use ini_string_match_si() and ini_string_match_ii() instead of parsing the former and perform a simple comparison afterwards. These two functions are in fact able to check directly for equality between unparsed INI strings without actually modifiyng them.

Usually ini_string comes from an IniDispatch (but any other string may be used as well). If the string does not contain quotes, or if quotes are considered to be normal characters, no changes will be made.

Note
If ini_string comes from INI_GLOBAL_IMPLICIT_VALUE this function is no-op and will only return the value of INI_GLOBAL_IMPLICIT_V_LEN minus the offset of ini_string within INI_GLOBAL_IMPLICIT_VALUE.

The format argument is used for the following fields:

/* examples/topics/ini_string_parse.c */
#include <stdio.h>
#include <confini.h>
static int ini_listener (
IniDispatch * const dispatch,
void * const v_null
) {
if (
dispatch->type == INI_KEY || dispatch->type == INI_DISABLED_KEY
) {
ini_unquote(dispatch->data, dispatch->format);
ini_string_parse(dispatch->value, dispatch->format);
}
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
dispatch->data,
dispatch->value,
dispatch->type
);
return 0;
}
int main () {
if (
"../ini_files/self_explaining.conf",
NULL,
ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}

◆ is_some_space()

static unsigned char is_some_space ( const char  chr,
const int_least8_t  depth 
)
inlinestatic

Check whether a character is a space.

Parameters
chrThe target character
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
A boolean: true if the character matches, false otherwise

◆ load_ini_file()

int load_ini_file ( FILE *const  ini_file,
const IniFormat  format,
const IniStatsHandler  f_init,
const IniDispHandler  f_foreach,
void *const  user_data 
)

Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.

Parameters
ini_fileThe FILE handle pointing to the INI file to parse
formatThe format of the INI file
f_initThe function that will be invoked before the first dispatch, or NULL
f_foreachThe function that will be invoked for each dispatch, or NULL
user_dataA custom argument, or NULL
Returns
Zero for success, otherwise an error code (see enum ConfiniInterruptNo)
Note
This function is absent if the --without-io-api option was passed to the configure script when the library was compiled

The ini_file parameter must be a FILE handle with read privileges. On some platforms, such as Microsoft Windows, it might be necessary to add the binary specifier to the mode string ("b") in order to prevent discrepancies between the physical size of the file and its computed size. Adding the binary specifier guarantees portability across all platforms:

FILE * my_file = fopen("example.conf", "rb");

For the two parameters f_init and f_foreach see function strip_ini_cache().

The parsing algorithms used by libconfini are able to parse any type of file encoded in 8-bit code units, as long as the characters that match the regular expression /[\s\[\]\.\\;#"']/ refer to the same code points they refer to in ASCII (as they do, for example, in UTF-8 and ISO-8859-1), independently of platform-specific conventions.

Note
In order to be null-byte-injection safe, NUL characters, if present in the file, will be removed from the dispatched strings.

Possible return values are: CONFINI_SUCCESS, CONFINI_IINTR, CONFINI_FEINTR, CONFINI_ENOMEM, CONFINI_EIO, CONFINI_EOOR, CONFINI_EBADF, CONFINI_EFBIG.

/* examples/topics/load_ini_file.c */
#include <stdio.h>
#include <confini.h>
static int my_callback (
IniDispatch * const dispatch,
void * const v_null
) {
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
dispatch->data, dispatch->value, dispatch->type
);
return 0;
}
int main () {
FILE * const ini_file = fopen("../ini_files/delivery.conf", "rb");
if (ini_file == NULL) {
fprintf(stderr, "File doesn't exist :-(\n");
return 1;
}
if (
ini_file,
NULL,
my_callback,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
fclose(ini_file);
return 0;
}
int load_ini_file(FILE *const ini_file, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.
Definition: confini.c:3180

◆ load_ini_path()

int load_ini_path ( const char *const  path,
const IniFormat  format,
const IniStatsHandler  f_init,
const IniDispHandler  f_foreach,
void *const  user_data 
)

Parse an INI file and dispatch its content to a custom callback using a path as argument.

Parameters
pathThe path of the INI file
formatThe format of the INI file
f_initThe function that will be invoked before the first dispatch, or NULL
f_foreachThe function that will be invoked for each dispatch, or NULL
user_dataA custom argument, or NULL
Returns
Zero for success, otherwise an error code (see enum ConfiniInterruptNo)
Note
This function is absent if the --without-io-api option was passed to the configure script when the library was compiled

For the two parameters f_init and f_foreach see function strip_ini_cache().

The parsing algorithms used by libconfini are able to parse any type of file encoded in 8-bit code units, as long as the characters that match the regular expression /[\s\[\]\.\\;#"']/ refer to the same code points they refer to in ASCII (as they do, for example, in UTF-8 and ISO-8859-1), independently of platform-specific conventions.

Note
In order to be null-byte-injection safe, NUL characters, if present in the file, will be removed from the dispatched strings.

Possible return values are: CONFINI_SUCCESS, CONFINI_IINTR, CONFINI_FEINTR, CONFINI_ENOENT, CONFINI_ENOMEM, CONFINI_EIO, CONFINI_EOOR, CONFINI_EBADF, CONFINI_EFBIG.

/* examples/topics/load_ini_path.c */
#include <stdio.h>
#include <confini.h>
static int my_callback (
IniDispatch * const dispatch,
void * const v_null
) {
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
dispatch->data, dispatch->value, dispatch->type
);
return 0;
}
int main () {
if (
"../ini_files/delivery.conf",
NULL,
my_callback,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}

◆ ltrim_h()

static size_t ltrim_h ( char *const  str,
const size_t  offs,
const int_least8_t  depth 
)
inlinestatic

Hard left trim – does change the buffer.

Parameters
strThe target string
offsThe offset where to start the left trim
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
The offset of the first non-space character

◆ ltrim_hh()

static size_t ltrim_hh ( char *const  str,
const size_t  offs,
const int_least8_t  depth 
)
inlinestatic

Shifting left trim – does change the buffer.

Parameters
strThe target string
offsThe offset where to start the left trim
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
The new length of the string

◆ ltrim_s()

static size_t ltrim_s ( const char *const  str,
const size_t  offs,
const int_least8_t  depth 
)
inlinestatic

Soft left trim – does not change the buffer.

Parameters
strThe target string
offsThe offset where to start the left trim
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
The offset of the first non-space character

◆ qultrim_h()

static size_t qultrim_h ( char *const  srcstr,
const size_t  offs,
const IniFormat  format 
)
inlinestatic

Unparsed hard left trim (left trim of /^(?:\s+|\\[\n\r]|''|"")+/) – does change the buffer.

Parameters
srcstrThe target string (it may contain multi-line escape sequences)
offsThe offset where to start the left trim
formatThe format of the INI file
Returns
The offset of the first non-trivial character

◆ rtrim_h()

static size_t rtrim_h ( char *const  str,
const size_t  len,
const int_least8_t  depth 
)
inlinestatic

Hard right trim – does change the buffer.

Parameters
strThe target string
lenThe length of the string
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
The new length of the string

◆ rtrim_s()

static size_t rtrim_s ( const char *const  str,
const size_t  len,
const int_least8_t  depth 
)
inlinestatic

Soft right trim – does not change the buffer.

Parameters
strThe target string
lenThe length of the string
depthWhat is actually considered a space (possible values: _CONFINI_WITH_EOL_, _CONFINI_NO_EOL_, _CONFINI_JUST_S_T_)
Returns
The length of the string until the last non-space character

◆ sanitize_section_path()

static size_t sanitize_section_path ( char *const  secpath,
const IniFormat  format 
)
static

Sanitize a section path.

Parameters
secpathThe section path
formatThe format of the INI file
Returns
The new length of the string

Out of quotes, similar to ECMAScript secpath.replace(/\.*\s*$|(?:\s*(\.))+\s*|^\s+/g, "$1").replace(/\s+/g, " ")

A section path can start with a dot (append), but cannot end with a dot. Spaces surrounding dots will be removed. Fragments surrounded by single or double quotes (if these are enabled) are prevented from changes.

◆ string_tolower()

static void string_tolower ( char *const  str)
inlinestatic

Convert an ASCII string to lower case.

Parameters
strThe target string
Returns
Nothing

◆ strip_ini_cache()

int strip_ini_cache ( register char *const  ini_source,
const size_t  ini_length,
const IniFormat  format,
const IniStatsHandler  f_init,
const IniDispHandler  f_foreach,
void *const  user_data 
)

Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.

Parameters
ini_sourceThe buffer containing the INI file to tokenize
ini_lengthThe length of ini_source without counting the NUL terminator (if any – se below)
formatThe format of the INI file
f_initThe function that will be invoked before the first dispatch, or NULL
f_foreachThe function that will be invoked for each dispatch, or NULL
user_dataA custom argument, or NULL
Returns
Zero for success, otherwise an error code (see enum ConfiniInterruptNo)

The ini_source parameter must be a valid pointer to a buffer of size ini_length + 1 and cannot be NULL. The ini_source string does not need to be NUL-terminated, but it does need one extra byte where to append a NUL terminator – in fact, as soon as this function is invoked, ini_source[ini_length] will be immediately set to \0.

In most cases, as when using strlen() for computing ini_length, this is not a concern, since ini_source[ini_length] will always be \0 by the very definition of strlen(), and will only get overwritten with the same value. However, if you are passing a substring of a string, for example the fragment foo=bar of the string foo=barracuda, you must expect the string to be immediately truncated into foo=bar\0acuda.

In other words, ini_source must point to a memory location where at least ini_length + 1 bytes are freely usable.

The user given function f_init (see IniStatsHandler data type) will be invoked with two arguments: statistics (a pointer to an IniStatistics structure containing some properties about the file read) and user_data (the custom argument user_data previously passed). If f_init returns a non-zero value the caller function will be interrupted.

The user given function f_foreach (see IniDispHandler data type) will be invoked with two arguments: dispatch (a pointer to an IniDispatch structure containing the parsed member of the INI file) and user_data (the custom argument user_data previously passed). If f_foreach returns a non-zero value the caller function will be interrupted.

After invoking strip_ini_cache(), the buffer pointed by the ini_source parameter must be considered as a corrupted buffer and should be freed or overwritten. For more information about this function, please refer to the Library Functions Manual.

The parsing algorithms used by libconfini are able to parse any type of file encoded in 8-bit code units, as long as the characters that match the regular expression /[\s\[\]\.\\;#"']/ refer to the same code points they refer to in ASCII (as they do, for example, in UTF-8 and ISO-8859-1), independently of platform-specific conventions.

Note
In order to be null-byte-injection-safe, before dispatching the parsed content this function strips all NUL characters possibly present in the buffer (with the exception of the last one).

Possible return values are: CONFINI_SUCCESS, CONFINI_IINTR, CONFINI_FEINTR, CONFINI_EOOR.

/* examples/topics/strip_ini_cache.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <confini.h>
static int my_callback (
IniDispatch * const dispatch,
void * const v_null
) {
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
dispatch->data, dispatch->value, dispatch->type
);
return 0;
}
int main () {
const char original_ini_buffer[] =
"[SectionOne]\n"
"\n"
"key = \"value\"\n"
"integer = 1234\n"
"real = 3.14\n"
"string1 = \"Case 1\"\n"
"string2 = 'Case 2'\n"
;
size_t ini_length = strlen(original_ini_buffer);
char * const ini_cache = strndup(original_ini_buffer, ini_length);
if (
!ini_cache || strip_ini_cache(
ini_cache,
ini_length,
NULL,
my_callback,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
printf(
"The previous dispatches come from the following buffer:\n\n"
"-------------[original INI buffer]-------------\n"
"%s"
"-----------------------------------------------\n\n",
original_ini_buffer
);
printf(
"After being processed by `strip_ini_cache()`, the buffer looks "
"like this:\n"
);
printf("\n---------------[disposed buffer]---------------\n");
for (size_t idx = 0; idx <= ini_length; idx++) {
putchar(ini_cache[idx] == 0 ? '.' : ini_cache[idx]);
}
printf("\n-----------------------------------------------\n\n");
printf(
"The dots in the example above represent NUL characters. Remember "
"that\n`strip_ini_cache()` does not free the buffer passed, you "
"will have to do that\nby yourself.\n"
);
free(ini_cache);
return 0;
}
int strip_ini_cache(register char *const ini_source, const size_t ini_length, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.
Definition: confini.c:2605

◆ uncomment()

static size_t uncomment ( char *const  srcstr,
size_t  len,
const IniFormat  format 
)
static

Remove all comment initializers (# and/or ;) from the beginning of each line of a comment.

Parameters
srcstrThe comment to parse (it may contain multi-line escape sequences)
lenThe length of srcstr
formatThe format of the INI file
Returns
The new length of the string
  • Multi-line comments: srcstr.replace(/^[#;]+|(\n\r?|\r\n?)[\t \v\f]*[#;]+/g, "$1")
  • Single-line comments: srcstr.replace(/^[#;]+/, "")

The argument srcstr may begin with a comment initializer (# or ; depending on the format), or with the character that immediately follows it.

◆ unescape_cr_lf()

static size_t unescape_cr_lf ( char *const  srcstr,
const size_t  len,
const unsigned char  is_disabled,
const IniFormat  format 
)
static

Replace /\\(\n\r?|\r\n?)[\t \v\f]*[#;]/ or /\\(\n\r?|\r\n?)/ with "$1"

Parameters
srcstrThe target string (it may contain multi-line escape sequences)
lenLength of the string
is_disabledThe string represents a disabled entry
formatThe format of the INI file
Returns
The new length of the string

◆ urtrim_s()

static size_t urtrim_s ( const char *const  str,
const size_t  len 
)
inlinestatic

Unparsed soft right trim (right trim of /(?:\s|\\[\n\r])+$/) – does not change the buffer.

Parameters
strThe target string
lenThe length of the string
Returns
The length of the string until the last non-space character

Variable Documentation

◆ INI_BOOLEANS

const char* const INI_BOOLEANS[][2]
static
Initial value:
= {
{ "no", "yes" },
{ "false", "true" },
{ "off", "on" }
}

A list of possible string representations of boolean pairs.

There may be infinite pairs here. Each pair must be organized according to the following order:

  1. Signifier of false
  2. Signifier of true
Note
Everything must be lowercase in this list.

◆ ini_get_double

double(* const ini_get_double) (const char *ini_string) ( const char *  ini_string) = atof

Pointer to atof()

◆ ini_get_float

double(* const ini_get_float) (const char *ini_string) ( const char *  ini_string) = atof

Legacy support for parsing a double data type – please do not use this function: use ini_get_double() instead.

Deprecated:
Deprecated since version 1.12.0 (it will be removed in version 2.0.0) – please use ini_get_double() instead
Parameters
ini_stringThe string to parse as a double

◆ ini_get_int

int(* const ini_get_int) (const char *ini_string) ( const char *  ini_string) = atoi

Pointer to atoi()

◆ ini_get_lint

long int(* const ini_get_lint) (const char *ini_string) ( const char *  ini_string) = atol

Pointer to atol()

◆ ini_get_llint

long long int(* const ini_get_llint) (const char *ini_string) ( const char *  ini_string) = atoll

Pointer to atoll()

◆ INI_GLOBAL_IMPLICIT_V_LEN

size_t INI_GLOBAL_IMPLICIT_V_LEN = 0

Length of the value assigned to implicit keys (default value: 0)

◆ INI_GLOBAL_IMPLICIT_VALUE

char* INI_GLOBAL_IMPLICIT_VALUE = (char *) 0

Value to be assigned to implicit keys (default value: NULL)

◆ INI_GLOBAL_LOWERCASE_MODE

bool INI_GLOBAL_LOWERCASE_MODE = 0

If set to true, key and section names in case-insensitive INI formats will be dispatched lowercase, verbatim otherwise (default value: false)

Deprecated:
Deprecated since version 1.15.0 (it will be removed in version 2.0.0)