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_(SIZE, PROPERTY, DEFAULT) unsigned char PROPERTY:SIZE;
26 #define _LIBCONFINI_INIFORMAT_ASSIGNEMENT_(SIZE, PROPERTY, DEFAULT) DEFAULT,
27 #define _LIBCONFINI_INIFORMAT_STRUCT_ struct IniFormat { _LIBCONFINI_INIFORMAT_AS_(_LIBCONFINI_INIFORMAT_DECLARATION_) }
28 #define _LIBCONFINI_DEFAULT_FORMAT_ { _LIBCONFINI_INIFORMAT_AS_(_LIBCONFINI_INIFORMAT_ASSIGNEMENT_) }
29 
30 
31 
32 /* PUBLIC MACROS */
33 
34 
35 #define _LIBCONFINI_INIFORMAT_AS_(______) /*-*\
36 
37  ______( BITS NAME DEFAULT VALUE )/--/
38  /-*/\
39  ______( 7, delimiter_symbol, INI_EQUALS ) \
40  ______( 1, case_sensitive, 0 )/*-/
41  /-*/\
42  ______( 2, semicolon_marker, INI_DISABLED_OR_COMMENT ) \
43  ______( 2, hash_marker, INI_DISABLED_OR_COMMENT ) \
44  ______( 2, section_paths, INI_ABSOLUTE_AND_RELATIVE ) \
45  ______( 2, multiline_nodes, INI_MULTILINE_EVERYWHERE )/*-/
46  /-*/\
47  ______( 1, no_single_quotes, 0 ) \
48  ______( 1, no_double_quotes, 0 ) \
49  ______( 1, no_spaces_in_names, 0 ) \
50  ______( 1, implicit_is_not_empty, 0 ) \
51  ______( 1, do_not_collapse_values, 0 ) \
52  ______( 1, preserve_empty_quotes, 0 ) \
53  ______( 1, disabled_after_space, 0 ) \
54  ______( 1, disabled_can_be_implicit, 0 )/*-/
55  /-*/
56 
57 
59 #define INIFORMAT_HAS_NO_ESC(FMT) (FMT.multiline_nodes == INI_NO_MULTILINE && FMT.no_double_quotes && FMT.no_single_quotes)
60 
61 
62 
63 /* PUBLIC TYPEDEFS */
64 
65 
67 typedef _LIBCONFINI_INIFORMAT_STRUCT_ IniFormat;
68 
70 typedef struct IniStatistics {
72  const size_t bytes;
73  const size_t members;
75 
77 typedef struct IniDispatch {
79  uint8_t type;
80  char * data;
81  char * value;
82  const char * append_to;
83  size_t d_len;
84  size_t v_len;
85  size_t at_len;
86  size_t dispatch_id;
87 } IniDispatch;
88 
90 typedef uint32_t IniFormatNum;
91 
92 
93 
94 /* PUBLIC FUNCTIONS */
95 
96 
97 extern int load_ini_file (
98  FILE * const ini_file,
99  const IniFormat format,
100  int (* const f_init) (
101  IniStatistics * statistics,
102  void * init_other
103  ),
104  int (* const f_foreach) (
105  IniDispatch * dispatch,
106  void * foreach_other
107  ),
108  void * const user_data
109 );
110 
111 extern int load_ini_path (
112  const char * const path,
113  const IniFormat format,
114  int (* const f_init) (
115  IniStatistics * statistics,
116  void * init_other
117  ),
118  int (* const f_foreach) (
119  IniDispatch * dispatch,
120  void * foreach_other
121  ),
122  void * const user_data
123 );
124 
125 extern _Bool ini_string_match_ss (
126  const char * const simple_string_a,
127  const char * const simple_string_b,
128  const IniFormat format
129 );
130 
131 extern _Bool ini_string_match_si (
132  const char * const simple_string,
133  const char * const ini_string,
134  const IniFormat format
135 );
136 
137 extern _Bool ini_string_match_ii (
138  const char * const ini_string_a,
139  const char * const ini_string_b,
140  const IniFormat format
141 );
142 
143 extern _Bool ini_array_match (
144  const char * const ini_string_a,
145  const char * const ini_string_b,
146  const char delimiter,
147  const IniFormat format
148 );
149 
150 extern size_t ini_unquote (
151  char * const ini_string,
152  const IniFormat format
153 );
154 
155 extern size_t ini_string_parse (
156  char * const ini_string,
157  const IniFormat format
158 );
159 
160 extern size_t ini_array_get_length (
161  const char * const ini_string,
162  const char delimiter,
163  const IniFormat format
164 );
165 
166 extern int ini_array_foreach (
167  const char * const ini_string,
168  const char delimiter,
169  const IniFormat format,
170  int (* const f_foreach) (
171  const char * fullstring,
172  size_t memb_offset,
173  size_t memb_length,
174  size_t memb_num,
175  IniFormat format,
176  void * foreach_other
177  ),
178  void * const user_data
179 );
180 
181 extern size_t ini_array_shift (
182  const char ** const ini_strptr,
183  const char delimiter,
184  const IniFormat format
185 );
186 
187 extern size_t ini_array_collapse (
188  char * const ini_string,
189  const char delimiter,
190  const IniFormat format
191 );
192 
193 extern char * ini_array_break (
194  char * const ini_string,
195  const char delimiter,
196  const IniFormat format
197 );
198 
199 extern char * ini_array_release (
200  char ** ini_strptr,
201  const char delimiter,
202  const IniFormat format
203 );
204 
205 extern int ini_array_split (
206  char * const ini_string,
207  const char delimiter,
208  const IniFormat format,
209  int (* const f_foreach) (
210  char * member,
211  size_t memb_length,
212  size_t memb_num,
213  IniFormat format,
214  void * foreach_other
215  ),
216  void * const user_data
217 );
218 
219 extern void ini_global_set_lowercase_mode (
220  _Bool lowercase
221 );
222 
223 extern void ini_global_set_implicit_value (
224  char * const implicit_value,
225  const size_t implicit_v_len
226 );
227 
228 extern IniFormatNum ini_fton (
229  const IniFormat format
230 );
231 
232 extern IniFormat ini_ntof (
233  IniFormatNum format_id
234 );
235 
236 extern signed int ini_get_bool (
237  const char * const ini_string,
238  const signed int return_value
239 );
240 
241 
242 
243 /* PUBLIC LINKS */
244 
245 
246 extern int (* const ini_get_int) (
247  const char * ini_string
248 );
249 
250 extern long int (* const ini_get_lint) (
251  const char * ini_string
252 );
253 
254 extern long long int (* const ini_get_llint) (
255  const char * ini_string
256 );
257 
258 extern double (* const ini_get_float) (
259  const char * ini_string
260 );
261 
262 
263 
264 /* PUBLIC CONSTANTS AND VARIABLES */
265 
266 
268 #define CONFINI_ERROR 252
269 
279 };
280 
284  INI_VALUE = 1,
285  INI_KEY = 2,
291 };
292 
296  INI_EQUALS = '=',
297  INI_COLON = ':',
298  INI_DOT = '.',
299  INI_COMMA = ','
300 };
301 
308 };
309 
316 };
317 
324 };
325 
327 static const IniFormat INI_DEFAULT_FORMAT = _LIBCONFINI_DEFAULT_FORMAT_;
328 
330 static const IniFormat INI_UNIXLIKE_FORMAT = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; /* All properties are set to `0` here. */
331 
333 extern _Bool INI_GLOBAL_LOWERCASE_MODE;
334 
336 extern char * INI_GLOBAL_IMPLICIT_VALUE;
337 
339 extern size_t INI_GLOBAL_IMPLICIT_V_LEN;
340 
341 
342 
343 /* CLEAN THE PRIVATE ENVIRONMENT */
344 
345 
346 #undef _LIBCONFINI_INIFORMAT_DECLARATION_
347 #undef _LIBCONFINI_INIFORMAT_ASSIGNEMENT_
348 #undef _LIBCONFINI_INIFORMAT_STRUCT_
349 #undef _LIBCONFINI_DEFAULT_FORMAT_
350 
351 
352 
353 /* END OF _LIBCONFINI_HEADER_ */
354 
355 
356 #endif
357 
358 
359 
360 /* EOF */
361 
void ini_global_set_lowercase_mode(_Bool lowercase)
Sets the value of the global variable INI_GLOBAL_LOWERCASE_MODE
Definition: confini.c:3900
int(*const ini_get_int)(const char *ini_string)
Link to atoi()
Definition: confini.c:4082
int ini_array_foreach(const char *const ini_string, const char delimiter, const IniFormat format, int(*const f_foreach)(const char *fullstring, size_t memb_offset, size_t memb_length, size_t memb_num, IniFormat format, void *foreach_other), 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:3256
int load_ini_file(FILE *const ini_file, const IniFormat format, int(*const f_init)(IniStatistics *statistics, void *init_other), int(*const f_foreach)(IniDispatch *dispatch, void *foreach_other), void *const user_data)
Parses an INI file and dispatches its content using a FILE structure as argument. ...
Definition: confini.c:1642
Definition: confini.h:321
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:330
_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:2200
Definition: confini.h:284
int ini_array_split(char *const ini_string, const char delimiter, const IniFormat format, int(*const f_foreach)(char *member, size_t memb_length, size_t memb_num, IniFormat format, void *foreach_other), void *const user_data)
Splits a stringified INI array into NUL-separated members and calls a custom function for each member...
Definition: confini.c:3795
Definition: confini.h:290
Definition: confini.h:277
Definition: confini.h:320
Definition: confini.h:275
Definition: confini.h:273
_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:2648
Definition: confini.h:323
Definition: confini.h:315
size_t v_len
Definition: confini.h:84
struct IniDispatch IniDispatch
Dispatch of a single INI node.
Definition: confini.h:289
Definition: confini.h:285
Definition: confini.h:313
Global statistics about an INI file.
Definition: confini.h:70
uint8_t type
Definition: confini.h:79
Definition: confini.h:274
IniFormatNum ini_fton(const IniFormat format)
Calculates the IniFormatNum of an IniFormat
Definition: confini.c:3943
Definition: confini.h:299
24-bit bitfield representing the format of an INI file (INI dialect)
Definition: confini.h:67
Definition: confini.h:276
const size_t bytes
Definition: confini.h:72
static const IniFormat INI_DEFAULT_FORMAT
A model format for standard INI files.
Definition: confini.h:327
double(*const ini_get_float)(const char *ini_string)
Link to atof()
Definition: confini.c:4088
int load_ini_path(const char *const path, const IniFormat format, int(*const f_init)(IniStatistics *statistics, void *init_other), int(*const f_foreach)(IniDispatch *dispatch, void *foreach_other), void *const user_data)
Parses an INI file and dispatches its content using a path as argument.
Definition: confini.c:2149
IniDelimiters
Most used key-value and array delimiters (but a delimiter may also be any other ASCII character) ...
Definition: confini.h:294
char * data
Definition: confini.h:80
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:2996
Definition: confini.h:312
IniMultiline
Possible values of IniFormat::multiline_nodes.
Definition: confini.h:319
IniNodeType
INI node types.
Definition: confini.h:282
Definition: confini.h:286
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:4019
struct IniStatistics IniStatistics
Global statistics about an INI file.
Definition: confini.h:278
Definition: confini.h:288
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:4099
Definition: confini.h:295
IniSectionPaths
Possible values of IniFormat::section_paths
Definition: confini.h:311
Definition: confini.h:304
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:3654
Definition: confini.h:298
Definition: confini.h:287
Definition: confini.h:272
long long int(*const ini_get_llint)(const char *ini_string)
Link to atoll()
Definition: confini.c:4086
IniCommentMarker
Possible values of IniFormat::semicolon_marker and IniFormat::hash_marker (i.e., meaning of /\s+[#;]/...
Definition: confini.h:303
char * value
Definition: confini.h:81
Definition: confini.h:307
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:3461
IniFormat ini_ntof(IniFormatNum format_id)
Constructs a new IniFormat according to an IniFormatNum
Definition: confini.c:3970
size_t d_len
Definition: confini.h:83
_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:4095
Definition: confini.h:322
Definition: confini.h:296
uint32_t IniFormatNum
The unique ID number of an INI format (24-bit maximum)
Definition: confini.h:90
ConfiniInterruptNo
Error codes – the actual value of each constant should be considered opaque.
Definition: confini.h:271
_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:2457
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:3727
size_t at_len
Definition: confini.h:85
Definition: confini.h:283
Definition: confini.h:297
size_t dispatch_id
Definition: confini.h:86
long int(*const ini_get_lint)(const char *ini_string)
Link to atol()
Definition: confini.c:4084
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:3926
const size_t members
Definition: confini.h:73
_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:2295
Dispatch of a single INI node.
Definition: confini.h:77
const char * append_to
Definition: confini.h:82
Definition: confini.h:306
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:2872
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:3145
char * INI_GLOBAL_IMPLICIT_VALUE
Value to be assigned to implicit keys (default value: NULL)
Definition: confini.c:4097
Definition: confini.h:305
struct IniFormat IniFormat
24-bit bitfield representing the format of an INI file (INI dialect)
const IniFormat format
Definition: confini.h:78
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:3369
Definition: confini.h:314
const IniFormat format
Definition: confini.h:71