• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

res/H27-Oct-2019-

src/H27-Oct-2019-

.gitignoreH A D27-Oct-20198

license.mdH A D27-Oct-2019483

makefileH A D27-Oct-2019835

readme.mdH A D27-Oct-20192.5 KiB

readme.md

1# Configator
2Configator is a lightweight library for ini config file parsing.
3This was created to make it easy following the "DRY" coding rule
4without using macros, and with flexibility in mind.
5
6It integrates very well with the [Argoat](https://github.com/cylgom/argoat.git)
7arguments parser library, by using the same function pointers format.
8This way, you can easily load settings from an ini file while overloading them
9with command-line arguments if needed: the handling functions will be the same.
10
11Configator does not use any macro or dynamic memory allocation,
12and was built in less than 350 lines of C99 code.
13
14## Testing
15Run `make` to compile an example executable and perform basic testing
16
17## Using
18### TL;DR
19Please see `example.c` for the condensed version
20(or better, read the actual documentation below).
21It is a bit too long to be copied here twice...
22
23### Details
24Include `argoat.h` and compile `argoat.c` with your code.
25
26Write the functions that will handle your parameters.
27They will be called during the parsing process, in the order given by the user
28```
29void handle_config_u8(void* data, char** value, const int pars_count)
30{
31	if (pars_count > 0)
32	{
33		*((uint8_t*) data) = atoi(*value);
34	}
35}
36```
37
38In your `main`, declare the variables to configure.
39They will be passed to the corresponding functions as `void* data`
40```
41	uint8_t answer = 0;
42```
43
44Declare the arrays of parameters by section, starting with the general section.
45If you don't want to handle parameters in some section, just declare it `NULL`.
46```
47struct configator_param* map_no_section = NULL;
48```
49
50Declare real sections parameters afterwards
51```
52struct configator_param map_test_section[] =
53{
54	{"ping", &answer, handle_config_u8},
55	{"pong", &answer, handle_config_u8},
56};
57```
58
59Then group them in the map
60```
61struct configator_param* map[] =
62{
63	map_no_section,
64	map_test_section
65};
66```
67
68And declare the sections array. Configator will execute the pointed function
69with `NULL` arguments at the beginning of each detected section.
70You can also declare sections with `NULL` parameters, in which case nothing
71will be executed.
72```
73struct configator_param sections[] =
74{
75	{"network_test", &answer, handle_config_u8},
76};
77```
78
79Don't forget to put the right numbers in the lenght variables
80```
81uint16_t map_len[] = {0, 2};
82uint16_t sections_len = 1;
83```
84
85Then initialize and use configator
86```
87struct configator config;
88config.map = map;
89config.map_len = map_len;
90config.sections = sections;
91config.sections_len = sections_len;
92
93configator(&config, "config.ini");
94printf("answer = %d\n", answer);
95```
96