libconfini
Yet another INI parser
confini.h
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
2 
15 #ifndef _LIBCONFINI_HEADER_
16 #define _LIBCONFINI_HEADER_
17 
18 
19 
20 #include <stdio.h>
21 #include <stdint.h>
22 
23 
24 
25 /* PRIVATE (HEADER-SCOPED) MACROS */
26 
27 
28 #define _LIBCONFINI_DEFAULT_FMT_FIELDS_(NAME, OFFSET, SIZE, DEFVAL) DEFVAL,
29 #define _LIBCONFINI_FMT_ZERO_FIELDS_(NAME, OFFSET, SIZE, DEFVAL) 0,
30 #define _LIBCONFINI_INIFORMAT_DECLS_(NAME, OFFSET, SIZE, DEFVAL) \
31  unsigned char NAME:SIZE;
32 #define _LIBCONFINI_INIFORMAT_STRUCT_ \
33  struct IniFormat { INIFORMAT_TABLE_AS(_LIBCONFINI_INIFORMAT_DECLS_) }
34 #define _LIBCONFINI_DEFAULT_FORMAT_ \
35  { INIFORMAT_TABLE_AS(_LIBCONFINI_DEFAULT_FMT_FIELDS_) }
36 #define _LIBCONFINI_FORMAT_ZERO_ \
37  { INIFORMAT_TABLE_AS(_LIBCONFINI_FMT_ZERO_FIELDS_) }
38 
39 
40 
41 /* PUBLIC MACROS */
42 
43 
47 /*
48  NOTE: The following table and the order of its rows **define** (and link
49  together) both the #IniFormat and #IniFormatNum data types declared in this
50  header
51 */
52 #define INIFORMAT_TABLE_AS(_____) /* IniFormat table *\
53 
54  NAME BIT SIZE DEFAULT
55  */\
56 _____( delimiter_symbol, 0, 7, INI_EQUALS ) \
57 _____( case_sensitive, 7, 1, 0 )/*
58  */\
59 _____( semicolon_marker, 8, 2, INI_DISABLED_OR_COMMENT ) \
60 _____( hash_marker, 10, 2, INI_DISABLED_OR_COMMENT ) \
61 _____( section_paths, 12, 2, INI_ABSOLUTE_AND_RELATIVE ) \
62 _____( multiline_nodes, 14, 2, INI_MULTILINE_EVERYWHERE )/*
63  */\
64 _____( no_single_quotes, 16, 1, 0 ) \
65 _____( no_double_quotes, 17, 1, 0 ) \
66 _____( no_spaces_in_names, 18, 1, 0 ) \
67 _____( implicit_is_not_empty, 19, 1, 0 ) \
68 _____( do_not_collapse_values, 20, 1, 0 ) \
69 _____( preserve_empty_quotes, 21, 1, 0 ) \
70 _____( disabled_after_space, 22, 1, 0 ) \
71 _____( disabled_can_be_implicit, 23, 1, 0 )
72 
73 
74 
78 #define INIFORMAT_HAS_NO_ESC(FORMAT) \
79  (FORMAT.multiline_nodes == INI_NO_MULTILINE && \
80  FORMAT.no_double_quotes && FORMAT.no_single_quotes)
81 
82 
83 
84 /* PUBLIC TYPEDEFS */
85 
86 
90 typedef _LIBCONFINI_INIFORMAT_STRUCT_ IniFormat;
91 
92 
96 typedef struct IniStatistics {
98  const size_t bytes;
99  const size_t members;
100 } IniStatistics;
101 
102 
106 typedef struct IniDispatch {
108  uint8_t type;
109  char * data;
110  char * value;
111  const char * append_to;
112  size_t d_len;
113  size_t v_len;
114  size_t at_len;
115  size_t dispatch_id;
116 } IniDispatch;
117 
118 
122 typedef uint32_t IniFormatNum;
123 
124 
128 typedef int (* IniStatsHandler) (
129  IniStatistics * statistics,
130  void * user_data
131 );
132 
133 
137 typedef int (* IniDispHandler) (
138  IniDispatch * dispatch,
139  void * user_data
140 );
141 
142 
146 typedef int (* IniStrHandler) (
147  char * ini_string,
148  size_t string_length,
149  size_t string_num,
150  IniFormat format,
151  void * user_data
152 );
153 
154 
158 typedef int (* IniSubstrHandler) (
159  const char * ini_string,
160  size_t fragm_offset,
161  size_t fragm_length,
162  size_t fragm_num,
163  IniFormat format,
164  void * user_data
165 );
166 
167 
168 
169 /* PUBLIC FUNCTIONS */
170 
171 
172 extern int load_ini_file (
173  FILE * const ini_file,
174  const IniFormat format,
175  const IniStatsHandler f_init,
176  const IniDispHandler f_foreach,
177  void * const user_data
178 );
179 
180 
181 extern int load_ini_path (
182  const char * const path,
183  const IniFormat format,
184  const IniStatsHandler f_init,
185  const IniDispHandler f_foreach,
186  void * const user_data
187 );
188 
189 
190 extern _Bool ini_string_match_ss (
191  const char * const simple_string_a,
192  const char * const simple_string_b,
193  const IniFormat format
194 );
195 
196 
197 extern _Bool ini_string_match_si (
198  const char * const simple_string,
199  const char * const ini_string,
200  const IniFormat format
201 );
202 
203 
204 extern _Bool ini_string_match_ii (
205  const char * const ini_string_a,
206  const char * const ini_string_b,
207  const IniFormat format
208 );
209 
210 
211 extern _Bool ini_array_match (
212  const char * const ini_string_a,
213  const char * const ini_string_b,
214  const char delimiter,
215  const IniFormat format
216 );
217 
218 
219 extern size_t ini_unquote (
220  char * const ini_string,
221  const IniFormat format
222 );
223 
224 
225 extern size_t ini_string_parse (
226  char * const ini_string,
227  const IniFormat format
228 );
229 
230 
231 extern size_t ini_array_get_length (
232  const char * const ini_string,
233  const char delimiter,
234  const IniFormat format
235 );
236 
237 
238 extern int ini_array_foreach (
239  const char * const ini_string,
240  const char delimiter,
241  const IniFormat format,
242  const IniSubstrHandler f_foreach,
243  void * const user_data
244 );
245 
246 
247 extern size_t ini_array_shift (
248  const char ** const ini_strptr,
249  const char delimiter,
250  const IniFormat format
251 );
252 
253 
254 extern size_t ini_array_collapse (
255  char * const ini_string,
256  const char delimiter,
257  const IniFormat format
258 );
259 
260 
261 extern char * ini_array_break (
262  char * const ini_string,
263  const char delimiter,
264  const IniFormat format
265 );
266 
267 
268 extern char * ini_array_release (
269  char ** ini_strptr,
270  const char delimiter,
271  const IniFormat format
272 );
273 
274 
275 extern int ini_array_split (
276  char * const ini_string,
277  const char delimiter,
278  const IniFormat format,
279  const IniStrHandler f_foreach,
280  void * const user_data
281 );
282 
283 
284 extern void ini_global_set_lowercase_mode (
285  _Bool lowercase
286 );
287 
288 
289 extern void ini_global_set_implicit_value (
290  char * const implicit_value,
291  const size_t implicit_v_len
292 );
293 
294 
295 extern IniFormatNum ini_fton (
296  const IniFormat format
297 );
298 
299 
300 extern IniFormat ini_ntof (
301  IniFormatNum format_id
302 );
303 
304 
305 extern int ini_get_bool (
306  const char * const ini_string,
307  const int return_value
308 );
309 
310 
311 
312 /* PUBLIC LINKS */
313 
314 
315 extern int (* const ini_get_int) (
316  const char * ini_string
317 );
318 
319 
320 extern long int (* const ini_get_lint) (
321  const char * ini_string
322 );
323 
324 
325 extern long long int (* const ini_get_llint) (
326  const char * ini_string
327 );
328 
329 
330 extern double (* const ini_get_float) (
331  const char * ini_string
332 );
333 
334 
335 
336 /* PUBLIC CONSTANTS AND VARIABLES */
337 
338 
342 #define CONFINI_ERROR 252
343 
344 
360 };
361 
362 
368  INI_VALUE = 1,
371  INI_KEY = 2,
377 };
378 
379 
388  INI_EQUALS = '=',
389  INI_COLON = ':',
390  INI_DOT = '.',
391  INI_COMMA = ','
392 };
393 
394 
408 };
409 
410 
427 };
428 
429 
445 };
446 
447 
451 static const IniFormat INI_DEFAULT_FORMAT = _LIBCONFINI_DEFAULT_FORMAT_;
452 
453 
458 /* All properties are set to `0` here. */
459 static const IniFormat INI_UNIXLIKE_FORMAT = _LIBCONFINI_FORMAT_ZERO_;
460 
461 
467 extern _Bool INI_GLOBAL_LOWERCASE_MODE;
468 
469 
473 extern char * INI_GLOBAL_IMPLICIT_VALUE;
474 
475 
481 extern size_t INI_GLOBAL_IMPLICIT_V_LEN;
482 
483 
484 
485 /* CLEAN THE PRIVATE ENVIRONMENT */
486 
487 
488 #undef _LIBCONFINI_FORMAT_ZERO_
489 #undef _LIBCONFINI_DEFAULT_FORMAT_
490 #undef _LIBCONFINI_INIFORMAT_STRUCT_
491 #undef _LIBCONFINI_INIFORMAT_DECLS_
492 #undef _LIBCONFINI_FMT_ZERO_FIELDS_
493 #undef _LIBCONFINI_DEFAULT_FMT_FIELDS_
494 
495 
496 
497 /* END OF `_LIBCONFINI_HEADER_` */
498 
499 
500 #endif
501 
502 
503 
504 /* EOF */
505 
void ini_global_set_lowercase_mode(_Bool lowercase)
Sets the value of the global variable INI_GLOBAL_LOWERCASE_MODE.
Definition: confini.c:4293
Definition: confini.h:437
static const IniFormat INI_UNIXLIKE_FORMAT
A model format for Unix-like .conf files (space characters are delimiters between keys and values) ...
Definition: confini.h:459
_Bool ini_string_match_ss(const char *const simple_string_a, const char *const simple_string_b, const IniFormat format)
Compares two simple strings and checks if they match.
Definition: confini.c:2499
Definition: confini.h:368
Definition: confini.h:376
Definition: confini.h:357
Definition: confini.h:434
Definition: confini.h:355
Definition: confini.h:351
_Bool ini_array_match(const char *const ini_string_a, const char *const ini_string_b, const char delimiter, const IniFormat format)
Compares two INI arrays and checks if they match.
Definition: confini.c:2950
Definition: confini.h:443
Definition: confini.h:424
int(* IniDispHandler)(IniDispatch *dispatch, void *user_data)
Callback function for handling an IniDispatch structure.
Definition: confini.h:137
size_t v_len
Definition: confini.h:113
struct IniDispatch IniDispatch
Dispatch of a single INI node.
Definition: confini.h:375
Definition: confini.h:371
Definition: confini.h:418
Global statistics about an INI file.
Definition: confini.h:96
long int(*const ini_get_lint)(const char *ini_string)
Link to atol()
Definition: confini.c:4469
uint8_t type
Definition: confini.h:108
Definition: confini.h:353
IniFormatNum ini_fton(const IniFormat format)
Calculates the IniFormatNum of an IniFormat.
Definition: confini.c:4336
Definition: confini.h:391
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:90
Definition: confini.h:356
const size_t bytes
Definition: confini.h:98
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:451
IniDelimiters
Most used key-value and array delimiters (but a delimiter may also be any other ASCII character) ...
Definition: confini.h:384
char * data
Definition: confini.h:109
size_t ini_string_parse(char *const ini_string, const IniFormat format)
Unescapes \', \", and \\ and removes all unescaped quotes (if single/double quotes are considered met...
Definition: confini.c:3311
Definition: confini.h:415
IniMultiline
Possible values of IniFormat::multiline_nodes.
Definition: confini.h:433
IniNodeType
INI node types.
Definition: confini.h:366
Definition: confini.h:372
struct IniStatistics IniStatistics
Global statistics about an INI file.
Definition: confini.h:358
Definition: confini.h:374
size_t INI_GLOBAL_IMPLICIT_V_LEN
Length of the value assigned to implicit keys – this may be any unsigned number, independently of th...
Definition: confini.c:4484
Definition: confini.h:385
IniSectionPaths
Possible values of IniFormat::section_paths.
Definition: confini.h:414
int(*const ini_get_int)(const char *ini_string)
Link to atoi()
Definition: confini.c:4467
Definition: confini.h:401
int load_ini_path(const char *const path, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
Parses an INI file and dispatches its content using a path as argument.
Definition: confini.c:2454
char * ini_array_break(char *const ini_string, const char delimiter, const IniFormat format)
Replaces the first delimiter found (together with the spaces that surround it) with \0 ...
Definition: confini.c:4029
int ini_array_foreach(const char *const ini_string, const char delimiter, const IniFormat format, const IniSubstrHandler f_foreach, void *const user_data)
Calls a custom function for each member of a stringified INI array, without modifying the content of ...
Definition: confini.c:3614
Definition: confini.h:390
Definition: confini.h:373
int(* IniStatsHandler)(IniStatistics *statistics, void *user_data)
Callback function for handling an IniStatistics structure.
Definition: confini.h:128
int ini_get_bool(const char *const ini_string, const int return_value)
Checks whether a string matches one of the booleans listed in the private constant INI_BOOLEANS (case...
Definition: confini.c:4404
Definition: confini.h:349
IniCommentMarker
Possible values of IniFormat::semicolon_marker and IniFormat::hash_marker (i.e., meaning of /\s+[#;]/...
Definition: confini.h:400
char * value
Definition: confini.h:110
Definition: confini.h:406
size_t ini_array_collapse(char *const ini_string, const char delimiter, const IniFormat format)
Compresses the distribution of the data of a stringified INI array by removing all the white spaces t...
Definition: confini.c:3831
IniFormat ini_ntof(IniFormatNum format_id)
Constructs a new IniFormat according to an IniFormatNum.
Definition: confini.c:4358
size_t d_len
Definition: confini.h:112
_Bool INI_GLOBAL_LOWERCASE_MODE
If set to true, key and section names in case-insensitive INI formats will be dispatched lowercase...
Definition: confini.c:4480
Definition: confini.h:440
Definition: confini.h:388
uint32_t IniFormatNum
The unique ID number of an INI format (24-bit maximum)
Definition: confini.h:122
ConfiniInterruptNo
Error codes.
Definition: confini.h:348
_Bool ini_string_match_ii(const char *const ini_string_a, const char *const ini_string_b, const IniFormat format)
Compares two INI strings and checks if they match.
Definition: confini.c:2759
int(* IniSubstrHandler)(const char *ini_string, size_t fragm_offset, size_t fragm_length, size_t fragm_num, IniFormat format, void *user_data)
Callback function for handling a selected fragment of an INI string.
Definition: confini.h:158
char * ini_array_release(char **ini_strptr, const char delimiter, const IniFormat format)
Replaces the first delimiter found (together with the spaces that surround it) with \0...
Definition: confini.c:4106
size_t at_len
Definition: confini.h:114
Definition: confini.h:367
Definition: confini.h:389
int ini_array_split(char *const ini_string, const char delimiter, const IniFormat format, const IniStrHandler f_foreach, void *const user_data)
Splits a stringified INI array into NUL-separated members and calls a custom function for each member...
Definition: confini.c:4174
size_t dispatch_id
Definition: confini.h:115
double(*const ini_get_float)(const char *ini_string)
Link to atof()
Definition: confini.c:4473
void ini_global_set_implicit_value(char *const implicit_value, const size_t implicit_v_len)
Sets the value to be to be assigned to implicit keys.
Definition: confini.c:4319
const size_t members
Definition: confini.h:99
_Bool ini_string_match_si(const char *const simple_string, const char *const ini_string, const IniFormat format)
Compares a simple string and an INI string and and checks if they match.
Definition: confini.c:2594
Dispatch of a single INI node.
Definition: confini.h:106
int load_ini_file(FILE *const ini_file, const IniFormat format, const IniStatsHandler f_init, const IniDispHandler f_foreach, void *const user_data)
Parses an INI file and dispatches its content using a FILE structure as argument. ...
Definition: confini.c:1923
const char * append_to
Definition: confini.h:111
Definition: confini.h:404
size_t ini_unquote(char *const ini_string, const IniFormat format)
Unescapes \', \", and \\ and removes all unescaped quotes (if single/double quotes are considered met...
Definition: confini.c:3182
size_t ini_array_get_length(const char *const ini_string, const char delimiter, const IniFormat format)
Gets the length of a stringified INI array in number of members.
Definition: confini.c:3498
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:4482
int(* IniStrHandler)(char *ini_string, size_t string_length, size_t string_num, IniFormat format, void *user_data)
Callback function for handling an INI string.
Definition: confini.h:146
Definition: confini.h:403
struct IniFormat IniFormat
24-bit bitfield representing the format of an INI file (INI dialect)
const IniFormat format
Definition: confini.h:107
size_t ini_array_shift(const char **const ini_strptr, const char delimiter, const IniFormat format)
Shifts the location pointed by ini_strptr to the next member of the INI array (without modifying the ...
Definition: confini.c:3740
Definition: confini.h:421
const IniFormat format
Definition: confini.h:97
long long int(*const ini_get_llint)(const char *ini_string)
Link to atoll()
Definition: confini.c:4471