liblcf
ini.h
Go to the documentation of this file.
1 /* inih -- simple .INI file parser
2 
3 The "inih" library is distributed under the New BSD license:
4 
5 Copyright (c) 2009, Ben Hoyt
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of Ben Hoyt nor the names of its contributors
16  may be used to endorse or promote products derived from this software
17  without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY BEN HOYT ''AS IS'' AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL BEN HOYT BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 Go to the project home page for more info: https://github.com/benhoyt/inih
31 
32 */
33 
34 #ifndef LCF_INI_H
35 #define LCF_INI_H
36 
37 /* Make this header file easier to include in C++ code */
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include <stdio.h>
43 
44 /* Nonzero if ini_handler callback should accept lineno parameter. */
45 #ifndef INI_HANDLER_LINENO
46 #define INI_HANDLER_LINENO 0
47 #endif
48 
49 /* Typedef for prototype of handler function. */
50 #if INI_HANDLER_LINENO
51 typedef int (*ini_handler)(void* user, const char* section,
52  const char* name, const char* value,
53  int lineno);
54 #else
55 typedef int (*ini_handler)(void* user, const char* section,
56  const char* name, const char* value);
57 #endif
58 
59 /* Typedef for prototype of fgets-style reader function. */
60 typedef char* (*ini_reader)(char* str, int num, void* stream);
61 
62 /* Parse given INI-style file. May have [section]s, name=value pairs
63  (whitespace stripped), and comments starting with ';' (semicolon). Section
64  is "" if name=value pair parsed before any section heading. name:value
65  pairs are also supported as a concession to Python's configparser.
66 
67  For each name=value pair parsed, call handler function with given user
68  pointer as well as section, name, and value (data only valid for duration
69  of handler call). Handler should return nonzero on success, zero on error.
70 
71  Returns 0 on success, line number of first error on parse error (doesn't
72  stop on first error), -1 on file open error, or -2 on memory allocation
73  error (only when INI_USE_STACK is zero).
74 */
75 int ini_parse(const char* filename, ini_handler handler, void* user);
76 
77 /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
78  close the file when it's finished -- the caller must do that. */
79 int ini_parse_file(FILE* file, ini_handler handler, void* user);
80 
81 /* Same as ini_parse(), but takes an ini_reader function pointer instead of
82  filename. Used for implementing custom or string-based I/O (see also
83  ini_parse_string). */
84 int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
85  void* user);
86 
87 /* Same as ini_parse(), but takes a zero-terminated string with the INI data
88  instead of a file. Useful for parsing INI data from a network socket or
89  already in memory. */
90 int ini_parse_string(const char* string, ini_handler handler, void* user);
91 
92 /* Nonzero to allow multi-line value parsing, in the style of Python's
93  configparser. If allowed, ini_parse() will call the handler with the same
94  name for each subsequent line parsed. */
95 #ifndef INI_ALLOW_MULTILINE
96 #define INI_ALLOW_MULTILINE 1
97 #endif
98 
99 /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
100  the file. See https://github.com/benhoyt/inih/issues/21 */
101 #ifndef INI_ALLOW_BOM
102 #define INI_ALLOW_BOM 1
103 #endif
104 
105 /* Chars that begin a start-of-line comment. Per Python configparser, allow
106  both ; and # comments at the start of a line by default. */
107 #ifndef INI_START_COMMENT_PREFIXES
108 #define INI_START_COMMENT_PREFIXES ";#"
109 #endif
110 
111 /* Nonzero to allow inline comments (with valid inline comment characters
112  specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
113  Python 3.2+ configparser behaviour. */
114 #ifndef INI_ALLOW_INLINE_COMMENTS
115 #define INI_ALLOW_INLINE_COMMENTS 1
116 #endif
117 #ifndef INI_INLINE_COMMENT_PREFIXES
118 #define INI_INLINE_COMMENT_PREFIXES ";"
119 #endif
120 
121 /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
122 #ifndef INI_USE_STACK
123 #define INI_USE_STACK 1
124 #endif
125 
126 /* Maximum line length for any line in INI file (stack or heap). Note that
127  this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
128 #ifndef INI_MAX_LINE
129 #define INI_MAX_LINE 200
130 #endif
131 
132 /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
133  fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
134  zero. */
135 #ifndef INI_ALLOW_REALLOC
136 #define INI_ALLOW_REALLOC 0
137 #endif
138 
139 /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
140  is zero. */
141 #ifndef INI_INITIAL_ALLOC
142 #define INI_INITIAL_ALLOC 200
143 #endif
144 
145 /* Stop parsing on first error (default is to keep parsing). */
146 #ifndef INI_STOP_ON_FIRST_ERROR
147 #define INI_STOP_ON_FIRST_ERROR 0
148 #endif
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif
int ini_parse(const char *filename, ini_handler handler, void *user)
Definition: ini.cpp:245
char *(* ini_reader)(char *str, int num, void *stream)
Definition: ini.h:60
int(* ini_handler)(void *user, const char *section, const char *name, const char *value)
Definition: ini.h:55
int ini_parse_file(FILE *file, ini_handler handler, void *user)
Definition: ini.cpp:239
int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler, void *user)
Definition: ini.cpp:103
int ini_parse_string(const char *string, ini_handler handler, void *user)
Definition: ini.cpp:286