|
| 1 | +/* inih -- simple .INI file parser |
| 2 | +
|
| 3 | +SPDX-License-Identifier: BSD-3-Clause |
| 4 | +
|
| 5 | +Copyright (C) 2009-2025, 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 | +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| 15 | +#define _CRT_SECURE_NO_WARNINGS |
| 16 | +#endif |
| 17 | + |
| 18 | +#include <stdio.h> |
| 19 | +#include <ctype.h> |
| 20 | +#include <string.h> |
| 21 | +#include <assert.h> |
| 22 | + |
| 23 | +#include "ini.h" |
| 24 | + |
| 25 | +#if !INI_USE_STACK |
| 26 | +#if INI_CUSTOM_ALLOCATOR |
| 27 | +#include <stddef.h> |
| 28 | +void* ini_malloc(size_t size); |
| 29 | +void ini_free(void* ptr); |
| 30 | +void* ini_realloc(void* ptr, size_t size); |
| 31 | +#else |
| 32 | +#include <stdlib.h> |
| 33 | +#define ini_malloc malloc |
| 34 | +#define ini_free free |
| 35 | +#define ini_realloc realloc |
| 36 | +#endif |
| 37 | +#endif |
| 38 | + |
| 39 | +#define MAX_SECTION 50 |
| 40 | +#define MAX_NAME 50 |
| 41 | + |
| 42 | +/* Used by ini_parse_string() to keep track of string parsing state. */ |
| 43 | +typedef struct { |
| 44 | + const char* ptr; |
| 45 | + size_t num_left; |
| 46 | +} ini_parse_string_ctx; |
| 47 | + |
| 48 | +/* Strip whitespace chars off end of given string, in place. end must be a |
| 49 | + pointer to the NUL terminator at the end of the string. Return s. */ |
| 50 | +static char* ini_rstrip(char* s, char* end) |
| 51 | +{ |
| 52 | + while (end > s && isspace((unsigned char)(*--end))) |
| 53 | + *end = '\0'; |
| 54 | + return s; |
| 55 | +} |
| 56 | + |
| 57 | +/* Return pointer to first non-whitespace char in given string. */ |
| 58 | +static char* ini_lskip(const char* s) |
| 59 | +{ |
| 60 | + while (*s && isspace((unsigned char)(*s))) |
| 61 | + s++; |
| 62 | + return (char*)s; |
| 63 | +} |
| 64 | + |
| 65 | +/* Return pointer to first char (of chars) or inline comment in given string, |
| 66 | + or pointer to NUL at end of string if neither found. Inline comment must |
| 67 | + be prefixed by a whitespace character to register as a comment. */ |
| 68 | +static char* ini_find_chars_or_comment(const char* s, const char* chars) |
| 69 | +{ |
| 70 | +#if INI_ALLOW_INLINE_COMMENTS |
| 71 | + int was_space = 0; |
| 72 | + while (*s && (!chars || !strchr(chars, *s)) && |
| 73 | + !(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) { |
| 74 | + was_space = isspace((unsigned char)(*s)); |
| 75 | + s++; |
| 76 | + } |
| 77 | +#else |
| 78 | + while (*s && (!chars || !strchr(chars, *s))) { |
| 79 | + s++; |
| 80 | + } |
| 81 | +#endif |
| 82 | + return (char*)s; |
| 83 | +} |
| 84 | + |
| 85 | +/* Similar to strncpy, but ensures dest (size bytes) is |
| 86 | + NUL-terminated, and doesn't pad with NULs. */ |
| 87 | +static char* ini_strncpy0(char* dest, const char* src, size_t size) |
| 88 | +{ |
| 89 | + /* Could use strncpy internally, but it causes gcc warnings (see issue #91) */ |
| 90 | + size_t i; |
| 91 | + for (i = 0; i < size - 1 && src[i]; i++) |
| 92 | + dest[i] = src[i]; |
| 93 | + dest[i] = '\0'; |
| 94 | + return dest; |
| 95 | +} |
| 96 | + |
| 97 | +/* See documentation in header file. */ |
| 98 | +int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, |
| 99 | + void* user) |
| 100 | +{ |
| 101 | + /* Uses a fair bit of stack (use heap instead if you need to) */ |
| 102 | +#if INI_USE_STACK |
| 103 | + char line[INI_MAX_LINE]; |
| 104 | + size_t max_line = INI_MAX_LINE; |
| 105 | +#else |
| 106 | + char* line; |
| 107 | + size_t max_line = INI_INITIAL_ALLOC; |
| 108 | +#endif |
| 109 | +#if INI_ALLOW_REALLOC && !INI_USE_STACK |
| 110 | + char* new_line; |
| 111 | +#endif |
| 112 | + char section[MAX_SECTION] = ""; |
| 113 | +#if INI_ALLOW_MULTILINE |
| 114 | + char prev_name[MAX_NAME] = ""; |
| 115 | +#endif |
| 116 | + |
| 117 | + size_t offset; |
| 118 | + char* start; |
| 119 | + char* end; |
| 120 | + char* name; |
| 121 | + char* value; |
| 122 | + int lineno = 0; |
| 123 | + int error = 0; |
| 124 | + char abyss[16]; /* Used to consume input when a line is too long. */ |
| 125 | + size_t abyss_len; |
| 126 | + |
| 127 | + assert(reader != NULL); |
| 128 | + assert(stream != NULL); |
| 129 | + assert(handler != NULL); |
| 130 | + |
| 131 | +#if !INI_USE_STACK |
| 132 | + line = (char*)ini_malloc(INI_INITIAL_ALLOC); |
| 133 | + if (!line) { |
| 134 | + return -2; |
| 135 | + } |
| 136 | +#endif |
| 137 | + |
| 138 | +#if INI_HANDLER_LINENO |
| 139 | +#define HANDLER(u, s, n, v) handler(u, s, n, v, lineno) |
| 140 | +#else |
| 141 | +#define HANDLER(u, s, n, v) handler(u, s, n, v) |
| 142 | +#endif |
| 143 | + |
| 144 | + /* Scan through stream line by line */ |
| 145 | + while (reader(line, (int)max_line, stream) != NULL) { |
| 146 | + offset = strlen(line); |
| 147 | + |
| 148 | +#if INI_ALLOW_REALLOC && !INI_USE_STACK |
| 149 | + while (max_line < INI_MAX_LINE && |
| 150 | + offset == max_line - 1 && line[offset - 1] != '\n') { |
| 151 | + max_line *= 2; |
| 152 | + if (max_line > INI_MAX_LINE) |
| 153 | + max_line = INI_MAX_LINE; |
| 154 | + new_line = ini_realloc(line, max_line); |
| 155 | + if (!new_line) { |
| 156 | + ini_free(line); |
| 157 | + return -2; |
| 158 | + } |
| 159 | + line = new_line; |
| 160 | + if (reader(line + offset, (int)(max_line - offset), stream) == NULL) |
| 161 | + break; |
| 162 | + offset += strlen(line + offset); |
| 163 | + } |
| 164 | +#endif |
| 165 | + |
| 166 | + lineno++; |
| 167 | + |
| 168 | + /* If line exceeded INI_MAX_LINE bytes, discard till end of line. */ |
| 169 | + if (offset == max_line - 1 && line[offset - 1] != '\n') { |
| 170 | + while (reader(abyss, sizeof(abyss), stream) != NULL) { |
| 171 | + if (!error) |
| 172 | + error = lineno; |
| 173 | + abyss_len = strlen(abyss); |
| 174 | + if (abyss_len > 0 && abyss[abyss_len - 1] == '\n') |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + start = line; |
| 180 | +#if INI_ALLOW_BOM |
| 181 | + if (lineno == 1 && (unsigned char)start[0] == 0xEF && |
| 182 | + (unsigned char)start[1] == 0xBB && |
| 183 | + (unsigned char)start[2] == 0xBF) { |
| 184 | + start += 3; |
| 185 | + } |
| 186 | +#endif |
| 187 | + start = ini_rstrip(ini_lskip(start), line + offset); |
| 188 | + |
| 189 | + if (strchr(INI_START_COMMENT_PREFIXES, *start)) { |
| 190 | + /* Start-of-line comment */ |
| 191 | + } |
| 192 | +#if INI_ALLOW_MULTILINE |
| 193 | + else if (*prev_name && *start && start > line) { |
| 194 | +#if INI_ALLOW_INLINE_COMMENTS |
| 195 | + end = ini_find_chars_or_comment(start, NULL); |
| 196 | + *end = '\0'; |
| 197 | + ini_rstrip(start, end); |
| 198 | +#endif |
| 199 | + /* Non-blank line with leading whitespace, treat as continuation |
| 200 | + of previous name's value (as per Python configparser). */ |
| 201 | + if (!HANDLER(user, section, prev_name, start) && !error) |
| 202 | + error = lineno; |
| 203 | + } |
| 204 | +#endif |
| 205 | + else if (*start == '[') { |
| 206 | + /* A "[section]" line */ |
| 207 | + end = ini_find_chars_or_comment(start + 1, "]"); |
| 208 | + if (*end == ']') { |
| 209 | + *end = '\0'; |
| 210 | + ini_strncpy0(section, start + 1, sizeof(section)); |
| 211 | +#if INI_ALLOW_MULTILINE |
| 212 | + *prev_name = '\0'; |
| 213 | +#endif |
| 214 | +#if INI_CALL_HANDLER_ON_NEW_SECTION |
| 215 | + if (!HANDLER(user, section, NULL, NULL) && !error) |
| 216 | + error = lineno; |
| 217 | +#endif |
| 218 | + } |
| 219 | + else if (!error) { |
| 220 | + /* No ']' found on section line */ |
| 221 | + error = lineno; |
| 222 | + } |
| 223 | + } |
| 224 | + else if (*start) { |
| 225 | + /* Not a comment, must be a name[=:]value pair */ |
| 226 | + end = ini_find_chars_or_comment(start, "=:"); |
| 227 | + if (*end == '=' || *end == ':') { |
| 228 | + *end = '\0'; |
| 229 | + name = ini_rstrip(start, end); |
| 230 | + value = end + 1; |
| 231 | +#if INI_ALLOW_INLINE_COMMENTS |
| 232 | + end = ini_find_chars_or_comment(value, NULL); |
| 233 | + *end = '\0'; |
| 234 | +#endif |
| 235 | + value = ini_lskip(value); |
| 236 | + ini_rstrip(value, end); |
| 237 | + |
| 238 | +#if INI_ALLOW_MULTILINE |
| 239 | + ini_strncpy0(prev_name, name, sizeof(prev_name)); |
| 240 | +#endif |
| 241 | + /* Valid name[=:]value pair found, call handler */ |
| 242 | + if (!HANDLER(user, section, name, value) && !error) |
| 243 | + error = lineno; |
| 244 | + } |
| 245 | + else { |
| 246 | + /* No '=' or ':' found on name[=:]value line */ |
| 247 | +#if INI_ALLOW_NO_VALUE |
| 248 | + *end = '\0'; |
| 249 | + name = ini_rstrip(start, end); |
| 250 | + if (!HANDLER(user, section, name, NULL) && !error) |
| 251 | + error = lineno; |
| 252 | +#else |
| 253 | + if (!error) |
| 254 | + error = lineno; |
| 255 | +#endif |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | +#if INI_STOP_ON_FIRST_ERROR |
| 260 | + if (error) |
| 261 | + break; |
| 262 | +#endif |
| 263 | + } |
| 264 | + |
| 265 | +#if !INI_USE_STACK |
| 266 | + ini_free(line); |
| 267 | +#endif |
| 268 | + |
| 269 | + return error; |
| 270 | +} |
| 271 | + |
| 272 | +/* See documentation in header file. */ |
| 273 | +int ini_parse_file(FILE* file, ini_handler handler, void* user) |
| 274 | +{ |
| 275 | + return ini_parse_stream((ini_reader)fgets, file, handler, user); |
| 276 | +} |
| 277 | + |
| 278 | +/* See documentation in header file. */ |
| 279 | +int ini_parse(const char* filename, ini_handler handler, void* user) |
| 280 | +{ |
| 281 | + FILE* file; |
| 282 | + int error; |
| 283 | + |
| 284 | + file = fopen(filename, "r"); |
| 285 | + if (!file) |
| 286 | + return -1; |
| 287 | + error = ini_parse_file(file, handler, user); |
| 288 | + fclose(file); |
| 289 | + return error; |
| 290 | +} |
| 291 | + |
| 292 | +/* An ini_reader function to read the next line from a string buffer. This |
| 293 | + is the fgets() equivalent used by ini_parse_string(). */ |
| 294 | +static char* ini_reader_string(char* str, int num, void* stream) { |
| 295 | + ini_parse_string_ctx* ctx = (ini_parse_string_ctx*)stream; |
| 296 | + const char* ctx_ptr = ctx->ptr; |
| 297 | + size_t ctx_num_left = ctx->num_left; |
| 298 | + char* strp = str; |
| 299 | + char c; |
| 300 | + |
| 301 | + if (ctx_num_left == 0 || num < 2) |
| 302 | + return NULL; |
| 303 | + |
| 304 | + while (num > 1 && ctx_num_left != 0) { |
| 305 | + c = *ctx_ptr++; |
| 306 | + ctx_num_left--; |
| 307 | + *strp++ = c; |
| 308 | + if (c == '\n') |
| 309 | + break; |
| 310 | + num--; |
| 311 | + } |
| 312 | + |
| 313 | + *strp = '\0'; |
| 314 | + ctx->ptr = ctx_ptr; |
| 315 | + ctx->num_left = ctx_num_left; |
| 316 | + return str; |
| 317 | +} |
| 318 | + |
| 319 | +/* See documentation in header file. */ |
| 320 | +int ini_parse_string(const char* string, ini_handler handler, void* user) { |
| 321 | + return ini_parse_string_length(string, strlen(string), handler, user); |
| 322 | +} |
| 323 | + |
| 324 | +/* See documentation in header file. */ |
| 325 | +int ini_parse_string_length(const char* string, size_t length, |
| 326 | + ini_handler handler, void* user) { |
| 327 | + ini_parse_string_ctx ctx; |
| 328 | + |
| 329 | + ctx.ptr = string; |
| 330 | + ctx.num_left = length; |
| 331 | + return ini_parse_stream((ini_reader)ini_reader_string, &ctx, handler, |
| 332 | + user); |
| 333 | +} |
0 commit comments