libconfini
Yet another INI parser
|
What ./configure --without-io-api
does
Nearly everything in libconfini is implemented from scratch, with the only notable exception of the I/O functions load_ini_file()
and load_ini_path()
, which rely on C standard libraries. On some platforms, however, only a rather exotic I/O API is available, while for some other platforms the C Standard Library is simply too heavy or just not implementable.
In the past the build environment of libconfini did not offer shortcuts for facing this kind of situations – although thanks to the modularity of the source code it was still relatively simple to get rid of every tie with the C Standard Library and compile libconfini as “bare metal”, with strip_ini_cache()
as the only parsing function (since this relies only on a buffer for its input) – i.e. without load_ini_file()
and load_ini_path()
, and even without any header at all.
Starting from version 1.13.0 a “bare metal” version of libconfini has been made available by simply passing a --without-io-api
option to the configure
script.
--without-io-api
optionWhen the configure
script is launched with the --without-io-api
option, it assumes that no standard library at all could be present in the system. Hence it runs a series of tests and creates an inventory of what is present and what is not, in order to amend the source code accordingly. The amendments are necessary (instead of just relying on the C preprocessor) because it is required to change the public header, and not just the compiled code.
Fortunately only a very small amount of code in libconfini, besides the I/O functions, depends on the C Standard library, so it is relatively easy to produce a “bare metal” fork with or without it.
The dev/hackings/baremetal
subdirectory contains all the necessary amendments. These are automatically applied when launching make baremetal-csources
from the top level directory, after having launched ./configure --without-io-api
(the original source code will be preserved).
The files pp-utils.c
and number-parsers.c
constitute a re-implementation of the functions ini_get_int()
, ini_get_lint()
, ini_get_llint()
, ini_get_float()
and ini_get_double()
, which in the original code are implemented as pointers to standard functions (see below). These two files amend src/confini.c
.
The file number-parsers.h
exports the function headers of what number-parsers.c
implements, and amends src/confini.h
(i.e., the public header).
The file confini-header.c
contains only a nominal workaround-amendment to src/confini.c
(for facilitating the build system) that does not change the final C code compiled.
To produce the source code of the “bare metal” version of libconfini a fifth amendment to the public header is also required, containing some common C standard objects. This amendment is automatically generated for each platform during the build process and will be located under no-dist/hackings/baremetal/c-standard-library.h
.
Here follows the summary of what is required by make baremetal-csources
:
dev/hackings/baremetal/confini-header.c
(pasted to the private module src/confini.c
)dev/hackings/baremetal/number-parsers.c
(pasted to the private module src/confini.c
)dev/hackings/baremetal/number-parsers.h
(pasted to the public header src/confini.h
)dev/hackings/baremetal/pp-utils.c
(pasted to the private module src/confini.c
)no-dist/hackings/baremetal/c-standard-library.h
(pasted to the public header src/confini.h
after having been automaticaly generated either by the configure
script, as an exact copy of dev/hackings/baremetal/c-standard-library.h
, or by make approve-revision
, in the few cases where manual user's intervention is required during the build process)The first four files (the ones located in the dev/hackings/baremetal
subdirectory) are static and do not need any intervention from the user, unless (s)he wants to participate in the development of libconfini.
If you prefer to hack the code manually, follow these simple steps (or adjust them according to your needs):
#include <stdio.h>
from confini.h
stddef.h
header (providing the typedef
for the size_t
data type) is available, add #include <stddef.h>
to confini.h
, otherwise provide your own implementation of the size_t
data typestdint.h
header (providing the typedef
for the int8_t
, uint8_t
, uint16_t
and uint32_t
data types) is not available, remove it from confini.h
and provide your own implementation of the int8_t
, uint8_t
, uint16_t
uint32_t
data typesload_ini_file()
and load_ini_path()
from both confini.h
and confini.c
stdlib.h
header is not available, remove #include <stdlib.h>
from confini.c
and remove the function pointers ini_get_int
, ini_get_lint
, ini_get_llint
and ini_get_double
from both confini.h
and confini.c
(you will have to use your own functions for converting strings to numbers) – for a set of possible replacements as actual functions instead of function pointers, please see below)After doing so libconfini will work even without a kernel.
As explained in the previous paragraph, compiling libconfini without the C Standard Library requires getting rid of the function pointers ini_get_int()
, ini_get_lint()
, ini_get_llint()
and ini_get_double()
– as these are nothing but links to the standard functions atoi()
, atol()
, atoll()
and atof()
.
Instead of just removing the function pointers above, however, it is also possible to provide novel functions implemented from scratch for parsing numbers.
What make baremetal-csources
does in fact is replacing in src/confini.h
the function pointers declared immediately after the comment /* PUBLIC LINKS */
with the following function headers:
The same make
recipe replaces in src/confini.c
(at the end of the file) the corresponding pointers with the code below. Note that the C language does not possess a templating mechanism, so the code below needs to rely on a macro for not repeating five times the same function body with only minimal variations.
Note: The code below is released under the terms of the GPL license, version 3 or any later version.