libconfini
Yet another INI parser
confini.h
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
14 #ifndef _LIBCONFINI_HEADER_
15 #define _LIBCONFINI_HEADER_
16 
17 #include <stdio.h>
18 #include <stdint.h>
19 
20 
21 
22 /* PRIVATE (HEADER-SCOPED) MACROS */
23 
24 
25 #define _LIBCONFINI_INIFORMAT_DECLS_(NAME, OFFSET, SIZE, DEFVAL) unsigned char NAME:SIZE;
26 #define _LIBCONFINI_DEF_FORMAT_FIELDS_(NAME, OFFSET, SIZE, DEFVAL) DEFVAL,
27 #define _LIBCONFINI_INIFORMAT_STRUCT_ struct IniFormat { INIFORMAT_TABLE_AS(_LIBCONFINI_INIFORMAT_DECLS_) }
28 #define _LIBCONFINI_DEFAULT_FORMAT_ { INIFORMAT_TABLE_AS(_LIBCONFINI_DEF_FORMAT_FIELDS_) }
29 
30 
31 
32 /* PUBLIC MACROS */
33 
34 
36 /*
37  NOTE: The following table **defines** (and links together) both the
38  #IniFormat and #IniFormatNum data types declared in this header
39 */
40 #define INIFORMAT_TABLE_AS(_____) /* IniFormat table *\
41 
42  NAME BIT SIZE DEFAULT
43  */\
44  _____( delimiter_symbol, 0, 7, INI_EQUALS ) \
45  _____( case_sensitive, 7, 1, 0 )/*
46  */\
47  _____( semicolon_marker, 8, 2, INI_DISABLED_OR_COMMENT ) \
48  _____( hash_marker, 10, 2, INI_DISABLED_OR_COMMENT )/*
49  */\
50  _____( section_paths, 12, 2, INI_ABSOLUTE_AND_RELATIVE ) \
51  _____( multiline_nodes, 14, 2, INI_MULTILINE_EVERYWHERE )/*
52  */\
53  _____( no_single_quotes, 16, 1, 0 ) \
54  _____( no_double_quotes, 17, 1, 0 ) \
55  _____( no_spaces_in_names, 18, 1, 0 ) \
56  _____( implicit_is_not_empty, 19, 1, 0 ) \
57  _____( do_not_collapse_values, 20, 1, 0 ) \
58  _____( preserve_empty_quotes, 21, 1, 0 ) \
59  _____( disabled_after_space, 22, 1, 0 ) \
60  _____( disabled_can_be_implicit, 23, 1, 0 )
61 
62 
64 #define INIFORMAT_HAS_NO_ESC(FORMAT) (FORMAT.multiline_nodes == INI_NO_MULTILINE && FORMAT.no_double_quotes && FORMAT.no_single_quotes)
65 
66 
67 
68 /* PUBLIC TYPEDEFS */
69 
70 
72 typedef _LIBCONFINI_INIFORMAT_STRUCT_ IniFormat;
73 
75 typedef struct IniStatistics {
77  const size_t bytes;
78  const size_t members;
80 
82 typedef struct IniDispatch {
84  uint8_t type;
85  char * data;
86  char * value;
87  const char * append_to;
88  size_t d_len;
89  size_t v_len;
90  size_t at_len;
91  size_t dispatch_id;
92 } IniDispatch;
93 
95 typedef uint32_t IniFormatNum;
96 
98 typedef int (* IniStatsHandler) (
99  IniStatistics * statistics,
100  void * user_data
101 );
102 
104 typedef int (* IniDispHandler) (
105  IniDispatch * dispatch,
106  void * user_data
107 );
108 
110 typedef int (* IniStrHandler) (
111  char * ini_string,
112  size_t string_length,
113  size_t string_num,
114  IniFormat format,
115  void * user_data
116 );
117 
119 typedef int (* IniSubstrHandler) (
120  const char * ini_string,
121  size_t fragm_offset,
122  size_t fragm_length,
123  size_t fragm_num,
124  IniFormat format,
125  void * user_data
126 );
127 
128 
129 
130 /* PUBLIC FUNCTIONS */
131 
132 
133 extern int load_ini_file (
134  FILE * const ini_file,
135  const IniFormat format,
136  const IniStatsHandler f_init,
137  const IniDispHandler f_foreach,
138  void * const user_data
139 );
140 
141 extern int load_ini_path (
142  const char * const path,
143  const IniFormat format,
144  const IniStatsHandler f_init,
145  const IniDispHandler f_foreach,
146  void * const user_data
147 );
148 
149 extern _Bool ini_string_match_ss (
150  const char * const simple_string_a,
151  const char * const simple_string_b,
152  const IniFormat format
153 );
154 
155 extern _Bool ini_string_match_si (
156  const char * const simple_string,
157  const char * const ini_string,
158  const IniFormat format
159 );
160 
161 extern _Bool ini_string_match_ii (
162  const char * const ini_string_a,
163  const char * const ini_string_b,
164  const IniFormat format
165 );
166 
167 extern _Bool ini_array_match (
168  const char * const ini_string_a,
169  const char * const ini_string_b,
170  const char delimiter,
171  const IniFormat format
172 );
173 
174 extern size_t ini_unquote (
175  char * const ini_string,
176  const IniFormat format
177 );
178 
179 extern size_t ini_string_parse (
180  char * const ini_string,
181  const IniFormat format
182 );
183 
184 extern size_t ini_array_get_length (
185  const char * const ini_string,
186  const char delimiter,
187  const IniFormat format
188 );
189 
190 extern int ini_array_foreach (
191  const char * const ini_string,
192  const char delimiter,
193  const IniFormat format,
194  const IniSubstrHandler f_foreach,
195  void * const user_data
196 );
197 
198 extern size_t ini_array_shift (
199  const char ** const ini_strptr,
200  const char delimiter,
201  const IniFormat format
202 );
203 
204 extern size_t ini_array_collapse (
205  char * const ini_string,
206  const char delimiter,
207  const IniFormat format
208 );
209 
210 extern char * ini_array_break (
211  char * const ini_string,
212  const char delimiter,
213  const IniFormat format
214 );
215 
216 extern char * ini_array_release (
217  char ** ini_strptr,
218  const char delimiter,
219  const IniFormat format
220 );
221 
222 extern int ini_array_split (
223  char * const ini_string,
224  const char delimiter,
225  const IniFormat format,
226  const IniStrHandler f_foreach,
227  void * const user_data
228 );
229 
230 extern void ini_global_set_lowercase_mode (
231  _Bool lowercase
232 );
233 
234 extern void ini_global_set_implicit_value (
235  char * const implicit_value,
236  const size_t implicit_v_len
237 );
238 
239 extern IniFormatNum ini_fton (
240  const IniFormat format
241 );
242 
243 extern IniFormat ini_ntof (
244  IniFormatNum format_id
245 );
246 
247 extern signed int ini_get_bool (
248  const char * const ini_string,
249  const signed int return_value
250 );
251 
252 
253 
254 /* PUBLIC LINKS */
255 
256 
257 extern int (* const ini_get_int) (
258  const char * ini_string
259 );
260 
261 extern long int (* const ini_get_lint) (
262  const char * ini_string
263 );
264 
265 extern long long int (* const ini_get_llint) (
266  const char * ini_string
267 );
268 
269 extern double (* const ini_get_float) (
270  const char * ini_string
271 );
272 
273 
274 
275 /* PUBLIC CONSTANTS AND VARIABLES */
276 
277 
279 #define CONFINI_ERROR 252
280 
290 };
291 
295  INI_VALUE = 1,
297  INI_KEY = 2,
303 };
304 
309  INI_EQUALS = '=',
310  INI_COLON = ':',
311  INI_DOT = '.',
312  INI_COMMA = ','
313 };
314 
321 };
322 
329 };
330 
337 };
338 
340 static const IniFormat INI_DEFAULT_FORMAT = _LIBCONFINI_DEFAULT_FORMAT_;
341 
343 /* All properties are set to `0` here. */
344 static const IniFormat INI_UNIXLIKE_FORMAT = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
345 
347 extern _Bool INI_GLOBAL_LOWERCASE_MODE;
348 
350 extern char * INI_GLOBAL_IMPLICIT_VALUE;
351 
353 extern size_t INI_GLOBAL_IMPLICIT_V_LEN;
354 
355 
356 
357 /* CLEAN THE PRIVATE ENVIRONMENT */
358 
359 
360 #undef _LIBCONFINI_DEFAULT_FORMAT_
361 #undef _LIBCONFINI_INIFORMAT_STRUCT_
362 #undef _LIBCONFINI_DEF_FORMAT_FIELDS_
363 #undef _LIBCONFINI_INIFORMAT_DECLS_
364 
365 
366 
367 /* END OF _LIBCONFINI_HEADER_ */
368 
369 
370 #endif
371 
372 
373 
374 /* EOF */
375 
void ini_global_set_lowercase_mode(_Bool lowercase)
Sets the value of the global variable INI_GLOBAL_LOWERCASE_MODE.
Definition: confini.c:4281
int(*const ini_get_int)(const char *ini_string)
Link to atoi()
Definition: confini.c:4455
Definition: confini.h:334
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:344
_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:2489
Definition: confini.h:295
Definition: confini.h:302
Definition: confini.h:288
Definition: confini.h:333
Definition: confini.h:286
Definition: confini.h:284
_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:2939
Definition: confini.h:336
Definition: confini.h:328
int(* IniDispHandler)(IniDispatch *dispatch, void *user_data)
Callback function for handling an IniDispatch structure.
Definition: confini.h:104
size_t v_len
Definition: confini.h:89
struct IniDispatch IniDispatch
Dispatch of a single INI node.
Definition: confini.h:301
Definition: confini.h:297
Definition: confini.h:326
Global statistics about an INI file.
Definition: confini.h:75
uint8_t type
Definition: confini.h:84
Definition: confini.h:285
IniFormatNum ini_fton(const IniFormat format)
Calculates the IniFormatNum of an IniFormat.
Definition: confini.c:4324
Definition: confini.h:312
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:72
Definition: confini.h:287
const size_t bytes
Definition: confini.h:77
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:340
double(*const ini_get_float)(const char *ini_string)
Link to atof()
Definition: confini.c:4461
IniDelimiters
Most used key-value and array delimiters (but a delimiter may also be any other ASCII character) ...
Definition: confini.h:306
char * data
Definition: confini.h:85
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:3300
Definition: confini.h:325
IniMultiline
Possible values of IniFormat::multiline_nodes.
Definition: confini.h:332
IniNodeType
INI node types.
Definition: confini.h:293
Definition: confini.h:298
signed int ini_get_bool(const char *const ini_string, const signed int return_value)
Checks whether a string matches one of the booleans listed in the private constant INI_BOOLEANS (case...
Definition: confini.c:4392
struct IniStatistics IniStatistics
Global statistics about an INI file.
Definition: confini.h:289
Definition: confini.h:300
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:4472
Definition: confini.h:307
IniSectionPaths
Possible values of IniFormat::section_paths.
Definition: confini.h:324
Definition: confini.h:317
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:2444
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:4019
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 t...
Definition: confini.c:3605
Definition: confini.h:311
Definition: confini.h:299
int(* IniStatsHandler)(IniStatistics *statistics, void *user_data)
Callback function for handling an IniStatistics structure.
Definition: confini.h:98
Definition: confini.h:283
long long int(*const ini_get_llint)(const char *ini_string)
Link to atoll()
Definition: confini.c:4459
IniCommentMarker
Possible values of IniFormat::semicolon_marker and IniFormat::hash_marker (i.e., meaning of /\s+[#;]/...
Definition: confini.h:316
char * value
Definition: confini.h:86
Definition: confini.h:320
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:3821
IniFormat ini_ntof(IniFormatNum format_id)
Constructs a new IniFormat according to an IniFormatNum.
Definition: confini.c:4346
size_t d_len
Definition: confini.h:88
_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:4468
Definition: confini.h:335
Definition: confini.h:309
uint32_t IniFormatNum
The unique ID number of an INI format (24-bit maximum)
Definition: confini.h:95
ConfiniInterruptNo
Error codes.
Definition: confini.h:282
_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:2748
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:119
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:4096
size_t at_len
Definition: confini.h:90
Definition: confini.h:294
Definition: confini.h:310
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:4164
size_t dispatch_id
Definition: confini.h:91
long int(*const ini_get_lint)(const char *ini_string)
Link to atol()
Definition: confini.c:4457
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:4307
const size_t members
Definition: confini.h:78
_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:2584
Dispatch of a single INI node.
Definition: confini.h:82
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:1914
const char * append_to
Definition: confini.h:87
Definition: confini.h:319
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:3171
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:3488
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:4470
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:110
Definition: confini.h:318
struct IniFormat IniFormat
24-bit bitfield representing the format of an INI file (INI dialect)
const IniFormat format
Definition: confini.h:83
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:3729
Definition: confini.h:327
const IniFormat format
Definition: confini.h:76