xref: /f-stack/lib/ff_ini_parser.h (revision a9643ea8)
1*a9643ea8Slogwang /* inih -- simple .INI file parser
2*a9643ea8Slogwang 
3*a9643ea8Slogwang inih is released under the New BSD license. Go to the project
4*a9643ea8Slogwang home page for more info:
5*a9643ea8Slogwang 
6*a9643ea8Slogwang https://github.com/benhoyt/inih
7*a9643ea8Slogwang 
8*a9643ea8Slogwang */
9*a9643ea8Slogwang 
10*a9643ea8Slogwang #ifndef _FSTACK_INI_H__
11*a9643ea8Slogwang #define _FSTACK_INI_H__
12*a9643ea8Slogwang 
13*a9643ea8Slogwang /* Make this header file easier to include in C++ code */
14*a9643ea8Slogwang #ifdef __cplusplus
15*a9643ea8Slogwang extern "C" {
16*a9643ea8Slogwang #endif
17*a9643ea8Slogwang 
18*a9643ea8Slogwang #include <stdio.h>
19*a9643ea8Slogwang 
20*a9643ea8Slogwang /* Typedef for prototype of handler function. */
21*a9643ea8Slogwang typedef int (*ini_handler)(void* user, const char* section,
22*a9643ea8Slogwang                            const char* name, const char* value);
23*a9643ea8Slogwang 
24*a9643ea8Slogwang /* Typedef for prototype of fgets-style reader function. */
25*a9643ea8Slogwang typedef char* (*ini_reader)(char* str, int num, void* stream);
26*a9643ea8Slogwang 
27*a9643ea8Slogwang /* Parse given INI-style file. May have [section]s, name=value pairs
28*a9643ea8Slogwang    (whitespace stripped), and comments starting with ';' (semicolon). Section
29*a9643ea8Slogwang    is "" if name=value pair parsed before any section heading. name:value
30*a9643ea8Slogwang    pairs are also supported as a concession to Python's configparser.
31*a9643ea8Slogwang 
32*a9643ea8Slogwang    For each name=value pair parsed, call handler function with given user
33*a9643ea8Slogwang    pointer as well as section, name, and value (data only valid for duration
34*a9643ea8Slogwang    of handler call). Handler should return nonzero on success, zero on error.
35*a9643ea8Slogwang 
36*a9643ea8Slogwang    Returns 0 on success, line number of first error on parse error (doesn't
37*a9643ea8Slogwang    stop on first error), -1 on file open error, or -2 on memory allocation
38*a9643ea8Slogwang    error (only when INI_USE_STACK is zero).
39*a9643ea8Slogwang */
40*a9643ea8Slogwang int ini_parse(const char* filename, ini_handler handler, void* user);
41*a9643ea8Slogwang 
42*a9643ea8Slogwang /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
43*a9643ea8Slogwang    close the file when it's finished -- the caller must do that. */
44*a9643ea8Slogwang int ini_parse_file(FILE* file, ini_handler handler, void* user);
45*a9643ea8Slogwang 
46*a9643ea8Slogwang /* Same as ini_parse(), but takes an ini_reader function pointer instead of
47*a9643ea8Slogwang    filename. Used for implementing custom or string-based I/O. */
48*a9643ea8Slogwang int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
49*a9643ea8Slogwang                      void* user);
50*a9643ea8Slogwang 
51*a9643ea8Slogwang /* Nonzero to allow multi-line value parsing, in the style of Python's
52*a9643ea8Slogwang    configparser. If allowed, ini_parse() will call the handler with the same
53*a9643ea8Slogwang    name for each subsequent line parsed. */
54*a9643ea8Slogwang #ifndef INI_ALLOW_MULTILINE
55*a9643ea8Slogwang #define INI_ALLOW_MULTILINE 1
56*a9643ea8Slogwang #endif
57*a9643ea8Slogwang 
58*a9643ea8Slogwang /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
59*a9643ea8Slogwang    the file. See http://code.google.com/p/inih/issues/detail?id=21 */
60*a9643ea8Slogwang #ifndef INI_ALLOW_BOM
61*a9643ea8Slogwang #define INI_ALLOW_BOM 1
62*a9643ea8Slogwang #endif
63*a9643ea8Slogwang 
64*a9643ea8Slogwang /* Nonzero to allow inline comments (with valid inline comment characters
65*a9643ea8Slogwang    specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
66*a9643ea8Slogwang    Python 3.2+ configparser behaviour. */
67*a9643ea8Slogwang #ifndef INI_ALLOW_INLINE_COMMENTS
68*a9643ea8Slogwang #define INI_ALLOW_INLINE_COMMENTS 1
69*a9643ea8Slogwang #endif
70*a9643ea8Slogwang #ifndef INI_INLINE_COMMENT_PREFIXES
71*a9643ea8Slogwang #define INI_INLINE_COMMENT_PREFIXES ";"
72*a9643ea8Slogwang #endif
73*a9643ea8Slogwang 
74*a9643ea8Slogwang /* Nonzero to use stack, zero to use heap (malloc/free). */
75*a9643ea8Slogwang #ifndef INI_USE_STACK
76*a9643ea8Slogwang #define INI_USE_STACK 1
77*a9643ea8Slogwang #endif
78*a9643ea8Slogwang 
79*a9643ea8Slogwang /* Stop parsing on first error (default is to keep parsing). */
80*a9643ea8Slogwang #ifndef INI_STOP_ON_FIRST_ERROR
81*a9643ea8Slogwang #define INI_STOP_ON_FIRST_ERROR 1
82*a9643ea8Slogwang #endif
83*a9643ea8Slogwang 
84*a9643ea8Slogwang /* Maximum line length for any line in INI file. */
85*a9643ea8Slogwang #ifndef INI_MAX_LINE
86*a9643ea8Slogwang #define INI_MAX_LINE 200
87*a9643ea8Slogwang #endif
88*a9643ea8Slogwang 
89*a9643ea8Slogwang #ifdef __cplusplus
90*a9643ea8Slogwang }
91*a9643ea8Slogwang #endif
92*a9643ea8Slogwang 
93*a9643ea8Slogwang #endif /* _FSTACK_INI_H__ */
94