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_DECLARATION_(PROPERTY, OFFSET, SIZE, DEFAULT) unsigned char PROPERTY:SIZE;
26 #define _LIBCONFINI_INIFORMAT_ASSIGNEMENT_(PROPERTY, OFFSET, SIZE, DEFAULT) DEFAULT,
27 #define _LIBCONFINI_INIFORMAT_STRUCT_ struct IniFormat { INIFORMAT_TABLE_AS(_LIBCONFINI_INIFORMAT_DECLARATION_) }
28 #define _LIBCONFINI_DEFAULT_FORMAT_ { INIFORMAT_TABLE_AS(_LIBCONFINI_INIFORMAT_ASSIGNEMENT_) }
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 OFFSET LENGTH DEFAULT VALUE
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  _____( section_paths, 12, 2, INI_ABSOLUTE_AND_RELATIVE ) \
50  _____( multiline_nodes, 14, 2, INI_MULTILINE_EVERYWHERE )/*
51  */\
52  _____( no_single_quotes, 16, 1, 0 ) \
53  _____( no_double_quotes, 17, 1, 0 ) \
54  _____( no_spaces_in_names, 18, 1, 0 ) \
55  _____( implicit_is_not_empty, 19, 1, 0 ) \
56  _____( do_not_collapse_values, 20, 1, 0 ) \
57  _____( preserve_empty_quotes, 21, 1, 0 ) \
58  _____( disabled_after_space, 22, 1, 0 ) \
59  _____( disabled_can_be_implicit, 23, 1, 0 )
60 
61 
63 #define INIFORMAT_HAS_NO_ESC(FMT) (FMT.multiline_nodes == INI_NO_MULTILINE && FMT.no_double_quotes && FMT.no_single_quotes)
64 
65 
66 
67 /* PUBLIC TYPEDEFS */
68 
69 
71 typedef _LIBCONFINI_INIFORMAT_STRUCT_ IniFormat;
72 
74 typedef struct IniStatistics {
76  const size_t bytes;
77  const size_t members;
79 
81 typedef struct IniDispatch {
83  uint8_t type;
84  char * data;
85  char * value;
86  const char * append_to;
87  size_t d_len;
88  size_t v_len;
89  size_t at_len;
90  size_t dispatch_id;
91 } IniDispatch;
92 
94 typedef uint32_t IniFormatNum;
95 
97 typedef int (* IniStatsHandler) (
98  IniStatistics * statistics,
99  void * user_data
100 );
101 
103 typedef int (* IniDispHandler) (
104  IniDispatch * dispatch,
105  void * user_data
106 );
107 
109 typedef int (* IniStrHandler) (
110  char * ini_string,
111  size_t string_length,
112  size_t string_num,
113  IniFormat format,
114  void * user_data
115 );
116 
118 typedef int (* IniSubstrHandler) (
119  const char * ini_string,
120  size_t fragm_offset,
121  size_t fragm_length,
122  size_t fragm_num,
123  IniFormat format,
124  void * user_data
125 );
126 
127 
128 
129 /* PUBLIC FUNCTIONS */
130 
131 
132 extern int load_ini_file (
133  FILE * const ini_file,
134  const IniFormat format,
135  const IniStatsHandler f_init,
136  const IniDispHandler f_foreach,
137  void * const user_data
138 );
139 
140 extern int load_ini_path (
141  const char * const path,
142  const IniFormat format,
143  const IniStatsHandler f_init,
144  const IniDispHandler f_foreach,
145  void * const user_data
146 );
147 
148 extern _Bool ini_string_match_ss (
149  const char * const simple_string_a,
150  const char * const simple_string_b,
151  const IniFormat format
152 );
153 
154 extern _Bool ini_string_match_si (
155  const char * const simple_string,
156  const char * const ini_string,
157  const IniFormat format
158 );
159 
160 extern _Bool ini_string_match_ii (
161  const char * const ini_string_a,
162  const char * const ini_string_b,
163  const IniFormat format
164 );
165 
166 extern _Bool ini_array_match (
167  const char * const ini_string_a,
168  const char * const ini_string_b,
169  const char delimiter,
170  const IniFormat format
171 );
172 
173 extern size_t ini_unquote (
174  char * const ini_string,
175  const IniFormat format
176 );
177 
178 extern size_t ini_string_parse (
179  char * const ini_string,
180  const IniFormat format
181 );
182 
183 extern size_t ini_array_get_length (
184  const char * const ini_string,
185  const char delimiter,
186  const IniFormat format
187 );
188 
189 extern int ini_array_foreach (
190  const char * const ini_string,
191  const char delimiter,
192  const IniFormat format,
193  const IniSubstrHandler f_foreach,
194  void * const user_data
195 );
196 
197 extern size_t ini_array_shift (
198  const char ** const ini_strptr,
199  const char delimiter,
200  const IniFormat format
201 );
202 
203 extern size_t ini_array_collapse (
204  char * const ini_string,
205  const char delimiter,
206  const IniFormat format
207 );
208 
209 extern char * ini_array_break (
210  char * const ini_string,
211  const char delimiter,
212  const IniFormat format
213 );
214 
215 extern char * ini_array_release (
216  char ** ini_strptr,
217  const char delimiter,
218  const IniFormat format
219 );
220 
221 extern int ini_array_split (
222  char * const ini_string,
223  const char delimiter,
224  const IniFormat format,
225  const IniStrHandler f_foreach,
226  void * const user_data
227 );
228 
229 extern void ini_global_set_lowercase_mode (
230  _Bool lowercase
231 );
232 
233 extern void ini_global_set_implicit_value (
234  char * const implicit_value,
235  const size_t implicit_v_len
236 );
237 
238 extern IniFormatNum ini_fton (
239  const IniFormat format
240 );
241 
242 extern IniFormat ini_ntof (
243  IniFormatNum format_id
244 );
245 
246 extern signed int ini_get_bool (
247  const char * const ini_string,
248  const signed int return_value
249 );
250 
251 
252 
253 /* PUBLIC LINKS */
254 
255 
256 extern int (* const ini_get_int) (
257  const char * ini_string
258 );
259 
260 extern long int (* const ini_get_lint) (
261  const char * ini_string
262 );
263 
264 extern long long int (* const ini_get_llint) (
265  const char * ini_string
266 );
267 
268 extern double (* const ini_get_float) (
269  const char * ini_string
270 );
271 
272 
273 
274 /* PUBLIC CONSTANTS AND VARIABLES */
275 
276 
278 #define CONFINI_ERROR 252
279 
289 };
290 
294  INI_VALUE = 1,
295  INI_KEY = 2,
301 };
302 
306  INI_EQUALS = '=',
307  INI_COLON = ':',
308  INI_DOT = '.',
309  INI_COMMA = ','
310 };
311 
318 };
319 
326 };
327 
334 };
335 
337 static const IniFormat INI_DEFAULT_FORMAT = _LIBCONFINI_DEFAULT_FORMAT_;
338 
340 /* All properties are set to `0` here. */
341 static const IniFormat INI_UNIXLIKE_FORMAT = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
342 
344 extern _Bool INI_GLOBAL_LOWERCASE_MODE;
345 
347 extern char * INI_GLOBAL_IMPLICIT_VALUE;
348 
350 extern size_t INI_GLOBAL_IMPLICIT_V_LEN;
351 
352 
353 
354 /* CLEAN THE PRIVATE ENVIRONMENT */
355 
356 
357 #undef _LIBCONFINI_DEFAULT_FORMAT_
358 #undef _LIBCONFINI_INIFORMAT_STRUCT_
359 #undef _LIBCONFINI_INIFORMAT_ASSIGNEMENT_
360 #undef _LIBCONFINI_INIFORMAT_DECLARATION_
361 
362 
363 
364 /* END OF _LIBCONFINI_HEADER_ */
365 
366 
367 #endif
368 
369 
370 
371 /* EOF */
372 
void ini_global_set_lowercase_mode(_Bool lowercase)
Sets the value of the global variable INI_GLOBAL_LOWERCASE_MODE
Definition: confini.c:4029
int(*const ini_get_int)(const char *ini_string)
Link to atoi()
Definition: confini.c:4203
Definition: confini.h:331
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:341
_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:2342
Definition: confini.h:294
Definition: confini.h:300
Definition: confini.h:287
Definition: confini.h:330
Definition: confini.h:285
Definition: confini.h:283
_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:2790
Definition: confini.h:333
Definition: confini.h:325
int(* IniDispHandler)(IniDispatch *dispatch, void *user_data)
Callback function for handling an IniDispatch structure.
Definition: confini.h:103
size_t v_len
Definition: confini.h:88
struct IniDispatch IniDispatch
Dispatch of a single INI node.
Definition: confini.h:299
Definition: confini.h:295
Definition: confini.h:323
Global statistics about an INI file.
Definition: confini.h:74
uint8_t type
Definition: confini.h:83
Definition: confini.h:284
IniFormatNum ini_fton(const IniFormat format)
Calculates the IniFormatNum of an IniFormat
Definition: confini.c:4072
Definition: confini.h:309
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:71
Definition: confini.h:286
const size_t bytes
Definition: confini.h:76
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:337
double(*const ini_get_float)(const char *ini_string)
Link to atof()
Definition: confini.c:4209
IniDelimiters
Most used key-value and array delimiters (but a delimiter may also be any other ASCII character) ...
Definition: confini.h:304
char * data
Definition: confini.h:84
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 meta...
Definition: confini.c:3138
Definition: confini.h:322
IniMultiline
Possible values of IniFormat::multiline_nodes.
Definition: confini.h:329
IniNodeType
INI node types.
Definition: confini.h:292
Definition: confini.h:296
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:4140
struct IniStatistics IniStatistics
Global statistics about an INI file.
Definition: confini.h:288
Definition: confini.h:298
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:4220
Definition: confini.h:305
IniSectionPaths
Possible values of IniFormat::section_paths
Definition: confini.h:321
Definition: confini.h:314
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:2297
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:3789
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:3398
Definition: confini.h:308
Definition: confini.h:297
int(* IniStatsHandler)(IniStatistics *statistics, void *user_data)
Callback function for handling an IniStatistics structure.
Definition: confini.h:97
Definition: confini.h:282
long long int(*const ini_get_llint)(const char *ini_string)
Link to atoll()
Definition: confini.c:4207
IniCommentMarker
Possible values of IniFormat::semicolon_marker and IniFormat::hash_marker (i.e., meaning of /\s+[#;]/...
Definition: confini.h:313
char * value
Definition: confini.h:85
Definition: confini.h:317
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:3596
IniFormat ini_ntof(IniFormatNum format_id)
Constructs a new IniFormat according to an IniFormatNum
Definition: confini.c:4094
size_t d_len
Definition: confini.h:87
_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:4216
Definition: confini.h:332
Definition: confini.h:306
uint32_t IniFormatNum
The unique ID number of an INI format (24-bit maximum)
Definition: confini.h:94
ConfiniInterruptNo
Error codes.
Definition: confini.h:281
_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:2599
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:118
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:3862
size_t at_len
Definition: confini.h:89
Definition: confini.h:293
Definition: confini.h:307
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:3930
size_t dispatch_id
Definition: confini.h:90
long int(*const ini_get_lint)(const char *ini_string)
Link to atol()
Definition: confini.c:4205
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:4055
const size_t members
Definition: confini.h:77
_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:2437
Dispatch of a single INI node.
Definition: confini.h:81
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:1794
const char * append_to
Definition: confini.h:86
Definition: confini.h:316
size_t ini_unquote(char *const ini_string, const IniFormat format)
Unescapes \\, \' and \" and removes all unescaped quotes (if single/double quotes are considered meta...
Definition: confini.c:3014
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:3287
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:4218
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:109
Definition: confini.h:315
struct IniFormat IniFormat
24-bit bitfield representing the format of an INI file (INI dialect)
const IniFormat format
Definition: confini.h:82
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:3504
Definition: confini.h:324
const IniFormat format
Definition: confini.h:75