libconfini: Library Functions Manual (original) (raw)
User Guide for libconfini
Copyright
GPL license version 3 or any later version
Description
libconfini is a simple INI parsing library with the ability to read disabled entries (i.e. valid entries nested in comments). libconfini does not store the data read from an INI file, but rather dispatches it, formatted, to a custom listener.
The code is written in C (C99) and does not depend on any particular library, except for the C standard headers stdio.h, stdlib.h, stdbool.h and stdint.h.
If you want to start learning directly from the code, you can find partially self-documented sample usages of libconfini under /usr/share/doc/libconfini/examples.
What is an INI file?
INI files were introduced with the early versions of Microsoft Windows, where the .ini file name extension stood for INItialization. An INI file can be considered as a string representation of a tree object, with new lines used as delimiters between nodes. A typical INI file is a plain text file looking like the following example:
# delivery.conf
; general options
location = Colosseum
place = Rome
[sender]
name = John Smith
email = john.smith@example.com
[receiver]
name = Mario Rossi # He's a big guy
email = mario.rossi@example.com
Supported syntaxes
During the years several interpretations of INI files appeared. In some implementations the colon character (:) was adopted as delimiter between keys and values instead of the classic equals sign (a typical example under GNU is /etc/nsswitch.conf); in other implementations, under the influence of Unix standard configuration files, a sequence of one or more spaces (/[ \t\v\f]+/ or /(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/) was adopted instead (see for example /etc/host.conf).
Equals sign used as delimiter between keys and values:
# example1.ini
home = Champ de Mars, 5 Avenue Anatole
city = Paris
Colon sign used as delimiter between keys and values:
# example2.ini
home: Champ de Mars, 5 Avenue Anatole
city: Paris
Space sequence used as delimiter between keys and values:
# example3.ini
home Champ de Mars, 5 Avenue Anatole
city Paris
libconfini was born as a general INI parser for GNU, so the support of most part of INI dialects has been implemented within it.
Especially in Microsoft Windows a more radical syntax variation found its way into INI files: the use of semicolons, instead of new lines, as delimiter between nodes, as in the following example:
# delivery.conf
; general options
location=Colosseum;place=Rome;[sender] name=John Smith;
email=john.smith@example.com;
[receiver] name=Mario Rossi; # He's a big guy
email=mario.rossi@example.com
For several reasons the use of semicolons as node delimiters is not (and will never be) supported by libconfini.
Keys
A key-value element is identified as a string placed after a new line and followed by a key-value delimiter – typically the equals sign (=) or the colon sign (:) or a space sequence (/\s+/) – which is followed by a value, which is followed by a new line or an inline comment.
Both the key part and the value part may be enclosed within quotes (' or "):
foo = 'bar'
"hello" = world
"artist" = "Pablo Picasso"
The key part can contain any character, except the delimiter (although this may be enclosed within quotes for not beeing considered as such). In multi-line formats internal new line sequences must be escaped (/\\(?:\n\r?|\r\n?)/).
If the key part part is missing libconfini considers the element of unknown type (example: = foo). If the value part is missing the key element is considered empty (example: foo =). If the delimiter is missing (and therefore the value part as well), according to some formats the key element is is considered to be an implicit key – typically representing the boolean true (example: foo). For instance, in the following example from /etc/pacman.conf, IgnorePkg is an empty key, while Color is an implicit key representing a true boolean – i.e. Color = YES:
HoldPkg = pacman glibc
Architecture = auto
IgnorePkg =
Color
SigLevel = Required DatabaseOptional
LocalFileSigLevel = Optional
The value part can contain typed data, usually: a boolean (booleans supported by libconfini are: FALSE/TRUE, NO/YES, OFF/ON – case-insensitive), a string, a number, or an array (typically with commas or spaces as delimiters between members – examples: paths = /etc, /usr, /home/john/Personal Data or paths = /etc /usr "/home/john/Personal Data"). In multi-line formats internal new line sequences must be escaped (/\\(?:\n\r?|\r\n?)/).
[my_section]
my_string = "Hello world"
'my_number' = 42
my_boolean = NO
my_implicit_boolean
my_array = Asia, Africa, 'North America', South America, \
Antarctica, Europe, Australia
Sections
A section can be imagined as a directory. A section path is identified as the string "$1" in the regular expression /(?:^|\n|\r)[ \t\v\f]*\[[ \t\v\f]*([^\]]*)[ \t\v\f]*\][ \t\v\f]*(?:\n|\r|$)/ globally applied to an INI file. A section path expresses nesting using the “dot” character, as in the following example:
[section]
foo = bar
[section.subsection]
foo = bar
A section path starting with a dot expresses nesting to the previous section. Hence, the last example is equivalent to:
[section]
foo = bar
[.subsection]
foo = bar
Keys appearing before any section path belong to a virtual root node (with an empty string as path), as the key foo in the following example:
foo = bar
[options]
interval = 3600
[host]
address = 127.0.0.1
port = 80
Section parts may be enclosed within quotes:
["world".europe.'germany'.berlin]
foo = bar
Comments
Comments are string segments enclosed within the sequence /(?:^|\s)[;#]/ and a new line sequence, as in the following example:
# this is a comment
foo = bar # this is an inline comment
; this is another comment
Comments may in theory be multi-line, following the same syntax of multi-line disabled entries (see below). This is usually of little utility, except for inline comments you want to make sure will refer to the previous entry:
play1 = The Tempest
play2 = Twelfth Night # If music be the food of love, play on; \
# Give me excess of it; that, surfeiting, \
# The appetite may sicken, and so die. \
# That strain again; it had a dying fall: \
# O, it came oer my ear, like the sweet sound \
# That breathes upon a bank of violets, \
# Stealing, and giving odour! Enough! No more.\
# 'Tis not so sweet now as it was before. \
# \
# Orsino, scene I
# This is also a masterpiece
play3 = The Merchant of Venice
Disabled entries
A disabled entry is either a section or a key that has been nested inside a comment as its only child. Inline comments cannot represent disabled entries. According to some formats disabled entries can be multi-line, using /\\(?:\n\r?|\r\n?)[\t \v\f]*[;#]/ as multi-line escape sequence. For example:
#this = is \
#a \
#multi-line \
#disabled \
#entry
Escape sequences
In order to maximize the flexibility of the data, only four escape sequences are supported by libconfini: \', \", \\ and the multi-line escape sequence (/\\(?:\n\r?|\r\n?)/).
The first three escape sequences are left untouched by all functions except [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") and [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") (see below). Nevertheless, the characters ', " and \ can determine different behaviors during the parsing depending on whether they are escaped or unescaped. For instance, the string johnsmith !" in the following example will not be parsed as a comment:
[users.jsmith]
comment = "hey! have a look at my hashtag #johnsmith !"
A particular case of escape sequence is the multi-line escape sequence (/\\(?:\n\r?|\r\n?)/), which in multi-line INI files gets immediately unescaped by libconfini.
mykey = \
this \
is \
a \
multi-line \
value
Getting started
The API is contained in one single public header:
When libconfini is used as a shared library, it might be wiser to include the versioned header (with only the major version number appended to the file name), in order to ensure that the code will compile correctly even when different major versions of the library cohabit in the same machine. This can apply also to version 1.X.X:
Reading an INI file
1. Using a pointer to a FILE handle:
FILE * ini_file,
void * user_data
)
int(* IniDispHandler)(IniDispatch *dispatch, void *user_data)
Callback function for handling an IniDispatch structure.
Definition: confini.h:162
int(* IniStatsHandler)(IniStatistics *statistics, void *user_data)
Callback function for handling an IniStatistics structure.
Definition: confini.h:153
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
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:115
2. Using a path:
const char * path,
void * user_data
)
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
Where:
ini_filein[load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.")is theFILEhandle pointing to the INI filepathin[load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.")is the path where the INI file is located (pointer to a char array, a.k.a. a "C string")formatis a bitfield that defines the syntax of the INI file (seestruct[IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)"))f_initis the function that will be invoked before any dispatching begins – it can beNULLf_foreachis the callback function that will be repeatedly invoked for each member of the INI file - it can beNULLuser_datais a pointer to a custom argument – it can beNULL
The user given function f_init (see IniStatsHandler data type) will be invoked with two arguments:
statistics– a pointer to an[IniStatistics](structIniStatistics.html "Global statistics about an INI file.")object containing some properties about the file read (like its size in bytes and the number of its members)user_data– a pointer to the custom argument previously passed to the[load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.")/[load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.")functions
The user given function f_foreach (see IniDispHandler data type) will be invoked with two arguments:
dispatch– a pointer to an[IniDispatch](structIniDispatch.html "Dispatch of a single INI node.")object containing the parsed member of the INI fileuser_data– a pointer to the custom argument previously passed to the[load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.")/[load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.")functions
Both functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") and [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") will return zero if the INI file has been completely dispatched, non-zero otherwise.
Basic examples
Example 1. Using a pointer to a FILE handle:
#include <stdio.h>
static int my_callback (
void * const v_null
) {
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
);
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;
}
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:524
Dispatch of a single INI node.
Definition: confini.h:131
char * data
Definition: confini.h:134
uint_least8_t type
Definition: confini.h:133
char * value
Definition: confini.h:135
Example 2. Using a path:
#include <stdio.h>
static int my_callback (
void * const v_null
) {
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
);
return 0;
}
int main () {
if (
"../ini_files/delivery.conf",
NULL,
my_callback,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
How it works
The function [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") is a clone of [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") that requires a path instead of a FILE handle.
Both functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") and [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") dynamically allocate at once the whole INI file into the heap, and the two structures [IniStatistics](structIniStatistics.html "Global statistics about an INI file.") and [IniDispatch](structIniDispatch.html "Dispatch of a single INI node.") into the stack. All members of the INI file are then dispatched to the custom listener f_foreach(). Finally, the allocated memory gets automatically freed.
Because of this mechanism it is very important that all the dispatched data be immediately copied by the user (when needed), and no pointers to the passed data be saved: after the end of the functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") / [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") all the allocated data will be destroyed indeed – and moreover each dispatch might overwrite data from previous dispatches.
Within a dispatching cycle, the structure containing each dispatch ([IniDispatch](structIniDispatch.html "Dispatch of a single INI node.") * dispatch) is always the same struct that gets constantly updated with new information.
Note: On some platforms, such as Microsoft Windows, it might be necessary to add the binary specifier ("b") to the mode string of the FILE handle passed to [loadinifile()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") in order to prevent discrepancies between the physical size and the computed size of the file. Adding the binary specifier guarantees portability across all platforms:
FILE * ini_file = fopen("example.conf", "rb");
Parsing a buffer instead of a file
Starting from version 1.10.0, it is possible to parse a disposable buffer containing an INI file instead of a physical file (i.e. to parse a char array). The function that allows to do so is named [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback."). This function presents some important differences compared to [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") and [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument."):
- As the name suggests, the buffer passed is not left untouched, but gets tokenized and rearranged instead – if you want to keep the original buffer you must pass a copy of it to
[strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.")– you can usestrndup()for this, or use the example below - No memory is freed after the dispatching cycle is over: getting rid of the disposable buffer is something that must be done manually
- The strings dispatched are all tokens from the buffer passed as argument (no new memory is ever allocated), but the existence of each token is granted only for a short time, that is until the dispatch of the next node (in fact the latter may overwrite previous information) – therefore, as with
[load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.")and[load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument."), every needed information must be copied immediately with each dispatch - After the dispatching cycle is over, the buffer passed as argument must be regarded as unreliable information (portions of it might have been repeatedly overwritten and corrupted by subsequent dispatches)
In short, [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.") works exactly like [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") and [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument."), but with the difference that it destroys the input while it dispatches it. And of course the input is not anymore a file, but a disposable buffer instead. As a matter of fact, [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.") is the main parsing function both [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument.") and [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") rely on in order to dispatch the content of an INI file. For a sample usage, please see examples/topics/strip_ini_cache.c.
If you want to automatize the process of making a copy of a read-only buffer, strip and parse the copy, then free the allocated memory, you can use the following function:
#include <stdio.h>
#include <string.h>
int load_ini_buffer (
const char * const ini_buffer,
const size_t ini_length,
void * const user_data
) {
char * const ini_cache = malloc(ini_length);
if (!ini_cache) {
}
memcpy(ini_cache, ini_buffer, ini_length);
ini_cache,
ini_length,
format,
f_init,
f_foreach,
user_data
);
free(ini_cache);
return retval;
}
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
@ CONFINI_ENOMEM
Definition: confini.h:412
The function above can then be invoked directly on a const buffer:
load_ini_buffer(
my_const_buffer,
strlen(my_const_buffer),
my_format,
my_stats_handler,
my_callback,
my_other_data
);
Since in most cases an INI buffer is a disposable buffer (unless one wants to parse the very same buffer more than once), libconfini's interface does not include the function in the example above.
The IniFormat data type
For a correct use of this library it is helpful to understand the [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") data type. libconfini has been born as a general INI parser, with the main purpose of being able to parse INI files written by other programs (see Rationale), therefore some flexibility was required. When an INI file is parsed it is parsed according to a particular format. The [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") data type is a univocal description of such format. It is implemented as a 24-bit bitfield. Its small size (3 bytes) allows it to be passed by value to the functions that require it.
Since no function requires a pointer to an [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") data type as argument, a preprocessor macro can be a good place where to store a custom format:
#define MY_FORMAT \
((IniFormat) { \
.delimiter_symbol = INI_EQUALS, \
.case_sensitive = false, \
.semicolon_marker = INI_IGNORE, \
.hash_marker = INI_IGNORE, \
.section_paths = INI_ABSOLUTE_AND_RELATIVE, \
.multiline_nodes = INI_MULTILINE_EVERYWHERE, \
.no_single_quotes = false, \
.no_double_quotes = false, \
.no_spaces_in_names = false, \
.implicit_is_not_empty = false, \
.do_not_collapse_values = false, \
.preserve_empty_quotes = false, \
.disabled_after_space = false, \
.disabled_can_be_implicit = false \
})
if (load_ini_path("example.conf", MY_FORMAT, NULL, my_cb, NULL)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
The model formats
A default format named INI_DEFAULT_FORMAT is available.
The code above corresponds to:
@ INI_ABSOLUTE_AND_RELATIVE
Definition: confini.h:488
@ INI_DISABLED_OR_COMMENT
Definition: confini.h:473
@ INI_MULTILINE_EVERYWHERE
Definition: confini.h:507
@ INI_EQUALS
Definition: confini.h:460
unsigned char no_single_quotes
Definition: confini.h:115
unsigned char section_paths
Definition: confini.h:115
unsigned char no_spaces_in_names
Definition: confini.h:115
unsigned char hash_marker
Definition: confini.h:115
unsigned char case_sensitive
Definition: confini.h:115
unsigned char disabled_after_space
Definition: confini.h:115
unsigned char delimiter_symbol
Definition: confini.h:115
unsigned char multiline_nodes
Definition: confini.h:115
unsigned char no_double_quotes
Definition: confini.h:115
unsigned char semicolon_marker
Definition: confini.h:115
unsigned char disabled_can_be_implicit
Definition: confini.h:115
unsigned char preserve_empty_quotes
Definition: confini.h:115
unsigned char implicit_is_not_empty
Definition: confini.h:115
unsigned char do_not_collapse_values
Definition: confini.h:115
or, equivalently, in macro form:
#define my_default_format \
((IniFormat) { \
.delimiter_symbol = INI_EQUALS, \
.case_sensitive = false, \
.semicolon_marker = INI_DISABLED_OR_COMMENT, \
.hash_marker = INI_DISABLED_OR_COMMENT, \
.section_paths = INI_ABSOLUTE_AND_RELATIVE, \
.multiline_nodes = INI_MULTILINE_EVERYWHERE, \
.no_single_quotes = false, \
.no_double_quotes = false, \
.no_spaces_in_names = false, \
.implicit_is_not_empty = false, \
.do_not_collapse_values = false, \
.preserve_empty_quotes = false, \
.disabled_after_space = false, \
.disabled_can_be_implicit = false \
})
Starting from version 1.7.0 a format named INI_UNIXLIKE_FORMAT is available.
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
This format is a clone of INI_DEFAULT_FORMAT with the only exception of [IniFormat::delimiter_symbol](structIniFormat.html#a6d9981301f5a16d73e2c38cb80f7f962), whose value is set to INI_ANY_SPACE instead of INI_EQUALS.
The semantics of the [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") bitfield has been designed to ensure that when all its fields are set to zero it equals INI_UNIXLIKE_FORMAT.
printf(
"`format_zero` and `INI_UNIXLIKE_FORMAT` are %s.\n",
"not equal"
:
"equal"
);
For further formats, please refer to the extensible list of common formats in the project's wiki. Feel free to contribute.
The IniFormatNum data type
Each format can be represented also as a univocal 24-bit unsigned integer. In order to convert an [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") to an unsigned integer and vice versa the functions [ini_fton()](confini%5F8h.html#af663493e17740c707d3b6a13ddde5b2e "Calculate the IniFormatNum of an IniFormat.") and [ini_ntof()](confini%5F8h.html#a777f4548185711a000e3d9feea8d9017 "Construct a new IniFormat according to an IniFormatNum.") are available. For simplicity, instead of using a uint_least32_t type, a size-agnostic custom type is used for this: the IniFormatNum data type.
For instance, imagine we want to create a format as close as possible to the typical Windows INI files. Probably we would define our format as follows:
.case_sensitive = false,
.no_single_quotes = false,
.no_double_quotes = false,
.no_spaces_in_names = false,
.implicit_is_not_empty = false,
.do_not_collapse_values = false,
.preserve_empty_quotes = false,
.disabled_after_space = false,
.disabled_can_be_implicit = false
};
printf("Format No. %u\n", my_format_num);
uint_least32_t IniFormatNum
The unique ID of an INI format (24-bit maximum)
Definition: confini.h:147
@ INI_ABSOLUTE_ONLY
Definition: confini.h:491
@ INI_ONLY_COMMENT
Definition: confini.h:475
@ INI_IS_NOT_A_MARKER
Definition: confini.h:479
@ INI_NO_MULTILINE
Definition: confini.h:516
IniFormatNum ini_fton(const IniFormat format)
Calculate the IniFormatNum of an IniFormat.
Definition: confini.c:5414
According to the [ini_fton()](confini%5F8h.html#af663493e17740c707d3b6a13ddde5b2e "Calculate the IniFormatNum of an IniFormat.") function this format is univocally the format No. 56637. The function [ini_ntof()](confini%5F8h.html#a777f4548185711a000e3d9feea8d9017 "Construct a new IniFormat according to an IniFormatNum.") then gives us a shortcut to construct the very same format using its format number. Hence, the code above corresponds to:
IniFormat ini_ntof(const IniFormatNum format_id)
Construct a new IniFormat according to an IniFormatNum.
Definition: confini.c:5437
The IniStatistics and IniDispatch structures
When the functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument."), [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") read an INI file, or when the function [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.") parses a buffer, they dispatch the file content to the f_foreach() listener. Before the dispatching begins some statistics about the parsed file can be dispatched to the f_init() listener (if this is non-NULL).
The information passed to f_init() is passed through an [IniStatistics](structIniStatistics.html "Global statistics about an INI file.") structure, while the information passed to f_foreach() is passed through an [IniDispatch](structIniDispatch.html "Dispatch of a single INI node.") structure.
Rendering
The output strings dispatched by libconfini follow some formatting rules depending on their role within the INI file. First, multi-line escape sequences will be unescaped, then
- Key names will be rendered according to ECMAScript
key_name.replace(/^[\n\r]\s*|\s+/g, " ")– within single or double quotes, if active, the text will be rendered verbatim. - Section paths, if format supports nesting, will be rendered according to ECMAScript
section_name.replace(/\.*\s*$|(?:\s*(\.))+\s*|^\s+/g, "$1").replace(/\s+/g, " ")– within single or double quotes, if active, the text will be rendered verbatim – otherwise, will be rendered according to the same algorithm used for key names. - Values, if
format.do_not_collapse_valuesis active, will only be cleaned of spaces at the beginning and at the end; otherwise will be rendered according to the same algorithm used for key names (with the difference that, ifformat.preserve_empty_quotesis set totrue, empty quotes surrounded by spaces will be preserved). - Comments, in multi-line formats, will be rendered according to ECMAScript
comment_string.replace(/(^|\n\r?|\r\n?)[ \t\v\f]*[#;]+/g, "$1"); elsewhere, according to ECMAScriptcomment_string.replace(/^[ \t\v\f]*[#;]+/, ""). - Unknown nodes will be rendered verbatim.
String comparisons
In order to perform comparisons between the strings dispatched the functions [ini_string_match_ss()](confini%5F8h.html#a8bb35677b6e7c201eb1fe83efee99d9a "Compare two simple strings and check whether they match."), [ini_string_match_si()](confini%5F8h.html#a5773ae8cbe4ce15737dcece84b362fa0 "Compare a simple string and an INI string and and check whether they match."), [ini_string_match_ii()](confini%5F8h.html#a731857502fa7f8e31c4a024335a6d5d6 "Compare two INI strings and check whether they match.") and [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") are available. The function [ini_string_match_ss()](confini%5F8h.html#a8bb35677b6e7c201eb1fe83efee99d9a "Compare two simple strings and check whether they match.") compares two simple strings, the function [ini_string_match_si()](confini%5F8h.html#a5773ae8cbe4ce15737dcece84b362fa0 "Compare a simple string and an INI string and and check whether they match.") compares a simple string with an unparsed INI string, the function [ini_string_match_ii()](confini%5F8h.html#a731857502fa7f8e31c4a024335a6d5d6 "Compare two INI strings and check whether they match.") compares two unparsed INI strings, and the function [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") compares two INI arrays. INI strings are the strings typically dispatched by [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument."), [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") or [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.") which may contain quotes and the three escape sequences \\, \', \". Simple strings are user-given strings or the result of [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...").
As a consequence, the functions [ini_string_match_si()](confini%5F8h.html#a5773ae8cbe4ce15737dcece84b362fa0 "Compare a simple string and an INI string and and check whether they match."), [ini_string_match_ii()](confini%5F8h.html#a731857502fa7f8e31c4a024335a6d5d6 "Compare two INI strings and check whether they match.") and [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") do not perform literal comparisons of equality between strings. For example, in the following (absurd) INI file the two keys foo and hello belong to the same section named this is a double quotation mark: "! (after being parsed by [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...")).
[this is a double quotation mark: \"!]
foo = bar
[this is a double quotation mark: '"'!]
hello = world
Therefore...
char
string1[] = "This is a double quotation mark: \\\"!",
string2[] = "This is a double quotation mark: '\"'!";
printf(
"%s\n",
"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
Or, for instance, in the following example the first two arrays are considered equal, while the third one is considered different.
#include <stdio.h>
int main () {
char
list_1[] = "foo:bar : apple : 'I said: wait!' : bye bye ",
list_2[] = "'foo':'bar':'apple':'I said: wait!':'bye' bye",
list_3[] = "foo:bar:tomorrow:apple:I said: wait!:bye bye";
printf(
"%s\n",
"They match"
:
"They don't match"
);
printf(
"%s\n",
"They match"
:
"They don't match"
);
return 0;
}
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.
Definition: confini.c:3864
In formats that support quotes, the function [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") is also the function that should be used, with '.', or INI_DOT, as delimiter (see enum IniDelimiters), to compare properly section paths containing more than one level of nesting.
if (
ini_array_match("foo.bar", disp->append_to, '.', disp->format) &&
) {
}
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
In case of multiple comparisons you might want to use a macro:
static int my_callback (IniDispatch * dsp, void * v_my_data) {
#define IS_KEY(SECTION, KEY) \
(ini_array_match(SECTION, dsp->append_to, '.', dsp->format) && \
ini_string_match_ii(KEY, dsp->data, dsp->format))
if (IS_KEY("europe.madrid", "have_visited")) {
} else if (IS_KEY("europe.london", "date")) {
} else if (...) {
}
}
#undef IS_KEY
}
@ INI_KEY
Definition: confini.h:442
The four functions [ini_string_match_ss()](confini%5F8h.html#a8bb35677b6e7c201eb1fe83efee99d9a "Compare two simple strings and check whether they match."), [ini_string_match_si()](confini%5F8h.html#a5773ae8cbe4ce15737dcece84b362fa0 "Compare a simple string and an INI string and and check whether they match."), [ini_string_match_ii()](confini%5F8h.html#a731857502fa7f8e31c4a024335a6d5d6 "Compare two INI strings and check whether they match."), [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") perform case-sensitive or case-insensitive comparisons depending on the format given. The comparison between UTF-8 codepoints out of the ASCII range is always case-sensitive.
Note that within INI strings empty quotes and spaces out of quotes are always collapsed during comparisons. Furthermore, remember that the multi-line escape sequence /\\(?:\n\r?|\r\n?)/ is not considered as such in INI strings, as this is the only escape sequence automatically unescaped by libconfini before each dispatch.
Arrays
It is possible with libconfini to parse INI values as arrays. In order to avoid that any particular character be treated as a metacharacter throughout an entire configuration file, INI arrays do not have a fixed delimiter symbol specified in the INI format.
Abstractly speaking, this means that array delimiters are not part of the INI syntax, but of the INI semantics instead. Concretely speaking, this means that developers have to provide a delimiter each time they decide to parse an INI string as an array.
Out in the wild, the most widespread INI array delimiter is probably the space sequence (INI_ANY_SPACE). Another widespread delimiter is the comma (',' or INI_COMMA). Besides these two, other characters can occasionally be found as array delimiters too (':', '|', ';', etc.).
For iterating through an INI array using a delimiter, libconfini provides the following functions:
[ini_array_get_length()](confini%5F8h.html#a312803fa24be2d7f164f3041fbfa6865 "Get the length of a stringified INI array in number of members.")[ini_array_foreach()](confini%5F8h.html#a414c3b92adf45dbb966df1606b71ccdf "Call a custom function for each member of a stringified INI array, without modifying the content of t...")[ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha...")[ini_array_break()](confini%5F8h.html#a75809c0e952d9f9e14333e085ba6b910 "Replace the first delimiter found (together with the spaces that surround it) with \0")[ini_array_release()](confini%5F8h.html#a2b34dc032fc37dda829dadf80bf34d26 "Replace the first delimiter found (together with the spaces that surround it) with \0,...")[ini_array_shift()](confini%5F8h.html#a6ca567ad863caef4609f8def7de0401a "Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the c...")[ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member.")
None of the functions above actually ever allocates any array, as libconfini does not know what kind of data type an array should be composed of – the developer might be looking for an array of strings, or an array of integers, or anything else. These tools only provide safe mechanisms to tokenize or iterate through the members of INI arrays according to the format and the delimiter given – making sure, for example, that when the delimiter symbol is found nested within quotes it is treated as a normal character, and so on – but allocating memory is something that must be done manually.
An example function that parses INI strings as newly allocated arrays of C strings is available under examples/utilities/make_strarray.h. If your program requires other data types (such as integers, booleans, etc.) you may adapt that example to your needs.
Editing the dispatched data
The strings dispatched, as already said, must not be freed. Nevertheless, before being copied or analyzed they can be edited, with some precautions:
- Be sure that your edit remains within the buffer lengths given (see
[IniDispatch::d_len](structIniDispatch.html#a34937218fa772fa66ceaa38923437791)and[IniDispatch::v_len](structIniDispatch.html#a67b4c4b61e3c801cbfbcca95073ce864)). - Do not edit the
[IniDispatch::data](structIniDispatch.html#a3821b15b23e587eca172763dd4acd954)field of a section: the[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)properties of its children will either share this buffer or will concatenate it to another buffer. If you edit its content – and depending on how you edit it – you might be no more able to rely on the[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)properties of this node's children (you would not make any damage, the loop will continue just fine – if you think you are never going to use the property[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9), just do it; alternatively, usestrndup()). - If
[IniDispatch::data](structIniDispatch.html#a3821b15b23e587eca172763dd4acd954)contains a key name or a comment, it is guaranteed that no other dispatch will share this buffer, so feel free to edit it before it gets lost. - Regarding
[IniDispatch::value](structIniDispatch.html#a8d7eb3387ff5ecb79900fae717234d16), if it does not represent an implicit value (see § Implicit keys) or if[IniFormat::implicit_is_not_empty](structIniFormat.html#ae5722efd535f1d27c624a927eea622c3)is set tofalse, this buffer is never shared between dispatches, so feel free to edit it. - The buffer pointed by
[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)is likely to be shared with other dispatches. Again, you would not destroy the world nor generate errors, but you would make the next[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)s useless. For this reason the buffer pointed by[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)is passed as constant. To unquote the path parts listed in this field please usestrndup(). - The numerical addresses of the pointers
[IniDispatch::data](structIniDispatch.html#a3821b15b23e587eca172763dd4acd954),[IniDispatch::value](structIniDispatch.html#a8d7eb3387ff5ecb79900fae717234d16)and[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)and the fields[IniDispatch::type](structIniDispatch.html#a7f6ebc6713aae413c4157ce68008a8a8),[IniDispatch::d_len](structIniDispatch.html#a34937218fa772fa66ceaa38923437791),[IniDispatch::v_len](structIniDispatch.html#a67b4c4b61e3c801cbfbcca95073ce864)and[IniDispatch::at_len](structIniDispatch.html#a12438a7f6cc343ad3202ffad82de671f), are constantly reset, so feel free to use them as custom placeholders if you like – but check their types:[IniDispatch::type](structIniDispatch.html#a7f6ebc6713aae413c4157ce68008a8a8)has only eight bits available and[IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9)will always point to aconstbuffer. - The numerical field
[IniDispatch::dispatch_id](structIniDispatch.html#a795864cff55f67498aa22722c17fc6d2)will be checked right before the next dispatch, so you should not edit it. The reading happens only as further diagnostics: if you constantly set this field to0the loop will still end at the right moment (hopefully), but if you set it instead to a value equal or higher than the[IniStatistics::members](structIniStatistics.html#ab4af1f388bd4109a9a9b1bc449146ec7)previously received, the loop will immediately stop and a CONFINI_EOOR will be thrown (seeenumConfiniInterruptNo). You can think of this as a dirty way to writereturn !0at the end of your listener. Unless you really know what you are doing, do not edit[IniDispatch::dispatch_id](structIniDispatch.html#a795864cff55f67498aa22722c17fc6d2).
Typical peaceful edits are the ones obtained by calling the functions [ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha...") and [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") directly on the buffer [IniDispatch::value](structIniDispatch.html#a8d7eb3387ff5ecb79900fae717234d16):
#include <stdio.h>
static int ini_listener (
void * const v_null
) {
if (
) {
}
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
);
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_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
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
const IniFormat format
Definition: confini.h:132
If all these rules, although thoroughly exposed, still sound confusing to you, always use strndup() on the strings dispatched and feel free to edit your own buffers as you wish. Under examples/utilities/clone_ini_dispatch.h you can find a function designed to make a hard copy of an entire [IniDispatch](structIniDispatch.html "Dispatch of a single INI node."), including all the strings that this points to.
Formatting the values
Once your listener starts to receive the parsed data you may want to format and better parse the value part of key-value elements. The following functions might be useful for this purpose:
[ini_get_bool()](confini%5F8h.html#ac43249a99e97f1d669a80c22a3e42564 "Check whether a simple string matches one of the booleans listed in the private constant INI_BOOLEANS...")[ini_get_bool_i()](confini%5F8h.html#a5d3460168636cfdffc6fb76be7c9b6b3 "Check whether an INI string matches one of the booleans listed in the private constant INI_BOOLEANS (...")[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...")[ini_array_get_length()](confini%5F8h.html#a312803fa24be2d7f164f3041fbfa6865 "Get the length of a stringified INI array in number of members.")[ini_array_foreach()](confini%5F8h.html#a414c3b92adf45dbb966df1606b71ccdf "Call a custom function for each member of a stringified INI array, without modifying the content of t...")[ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha...")[ini_array_break()](confini%5F8h.html#a75809c0e952d9f9e14333e085ba6b910 "Replace the first delimiter found (together with the spaces that surround it) with \0")[ini_array_release()](confini%5F8h.html#a2b34dc032fc37dda829dadf80bf34d26 "Replace the first delimiter found (together with the spaces that surround it) with \0,...")[ini_array_shift()](confini%5F8h.html#a6ca567ad863caef4609f8def7de0401a "Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the c...")[ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member.")
Together with the functions listed above the following links are available, in case you don't have #include <stdlib.h> in your source:
[ini_get_int()](confini%5F8h.html#adba0d5ee273d05ed845c66f01a621119 "Pointer to atoi()")= atoi()[ini_get_lint()](confini%5F8h.html#ae334387b2202eca25e16fb92694d56ca "Pointer to atol()")= atol()[ini_get_llint()](confini%5F8h.html#a9562da3c55b51795bbcbb2b19d608907 "Pointer to atoll()")= atoll()[ini_get_double()](confini%5F8h.html#ab0330bc2629009b1848293abc0de5a6c "Pointer to atof()")= atof()
Further useful resources include:
examples/utilities/clone_ini_dispatch.hexamples/utilities/make_strarray.h
Formatting the key names
The function [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") can be useful for key names enclosed within quotes. This function is very similar to [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."), except that does not bother collapsing the sequences of more than one space that might result from removing empty quotes – this is never necessary, since empty quotes surrounded by spaces in key and section names are always automatically collapsed before being dispatched.
You could use [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") as well to parse key and section names, but you would obtain the same result with a slightly bigger effort from the CPU.
Formatting the section paths
In order to retrieve the parts of a section path, the functions [ini_array_get_length()](confini%5F8h.html#a312803fa24be2d7f164f3041fbfa6865 "Get the length of a stringified INI array in number of members."), [ini_array_foreach()](confini%5F8h.html#a414c3b92adf45dbb966df1606b71ccdf "Call a custom function for each member of a stringified INI array, without modifying the content of t..."), [ini_array_break()](confini%5F8h.html#a75809c0e952d9f9e14333e085ba6b910 "Replace the first delimiter found (together with the spaces that surround it) with \0"), [ini_array_release()](confini%5F8h.html#a2b34dc032fc37dda829dadf80bf34d26 "Replace the first delimiter found (together with the spaces that surround it) with \0,..."), [ini_array_shift()](confini%5F8h.html#a6ca567ad863caef4609f8def7de0401a "Shift the location pointed by ini_strptr to the next member of the INI array (without modifying the c...") and [ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member.") can be used with '.' or INI_DOT as delimiter (see enum IniDelimiters). Note that section paths dispatched by libconfini are always collapsed arrays, therefore calling the function [ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha...") on them will have no effects.
It might be required that the function [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") be applied to each part of a section path, depending on the content and the format of the INI file.
Implicit keys
In order to set the value to assign to implicit keys (i.e. keys without a delimiter and a value), please use the [ini_global_set_implicit_value()](confini%5F8h.html#a3a6eb3b79a88304864341075240ee412 "Set the value to be to be assigned to implicit keys.") function. A true boolean is usually a good choice:
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
Alternatively, instead of [ini_global_set_implicit_value()](confini%5F8h.html#a3a6eb3b79a88304864341075240ee412 "Set the value to be to be assigned to implicit keys.") you can manually declare at the beginning of your code the two global variables INI_GLOBAL_IMPLICIT_VALUE and INI_GLOBAL_IMPLICIT_V_LEN, which will be retrieved by libconfini:
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:5764
size_t INI_GLOBAL_IMPLICIT_V_LEN
Length of the value assigned to implicit keys (default value: 0)
Definition: confini.c:5766
Or you can assign a value to them at the beginning of the main() function of your program:
If not defined elsewhere, these variables are initialized respectively to NULL and 0 by default.
Although the two variables INI_GLOBAL_IMPLICIT_VALUE and INI_GLOBAL_IMPLICIT_V_LEN are used only as placeholders for custom information accessible solely by the user, starting from version 1.14.0 it is safer to make INI_GLOBAL_IMPLICIT_V_LEN match exactly the real length of INI_GLOBAL_IMPLICIT_VALUE (without counting the NUL terminator). By doing so it is possible to make libconfini aware of a segment of memory that must be protected from writing operations.
After having set the value to be assigned to implicit key elements and having enabled [IniFormat::implicit_is_not_empty](structIniFormat.html#ae5722efd535f1d27c624a927eea622c3) in the format, it is possible to test whether a dispatched key is implicit or not by comparing the address of its value property with the global variable INI_GLOBAL_IMPLICIT_VALUE:
#include <stdio.h>
static int ini_listener (
void * const v_null
) {
printf(
"\nDATA: %s\nVALUE: %s\nNODE TYPE: %u\n"
"(This is an implicit key element)\n",
);
} else {
printf(
"\nDATA: %s\nVALUE: %s\nNODE TYPE: %u\n",
);
}
return 0;
}
int main () {
if (
"../ini_files/unix-like.conf",
my_format,
NULL,
ini_listener,
NULL
)
) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
}
Implicit keys can be parsed as booleans also without setting [IniFormat::implicit_is_not_empty](structIniFormat.html#ae5722efd535f1d27c624a927eea622c3) to true. By doing so there will be no distinction between empty and implicit keys, and there are situations where this can be a wanted behavior. The following example will parse both my_key and my_key = in the INI file as true:
bool my_boolean;
}
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
Code considerations
Return values
The functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument."), [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument."), [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback."), [ini_array_foreach()](confini%5F8h.html#a414c3b92adf45dbb966df1606b71ccdf "Call a custom function for each member of a stringified INI array, without modifying the content of t...") and [ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member.") require some listeners defined by the user. Such listeners must return an int value. When this is non-zero the caller function is interrupted, its loop stopped, and a non-zero value is returned by the caller as well.
The functions [load_ini_file()](confini%5F8h.html#a7d5042d3044dfb5a34a6bfc5af3a882d "Parse an INI file and dispatch its content to a custom callback using a FILE structure as argument."), [load_ini_path()](confini%5F8h.html#a54be108d26619db3066ce1c000dc8a17 "Parse an INI file and dispatch its content to a custom callback using a path as argument.") and [strip_ini_cache()](confini%5F8h.html#a25d648d387f6e8bc36e7965accfca63b "Parse and tokenize a buffer containing an INI file, then dispatch its content to a custom callback.") return a non-zero value also if the INI file, for any reason, has not been completely parsed (see enum ConfiniInterruptNo). Therefore, in order to be able to distinguish between internal errors and user-generated interruptions the mask CONFINI_ERROR can be used.
For instance, in the following example the f_foreach() listener returns a non-zero value if a key named password with a value that equals Hello world is found. Hence, by using the mask CONFINI_ERROR, the code below distinguishes a non-zero value generated by the listener from a non-zero value due to a parsing error.
#include <stdio.h>
static int passfinder (
void * const v_membid
) {
if (
) {
return 1;
}
return 0;
}
int main () {
size_t membid;
"../ini_files/self_explaining.conf",
NULL,
passfinder,
&membid
);
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
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
The formatting functions
The functions [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."), [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."), [ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha..."), [ini_array_break()](confini%5F8h.html#a75809c0e952d9f9e14333e085ba6b910 "Replace the first delimiter found (together with the spaces that surround it) with \0"), [ini_array_release()](confini%5F8h.html#a2b34dc032fc37dda829dadf80bf34d26 "Replace the first delimiter found (together with the spaces that surround it) with \0,...") and [ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member.") change the content of the given strings. It is important to point out that the edit is always performed within the lengths of the strings given.
The behavior of these functions depends on the format used. In particular, using [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") as a model one obtains the following scheme:
- Condition:
!format.no_single_quotes && !format.no_double_quotes && format.multiline_nodes != INI_NO_MULTILINE
⇒ Escape sequences:\\,\",\'
⇒ Behavior of[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."):\\,\'and\"will be unescaped, all unescaped single and double quotes will be removed, then the new length of the string will be returned. - Condition:
!format.no_single_quotes && format.no_double_quotes && format.multiline_nodes != INI_NO_MULTILINE
⇒ Escape sequences:\\,\'
⇒ Behavior of[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."):\\and\'will be unescaped, all unescaped single quotes will be removed, then the new length of the string will be returned. - Condition:
format.no_single_quotes && !format.no_double_quotes && format.multiline_nodes != INI_NO_MULTILINE
⇒ Escape sequences:\\,\"
⇒ Behavior of[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."):\\and\"will be unescaped, all unescaped double quotes will be removed, then the new length of the string will be returned. - Condition:
format.no_single_quotes && format.no_double_quotes && format.multiline_nodes != INI_NO_MULTILINE
⇒ Escape sequences:\\
⇒ Behavior of[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."): only\\will be unescaped, spaces at the beginning and at the end of the string will be removed, then the new length of the string will be returned. - Condition:
format.no_single_quotes && format.no_double_quotes && format.multiline_nodes == INI_NO_MULTILINE
⇒ Escape sequences: No escape sequences
⇒ Behavior of[ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."): Spaces at the beginning and at the end of the string will be removed, then the new length of the string will be returned.
A function-like macro named INIFORMAT_HAS_NO_ESC() is available for checking whether a format supports escape sequences or not.
It is possible to extend the list of supported escape sequences by parsing additional ones before invoking [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."). Under examples/utilities/ini_string_preparse.h there is a little helper function that adds support to the following sequences: "\a", "\b", "\f", "\n", "\r", "\t", "\v" and "\e". That function must be used right before [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") or [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met..."), as it relies on the latter for unescaping double backslashes:
#include "examples/utilities/ini_string_preparse.h"
static int my_callback (IniDispatch * disp, void * user_data) {
ini_string_preparse(disp->value);
}
return 0;
}
Common mistakes
The native formatting functions [ini_array_break()](confini%5F8h.html#a75809c0e952d9f9e14333e085ba6b910 "Replace the first delimiter found (together with the spaces that surround it) with \0"), [ini_array_collapse()](confini%5F8h.html#aa0721fcae5f04e5a9edd485dc1ddd2fa "Compress the distribution of the data in a stringified INI array by removing all the white spaces tha..."), [ini_array_release()](confini%5F8h.html#a2b34dc032fc37dda829dadf80bf34d26 "Replace the first delimiter found (together with the spaces that surround it) with \0,..."), [ini_array_split()](confini%5F8h.html#a04acd6a01a6a78810e7aab8e02923218 "Split a stringified INI array into NUL-separated members and call a custom function for each member."), [ini_string_parse()](confini%5F8h.html#ab4e3d48f0eb0438ba0c84fc77e362aa3 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") and [ini_unquote()](confini%5F8h.html#ac55f3fab95f697ac5058533df100fc11 "Unescape \', \", and \\ and remove all unescaped quotes (when single/double quotes are considered met...") have a special safeguard against attempting to edit the global variable INI_GLOBAL_IMPLICIT_VALUE. But if you use your own functions for editing the dispatches received, you must always make sure that dispatch->value != INI_GLOBAL_IMPLICIT_VALUE in formats where [IniFormat::implicit_is_not_empty](structIniFormat.html#ae5722efd535f1d27c624a927eea622c3) is set to true – otherwise a “Segmentation fault (core dumped)” error can be generated.
#include <stdio.h>
static void my_editing_function (char * const my_string) {
if (!my_string) {
return;
}
my_string[0] = '\0';
}
static int my_callback (IniDispatch * dispatch, void * v_null) {
my_editing_function(dispatch->value);
printf(
"DATA: %s\nVALUE: %s\nNODE TYPE: %u\n\n",
);
return 0;
}
int main () {
"examples/ini_files/typed_ini.conf",
myformat,
NULL,
my_callback,
NULL
)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
To check whether a particular substring belongs to INI_GLOBAL_IMPLICIT_VALUE, the [INI_IS_IMPLICIT_SUBSTR()](confini%5F8h.html#a22185e778bfd2a35407a4915dc56e02e "Check whether a given char * data type points to the global variable INI_GLOBAL_IMPLICIT_VALUE or to ...") macro can be used:
if (dispatch->v_len > 0) {
char * my_substring = dispatch->value + 1;
printf("%s\n", my_substring);
printf("\n\tYou tricky one! This was an implicit value too!\n");
}
return 0;
#define INI_IS_IMPLICIT_SUBSTR(CHAR_PTR)
Check whether a given char * data type points to the global variable INI_GLOBAL_IMPLICIT_VALUE or to ...
Definition: confini.h:102
size_t v_len
Definition: confini.h:138
Storing the dispatched data
In order to be as flexible as possible, libconfini does not store the dispatched data, nor indicizes them. This gives developers the power to deal with them in many different ways.
For small INI files a normal if/else chain, using [ini_array_match()](confini%5F8h.html#a76a050b341e311cc9bbb79b399691e12 "Compare two INI arrays and check whether they match.") for comparing section paths and [ini_string_match_si()](confini%5F8h.html#a5773ae8cbe4ce15737dcece84b362fa0 "Compare a simple string and an INI string and and check whether they match.")/[ini_string_match_ii()](confini%5F8h.html#a731857502fa7f8e31c4a024335a6d5d6 "Compare two INI strings and check whether they match.") for comparing key names, usually represents the most practical and efficient way to obtain the information required from an INI file. Sometimes however, especially in case of sizeable INI files, the most efficient solution would be to store the parsed data in a hash table before trying to access it.
Some INI parsers are released with a hash table API included by default. This is an unpractical solution, since fantastic free software libraries that focus solely on hash tables already exist, and providing a further API for managing a hash function together with an INI parser only complicates the code, makes it harder to maintain, and does not give the developer the real freedom to choose what suits best to each single case. Some programming languages even have hash tables in their standard libraries (see std::map in C++ for example).
When needed, the data parsed by libconfini can be stored in a hash table while it is being dispatched. If you are interested in combining libconfini with a hash table, we have left a general example of how to use GLib's GHashTable together with libconfini under examples/miscellanea/glib_hash_table.c. If you are using C++, you can find an example of how to construct a C++ class that relies on a std::unordered_map object under examples/cplusplus/map.cpp. By keeping these examples as models other solutions can be easily explored as well.
Size of the dispatched data
Writing
char * buf = malloc(stats->bytes + stats->members + (
stats->format.implicit_is_not_empty ?
:
0
) + 1);
will always allocate the smallest buffer large enough to store all the [IniDispatch::data](structIniDispatch.html#a3821b15b23e587eca172763dd4acd954) and [IniDispatch::value](structIniDispatch.html#a8d7eb3387ff5ecb79900fae717234d16) received, including their NUL terminators and any possible implicit value.
This buffer will not be capable enough to store also the [IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9) strings received (about which no information is available at early stages). However, thanks to the syntax of INI files, where a key is always appended to the section previously dispatched, it will be always possible to store the information about the structure of the tree by using numbers as references to the parents instead of copying verbatim the [IniDispatch::append_to](structIniDispatch.html#ae3aca4d49e430c9180b4f80b3f0624d9) strings. This will also result in a more efficient code.
#include <stdio.h>
#include <stdlib.h>
static int my_stats_listener (
void * v_destptr
) {
#define destptr ((char **) v_destptr)
*destptr = malloc(stats->bytes + stats->members + (
:
0
) + 1);
return 0;
#undef destptr
}
static int my_ini_listener (
void * v_my_capable_buffer
) {
char ** my_capable_buffer = (char **) v_my_capable_buffer;
return 0;
}
int main () {
char * my_copied_data;
"test.conf",
my_stats_listener,
my_ini_listener,
&my_copied_data
)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
Global statistics about an INI file.
Definition: confini.h:121
const IniFormat format
Definition: confini.h:122
const size_t members
Definition: confini.h:124
const size_t bytes
Definition: confini.h:123
Other global settings
In the past, besides the two global variables INI_GLOBAL_IMPLICIT_VALUE and INI_GLOBAL_IMPLICIT_V_LEN, a third variable named INI_GLOBAL_LOWERCASE_MODE would tell libconfini whether to dispatch in lower case all key names and section paths of case-insensitive INI files. This variable rarely needed to be set to true, since string comparisons made by libconfini are always either case-sensitive or case-insensitive depending on the format given.
This “dispatch in lowercase” functionality itself is a relic from a time when libconfini did not yet possess any string comparison functions. This is no longer the case, and there are now four functions for that; therefore starting from version 1.15.0 both the INI_GLOBAL_IMPLICIT_VALUE variable and the [ini_global_set_lowercase_mode()](confini%5F8h.html#a2d22fde4b4093871bcc498c2bddc7fbe "Set the value of the global variable INI_GLOBAL_LOWERCASE_MODE.") function have been marked as deprecated (see the list of deprecated functions and variables). If needed, to convert to lowercase key names and section paths in case-insensitive INI files it is possible to use the following simple snippet, which maps exactly what libconfini would do:
for (register char * ptr = str; *ptr; ptr++) {
*ptr = *ptr > 0x40 && *ptr < 0x5b ? *ptr | 0x60 : *ptr;
}
}
static int my_callback (IniDispatch * dispatch, void * v_null) {
switch (dispatch->type) {
}
}
return 0;
}
static void string_tolower(char *const str)
Convert an ASCII string to lower case.
Definition: confini.c:788
@ INI_DISABLED_SECTION
Definition: confini.h:447
@ INI_SECTION
Definition: confini.h:443
The unicode problem
Comparing an ASCII upper case letter to an ASCII lower case letter is an invariant process. But comparing two Unicode letter cases is a process that depends on the locale of the machine. Consider for example the lower case letter i: in most European languages its upper case is I, while this is not the case in Turkish, where the upper case of i is İ (and the lower case of I is ı). Therefore for a person living in Italy or France, i and I will represent the same letter, while for a person living in Turkey they will not.
Key and section names of an INI file however cannot depend on the locale of the machine, since they must be reliably searched for independently of where a machine is located. Imagine for example a key named “INI” and imagine that Unicode case folding were performed on key names during string comparisons. If you lived in Europe you could look up for such key using its lower case “ini”, while if you lived in Turkey you would have to use the lower case “ını” to find it. So the only solution is to consider Unicode characters out of the ASCII range always as case-sensitive. For this reason, libconfini (and probably any judicious INI parser) will never perform case folding of Unicode characters out of the ASCII range within key and section names.
It must be said that most Unicode characters do not possess a lower and upper case, and most characters outside of the ASCII range could theoretically appear without problems in key and section names also in case-insensitive INI files (think of the character § for example). And, as for case-sensitive INI files, no Unicode character would ever represent a problem. Nonetheless, it is generally more acceptable to use ASCII only within key and section names – and possibly, if needed, non-ASCII Unicode characters within values and comments.
That said, libconfini deals perfectly fine with UTF-8 (but is always case-sensitive outside of the ASCII range), so use the latter as you feel appropriate.
Thread safety
Depending on the format of the INI file, libconfini may use up to three global variables (INI_GLOBAL_IMPLICIT_VALUE, INI_GLOBAL_IMPLICIT_V_LEN and INI_GLOBAL_LOWERCASE_MODE). In order to be thread-safe these three variables (if needed) must be defined only once (either directly, or by using their setter functions [ini_global_set_implicit_value()](confini%5F8h.html#a3a6eb3b79a88304864341075240ee412 "Set the value to be to be assigned to implicit keys.") and [ini_global_set_lowercase_mode()](confini%5F8h.html#a2d22fde4b4093871bcc498c2bddc7fbe "Set the value of the global variable INI_GLOBAL_LOWERCASE_MODE.")), or otherwise a mutex logic must be introduced.
Apart from the three variables above, each parsing allocates and frees its own memory and every function is fully reentrant, therefore the library can be considered thread-safe.
Error exceptions
The philosophy of libconfini is that of parsing as much as possible without generating error exceptions. No parsing errors are returned once an INI file has been correctly allocated into the heap, with the exception of the out-of-range error CONFINI_EOOR (see enum ConfiniInterruptNo), whose meaning is that the dispatches are for unknown reasons more than expected – this error is possibly generated by the presence of bugs in the library's code and should never be returned (please contact me if this happens).
When an INI node is wrongly written in respect to the format given, it is dispatched verbatim as an INI_UNKNOWN node – see enum IniNodeType. Empty lines, or lines containing only spaces and empty quotes (if the latter are supported) will be skipped.
In order to avoid error exceptions, strings containing an unterminated quote will always be treated as if they had a virtual quote as their last + 1 character. For example,
will always determine the same behavior as if it were
Any format containing the following three settings will never produce INI_UNKNOWN nodes, even if instead of an INI file we tried to parse a .jpeg image (or anything else):
.no_spaces_in_names = false,
...
};
@ INI_NO_SECTIONS
Definition: confini.h:497
@ INI_ANY_SPACE
Definition: confini.h:457
Performance
The algorithms used by libconfini stand in a delicate equilibrium between flexibility, speed and code readability, with flexibility as primary target. Performance can vary with the format used to parse an INI file, but in most cases is not a concern.
One can measure the performance of the library by doing something like:
#include <stdio.h>
#include <time.h>
static int get_ini_size (IniStatistics * stats, void * v_bytes) {
*((size_t *) v_bytes) = stats->bytes;
return 0;
}
static int empty_listener (IniDispatch * dispatch, void * v_bytes) {
return 0;
}
int main () {
size_t bytes;
double seconds;
clock_t start, end;
start = clock();
"big_file.ini",
get_ini_size,
empty_listener,
&bytes
)) {
return 1;
}
end = clock();
seconds = (double) (end - start) / CLOCKS_PER_SEC;
printf(
"%zu bytes parsed in %f seconds.\n"
"Number of bytes parsed per second: %f\n",
bytes, seconds, (double) bytes / seconds
);
return 0;
}
By changing the format of the INI file on the code above one might obtain different results. In particular, switching disabled entry recognition off – by setting [IniFormat::semicolon_marker](structIniFormat.html#aab0c2bf1e89593bbbfc9ac5fc5f6f552) and [IniFormat::hash_marker](structIniFormat.html#a4d9984334335612830eef223e0adc913) to INI_ONLY_COMMENT or INI_IGNORE – and making the format non-multi-line – by setting [IniFormat::multiline_nodes](structIniFormat.html#a8a4a1d116d66bad0c61d9876bf9f87ee) to INI_NO_MULTILINE – will have a positive impact on the performance.
On my laptop libconfini seems to parse around 95 MiB per second using the model format INI_DEFAULT_FORMAT. Whether this is enough for you or not, it depends only on your needs.
53411840 bytes (1146880 nodes) parsed in 0.533315 seconds.
Number of bytes parsed per second: 100150642.678342
If you are interested in testing yourself the library's performance on a particular hardware, you can find a performance test under dev/tests/performance.
INI syntax considerations
Comment or disabled entry?
It is hard to imagine a reason to be interested in disabled entries if not for writing a GUI editor for INI files. If this is the case and you are not using libconfini like normal people do, you might wonder how to ensure that disabled entries and comments be always parsed without ambiguity.
In most of the cases libconfini is smart enough to distinguish a disabled entry from a comment. However some INI files can be tricky and might require some workarounds. For instance, imagine the following INI file:
# INI KEY/VALUE DELIMITER: `=`
[some_section]
hello = world
;foo = bar
##now=Sunday April 3rd, 2016 {#now-sunday-april-3rd}
And imagine that for unknown reasons the author of the INI file wanted only ;foo = bar to be considered as a disabled entry, and the first and last line as normal comments.
If we tried to parse it according to the format used below
#include <stdio.h>
static int ini_listener (IniDispatch * dispatch, void * v_null) {
printf(
"Node #%zu - Type: %u; Data: \"%s\"; Value: \"%s\"\n",
);
return 0;
}
int main () {
#define MY_FORMAT \
((IniFormat) { \
.delimiter_symbol = INI_EQUALS, \
.case_sensitive = false, \
.semicolon_marker = INI_DISABLED_OR_COMMENT, \
.hash_marker = INI_DISABLED_OR_COMMENT, \
.multiline_nodes = INI_NO_MULTILINE, \
.section_paths = INI_ABSOLUTE_ONLY, \
.no_single_quotes = false, \
.no_double_quotes = false, \
.no_spaces_in_names = false, \
.implicit_is_not_empty = true, \
.do_not_collapse_values = false, \
.preserve_empty_quotes = false, \
.disabled_after_space = true, \
.disabled_can_be_implicit = true \
})
printf(":: Content of \"ambiguous.conf\" ::\n\n");
"../ini_files/ambiguous.conf",
MY_FORMAT,
NULL,
ini_listener,
NULL
)) {
fprintf(stderr, "Sorry, something went wrong :-(\n");
return 1;
}
return 0;
}
we would obtain the following result:
:: Content of "ambiguous.conf" ::
Node #0 - Type: 6; Data: "INI KEY/VALUE DELIMITER: `"; Value: "`"
Node #1 - Type: 3; Data: "some_section"; Value: ""
Node #2 - Type: 2; Data: "hello"; Value: "world"
Node #3 - Type: 6; Data: "foo"; Value: "bar"
Node #4 - Type: 4; Data: "now=Sunday April 3rd, 2016"; Value: ""
As you can see, all comments but now=Sunday April 3rd, 2016 would be parsed as disabled entries – which is not what the author intended. Therefore, to ensure that such INI file be parsed properly, you can follow two possible approaches.
1. Intervene on the INI file. The reason why now=Sunday April 3rd, 2016 has been properly parsed as a comment – despite it really looks like a disabled entry – is because it has been nested within a comment block opened by more than one leading marker (in this case the two ##). As a general rule, libconfini never parses a comment beginning with more than one leading marker as a disabled entry, therefore this is the surest way to ensure that proper comments are always considered as such.
Hence, by adding one more number sign to the first comment
## INI KEY/VALUE DELIMITER: `=`
[some_section]
hello = world
;foo = bar
##now=Sunday April 3rd, 2016 {#now-sunday-april-3rd}
we obtain the wanted result:
:: Content of "ambiguous.conf" ::
Node #0 - Type: 4; Data: " INI KEY/VALUE DELIMITER: `=`"; Value: ""
Node #1 - Type: 3; Data: "some_section"; Value: ""
Node #2 - Type: 2; Data: "hello"; Value: "world"
Node #3 - Type: 6; Data: "foo"; Value: "bar"
Node #4 - Type: 4; Data: "now=Sunday April 3rd, 2016"; Value: ""
2. Intervene on the format. There are cases where the INI file is automatically generated by machines (comments included), or distributed as such, and human intervention would be required on each machine-generated release of the INI file. In these cases – and if we are sure about the expected content of the INI file – we can restrict the format chosen in order to parse comments and disabled entries properly. In particular, the following fields of the [IniFormat](structIniFormat.html "24-bit bitfield representing the format of an INI file (INI dialect)") bitfield can have an impact on the disambiguation between comments and disabled entries.
Reliable general patterns:
[IniFormat::semicolon_marker](structIniFormat.html#aab0c2bf1e89593bbbfc9ac5fc5f6f552)and[IniFormat::hash_marker](structIniFormat.html#a4d9984334335612830eef223e0adc913)– The imaginary author of our INI file, if we observe the latter closer, chose the semicolon symbol as the marker of disabled entries and the hash symbol as the marker of comments. We may exploit this difference and set ourmy_format.semicolon_markerto INI_DISABLED_OR_COMMENT and ourmy_format.hash_markerto INI_ONLY_COMMENT to obtain the correct disambiguation. If you believe that this solution is too artificial, think that/etc/samba/smb.confand/etc/pulse/daemon.confare systematically distributed using this pattern.[IniFormat::disabled_after_space](structIniFormat.html#a6ad00ea76bbe5ed109f4192f89859656)– Setting this property tofalse, due to the initial space that follows the comment marker (# INI...), forces the entire line to be considered as a comment. Some authors use this syntax to distinguish between comments and disabled entries (examples are/etc/pacman.confand/etc/bluetooth/main.conf)
Temporary workarounds:
[IniFormat::no_spaces_in_names](structIniFormat.html#a491e94bda3f8545471a86f0845c92474)– If our INI file has only comments containing more than one word and we are sure that key and section names cannot contain internal white spaces, we can set this property totrueto enhance disambiguation.[IniFormat::disabled_can_be_implicit](structIniFormat.html#ab5937aaf72b0ce5b62aece8d23ad7b82)– This property, if set tofalse, forces all comments that do not contain a key-value delimiter never to be considered as disabled entries. Despite not having an impact on our example, it has an impact on the disambiguation algorithms used by libconfini. Its value in INI_DEFAULT_FORMAT is set tofalse.
As a general rule, libconfini will always try to parse as a disabled entry whatever comment is allowed (by the format) to contain one. Only if this attempt fails, the block will be dispatched as a normal comment.