Btk
ini.h
1 /* inih -- simple .INI file parser
2 
3 SPDX-License-Identifier: BSD-3-Clause
4 
5 Copyright (C) 2009-2020, Ben Hoyt
6 
7 inih is released under the New BSD license (see LICENSE.txt). Go to the project
8 home page for more info:
9 
10 https://github.com/benhoyt/inih
11 
12 */
13 
14 #ifndef INI_H
15 #define INI_H
16 
17 /* Make this header file easier to include in C++ code */
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdio.h>
23 
24 /* Nonzero if ini_handler callback should accept lineno parameter. */
25 #ifndef INI_HANDLER_LINENO
26 #define INI_HANDLER_LINENO 0
27 #endif
28 
29 /* Typedef for prototype of handler function. */
30 #if INI_HANDLER_LINENO
31 typedef int (*ini_handler)(void* user, const char* section,
32  const char* name, const char* value,
33  int lineno);
34 #else
35 typedef int (*ini_handler)(void* user, const char* section,
36  const char* name, const char* value);
37 #endif
38 
39 /* Typedef for prototype of fgets-style reader function. */
40 typedef char* (*ini_reader)(char* str, int num, void* stream);
41 
42 /* Parse given INI-style file. May have [section]s, name=value pairs
43  (whitespace stripped), and comments starting with ';' (semicolon). Section
44  is "" if name=value pair parsed before any section heading. name:value
45  pairs are also supported as a concession to Python's configparser.
46 
47  For each name=value pair parsed, call handler function with given user
48  pointer as well as section, name, and value (data only valid for duration
49  of handler call). Handler should return nonzero on success, zero on error.
50 
51  Returns 0 on success, line number of first error on parse error (doesn't
52  stop on first error), -1 on file open error, or -2 on memory allocation
53  error (only when INI_USE_STACK is zero).
54 */
55 int ini_parse(const char* filename, ini_handler handler, void* user);
56 
57 /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
58  close the file when it's finished -- the caller must do that. */
59 int ini_parse_file(FILE* file, ini_handler handler, void* user);
60 
61 /* Same as ini_parse(), but takes an ini_reader function pointer instead of
62  filename. Used for implementing custom or string-based I/O (see also
63  ini_parse_string). */
64 int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
65  void* user);
66 
67 /* Same as ini_parse(), but takes a zero-terminated string with the INI data
68 instead of a file. Useful for parsing INI data from a network socket or
69 already in memory. */
70 int ini_parse_string(const char* string, ini_handler handler, void* user);
71 /* Same as ini_parse(), but takes a memory buffer with the INI data
72 instead of a file. Useful for parsing INI data from a network socket or
73 already in memory. */
74 int ini_parse_memory(const char* mem,size_t length,ini_handler handler, void* user);
75 
76 /* Nonzero to allow multi-line value parsing, in the style of Python's
77  configparser. If allowed, ini_parse() will call the handler with the same
78  name for each subsequent line parsed. */
79 #ifndef INI_ALLOW_MULTILINE
80 #define INI_ALLOW_MULTILINE 1
81 #endif
82 
83 /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
84  the file. See https://github.com/benhoyt/inih/issues/21 */
85 #ifndef INI_ALLOW_BOM
86 #define INI_ALLOW_BOM 1
87 #endif
88 
89 /* Chars that begin a start-of-line comment. Per Python configparser, allow
90  both ; and # comments at the start of a line by default. */
91 #ifndef INI_START_COMMENT_PREFIXES
92 #define INI_START_COMMENT_PREFIXES ";#"
93 #endif
94 
95 /* Nonzero to allow inline comments (with valid inline comment characters
96  specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
97  Python 3.2+ configparser behaviour. */
98 #ifndef INI_ALLOW_INLINE_COMMENTS
99 #define INI_ALLOW_INLINE_COMMENTS 1
100 #endif
101 #ifndef INI_INLINE_COMMENT_PREFIXES
102 #define INI_INLINE_COMMENT_PREFIXES ";"
103 #endif
104 
105 /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
106 #ifndef INI_USE_STACK
107 #define INI_USE_STACK 1
108 #endif
109 
110 /* Maximum line length for any line in INI file (stack or heap). Note that
111  this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
112 #ifndef INI_MAX_LINE
113 #define INI_MAX_LINE 200
114 #endif
115 
116 /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
117  fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
118  zero. */
119 #ifndef INI_ALLOW_REALLOC
120 #define INI_ALLOW_REALLOC 0
121 #endif
122 
123 /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
124  is zero. */
125 #ifndef INI_INITIAL_ALLOC
126 #define INI_INITIAL_ALLOC 200
127 #endif
128 
129 /* Stop parsing on first error (default is to keep parsing). */
130 #ifndef INI_STOP_ON_FIRST_ERROR
131 #define INI_STOP_ON_FIRST_ERROR 0
132 #endif
133 
134 /* Nonzero to call the handler at the start of each new section (with
135  name and value NULL). Default is to only call the handler on
136  each name=value pair. */
137 #ifndef INI_CALL_HANDLER_ON_NEW_SECTION
138 #define INI_CALL_HANDLER_ON_NEW_SECTION 0
139 #endif
140 
141 /* Nonzero to allow a name without a value (no '=' or ':' on the line) and
142  call the handler with value NULL in this case. Default is to treat
143  no-value lines as an error. */
144 #ifndef INI_ALLOW_NO_VALUE
145 #define INI_ALLOW_NO_VALUE 0
146 #endif
147 
148 /* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory
149  allocation functions (INI_USE_STACK must also be 0). These functions must
150  have the same signatures as malloc/free/realloc and behave in a similar
151  way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */
152 #ifndef INI_CUSTOM_ALLOCATOR
153 #define INI_CUSTOM_ALLOCATOR 0
154 #endif
155 
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif /* INI_H */