src/third-party/inih/ini.h (view raw)
1/* inih -- simple .INI file parser
2
3SPDX-License-Identifier: BSD-3-Clause
4
5Copyright (C) 2009-2020, Ben Hoyt
6
7inih is released under the New BSD license (see LICENSE.txt). Go to the project
8home page for more info:
9
10https://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
19extern "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
31typedef int (*ini_handler)(void* user, const char* section,
32 const char* name, const char* value,
33 int lineno);
34#else
35typedef 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. */
40typedef 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*/
55int 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. */
59int 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). */
64int 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
68instead of a file. Useful for parsing INI data from a network socket or
69already in memory. */
70int ini_parse_string(const char* string, ini_handler handler, void* user);
71
72/* Nonzero to allow multi-line value parsing, in the style of Python's
73 configparser. If allowed, ini_parse() will call the handler with the same
74 name for each subsequent line parsed. */
75#ifndef INI_ALLOW_MULTILINE
76#define INI_ALLOW_MULTILINE 1
77#endif
78
79/* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
80 the file. See https://github.com/benhoyt/inih/issues/21 */
81#ifndef INI_ALLOW_BOM
82#define INI_ALLOW_BOM 1
83#endif
84
85/* Chars that begin a start-of-line comment. Per Python configparser, allow
86 both ; and # comments at the start of a line by default. */
87#ifndef INI_START_COMMENT_PREFIXES
88#define INI_START_COMMENT_PREFIXES ";#"
89#endif
90
91/* Nonzero to allow inline comments (with valid inline comment characters
92 specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
93 Python 3.2+ configparser behaviour. */
94#ifndef INI_ALLOW_INLINE_COMMENTS
95#define INI_ALLOW_INLINE_COMMENTS 1
96#endif
97#ifndef INI_INLINE_COMMENT_PREFIXES
98#define INI_INLINE_COMMENT_PREFIXES ";"
99#endif
100
101/* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
102#ifndef INI_USE_STACK
103#define INI_USE_STACK 1
104#endif
105
106/* Maximum line length for any line in INI file (stack or heap). Note that
107 this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
108#ifndef INI_MAX_LINE
109#define INI_MAX_LINE 200
110#endif
111
112/* Nonzero to allow heap line buffer to grow via realloc(), zero for a
113 fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
114 zero. */
115#ifndef INI_ALLOW_REALLOC
116#define INI_ALLOW_REALLOC 0
117#endif
118
119/* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
120 is zero. */
121#ifndef INI_INITIAL_ALLOC
122#define INI_INITIAL_ALLOC 200
123#endif
124
125/* Stop parsing on first error (default is to keep parsing). */
126#ifndef INI_STOP_ON_FIRST_ERROR
127#define INI_STOP_ON_FIRST_ERROR 0
128#endif
129
130/* Nonzero to call the handler at the start of each new section (with
131 name and value NULL). Default is to only call the handler on
132 each name=value pair. */
133#ifndef INI_CALL_HANDLER_ON_NEW_SECTION
134#define INI_CALL_HANDLER_ON_NEW_SECTION 0
135#endif
136
137/* Nonzero to allow a name without a value (no '=' or ':' on the line) and
138 call the handler with value NULL in this case. Default is to treat
139 no-value lines as an error. */
140#ifndef INI_ALLOW_NO_VALUE
141#define INI_ALLOW_NO_VALUE 0
142#endif
143
144#ifdef __cplusplus
145}
146#endif
147
148#endif /* __INI_H__ */