ini.h (5825B)
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 /* Visibility symbols, required for Windows DLLs */ 30 #ifndef INI_API 31 #if defined _WIN32 || defined __CYGWIN__ 32 # ifdef INI_SHARED_LIB 33 # ifdef INI_SHARED_LIB_BUILDING 34 # define INI_API __declspec(dllexport) 35 # else 36 # define INI_API __declspec(dllimport) 37 # endif 38 # else 39 # define INI_API 40 # endif 41 #else 42 # if defined(__GNUC__) && __GNUC__ >= 4 43 # define INI_API __attribute__ ((visibility ("default"))) 44 # else 45 # define INI_API 46 # endif 47 #endif 48 #endif 49 50 /* Typedef for prototype of handler function. */ 51 #if INI_HANDLER_LINENO 52 typedef int (*ini_handler)(void* user, const char* section, 53 const char* name, const char* value, 54 int lineno); 55 #else 56 typedef int (*ini_handler)(void* user, const char* section, 57 const char* name, const char* value); 58 #endif 59 60 /* Typedef for prototype of fgets-style reader function. */ 61 typedef char* (*ini_reader)(char* str, int num, void* stream); 62 63 /* Parse given INI-style file. May have [section]s, name=value pairs 64 (whitespace stripped), and comments starting with ';' (semicolon). Section 65 is "" if name=value pair parsed before any section heading. name:value 66 pairs are also supported as a concession to Python's configparser. 67 68 For each name=value pair parsed, call handler function with given user 69 pointer as well as section, name, and value (data only valid for duration 70 of handler call). Handler should return nonzero on success, zero on error. 71 72 Returns 0 on success, line number of first error on parse error (doesn't 73 stop on first error), -1 on file open error, or -2 on memory allocation 74 error (only when INI_USE_STACK is zero). 75 */ 76 INI_API int ini_parse(const char* filename, ini_handler handler, void* user); 77 78 /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't 79 close the file when it's finished -- the caller must do that. */ 80 INI_API int ini_parse_file(FILE* file, ini_handler handler, void* user); 81 82 /* Same as ini_parse(), but takes an ini_reader function pointer instead of 83 filename. Used for implementing custom or string-based I/O (see also 84 ini_parse_string). */ 85 INI_API int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, 86 void* user); 87 88 /* Same as ini_parse(), but takes a zero-terminated string with the INI data 89 instead of a file. Useful for parsing INI data from a network socket or 90 already in memory. */ 91 INI_API int ini_parse_string(const char* string, ini_handler handler, void* user); 92 93 /* Nonzero to allow multi-line value parsing, in the style of Python's 94 configparser. If allowed, ini_parse() will call the handler with the same 95 name for each subsequent line parsed. */ 96 #ifndef INI_ALLOW_MULTILINE 97 #define INI_ALLOW_MULTILINE 1 98 #endif 99 100 /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of 101 the file. See https://github.com/benhoyt/inih/issues/21 */ 102 #ifndef INI_ALLOW_BOM 103 #define INI_ALLOW_BOM 1 104 #endif 105 106 /* Chars that begin a start-of-line comment. Per Python configparser, allow 107 both ; and # comments at the start of a line by default. */ 108 #ifndef INI_START_COMMENT_PREFIXES 109 #define INI_START_COMMENT_PREFIXES ";#" 110 #endif 111 112 /* Nonzero to allow inline comments (with valid inline comment characters 113 specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match 114 Python 3.2+ configparser behaviour. */ 115 #ifndef INI_ALLOW_INLINE_COMMENTS 116 #define INI_ALLOW_INLINE_COMMENTS 1 117 #endif 118 #ifndef INI_INLINE_COMMENT_PREFIXES 119 #define INI_INLINE_COMMENT_PREFIXES ";" 120 #endif 121 122 /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */ 123 #ifndef INI_USE_STACK 124 #define INI_USE_STACK 1 125 #endif 126 127 /* Maximum line length for any line in INI file (stack or heap). Note that 128 this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */ 129 #ifndef INI_MAX_LINE 130 #define INI_MAX_LINE 200 131 #endif 132 133 /* Nonzero to allow heap line buffer to grow via realloc(), zero for a 134 fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is 135 zero. */ 136 #ifndef INI_ALLOW_REALLOC 137 #define INI_ALLOW_REALLOC 0 138 #endif 139 140 /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK 141 is zero. */ 142 #ifndef INI_INITIAL_ALLOC 143 #define INI_INITIAL_ALLOC 200 144 #endif 145 146 /* Stop parsing on first error (default is to keep parsing). */ 147 #ifndef INI_STOP_ON_FIRST_ERROR 148 #define INI_STOP_ON_FIRST_ERROR 0 149 #endif 150 151 /* Nonzero to call the handler at the start of each new section (with 152 name and value NULL). Default is to only call the handler on 153 each name=value pair. */ 154 #ifndef INI_CALL_HANDLER_ON_NEW_SECTION 155 #define INI_CALL_HANDLER_ON_NEW_SECTION 0 156 #endif 157 158 /* Nonzero to allow a name without a value (no '=' or ':' on the line) and 159 call the handler with value NULL in this case. Default is to treat 160 no-value lines as an error. */ 161 #ifndef INI_ALLOW_NO_VALUE 162 #define INI_ALLOW_NO_VALUE 0 163 #endif 164 165 /* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory 166 allocation functions (INI_USE_STACK must also be 0). These functions must 167 have the same signatures as malloc/free/realloc and behave in a similar 168 way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */ 169 #ifndef INI_CUSTOM_ALLOCATOR 170 #define INI_CUSTOM_ALLOCATOR 0 171 #endif 172 173 174 #ifdef __cplusplus 175 } 176 #endif 177 178 #endif /* INI_H */