Skip to content

Commit

Permalink
Update ini.[ch]
Browse files Browse the repository at this point in the history
  • Loading branch information
overtaker committed Jun 8, 2020
1 parent eef5ae7 commit 5c2cde7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
26 changes: 21 additions & 5 deletions ExternData/Resources/C-Sources/inih/ini.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* inih -- simple .INI file parser
SPDX-License-Identifier: BSD-3-Clause
Copyright (C) 2009-2020, Ben Hoyt
inih is released under the New BSD license (see LICENSE.txt). Go to the project
home page for more info:
Expand Down Expand Up @@ -70,7 +74,8 @@ static char* find_chars_or_comment(const char* s, const char* chars)
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
static char* strncpy0(char* dest, const char* src, size_t size)
{
strncpy(dest, src, size - 1);
/* Use memcpy instead of strncpy to avoid gcc warnings (see issue #91) */
memcpy(dest, src, size - 1);
dest[size - 1] = '\0';
return dest;
}
Expand All @@ -85,11 +90,11 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
int max_line = INI_MAX_LINE;
#else
char* line;
int max_line = INI_INITIAL_ALLOC;
size_t max_line = INI_INITIAL_ALLOC;
#endif
#if INI_ALLOW_REALLOC && !INI_USE_STACK
char* new_line;
int offset;
size_t offset;
#endif
char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = "";
Expand All @@ -115,7 +120,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
#endif

/* Scan through stream line by line */
while (reader(line, max_line, stream) != NULL) {
while (reader(line, (int)max_line, stream) != NULL) {
#if INI_ALLOW_REALLOC && !INI_USE_STACK
offset = strlen(line);
while (offset == max_line - 1 && line[offset - 1] != '\n') {
Expand All @@ -128,7 +133,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
return -2;
}
line = new_line;
if (reader(line + offset, max_line - offset, stream) == NULL)
if (reader(line + offset, (int)(max_line - offset), stream) == NULL)
break;
if (max_line >= INI_MAX_LINE)
break;
Expand Down Expand Up @@ -166,6 +171,10 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
*end = '\0';
strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0';
#if INI_CALL_HANDLER_ON_NEW_SECTION
if (!HANDLER(user, section, NULL, NULL) && !error)
error = lineno;
#endif
}
else if (!error) {
/* No ']' found on section line */
Expand Down Expand Up @@ -194,7 +203,14 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
}
else if (!error) {
/* No '=' or ':' found on name[=:]value line */
#if INI_ALLOW_NO_VALUE
*end = '\0';
name = rstrip(start);
if (!HANDLER(user, section, name, NULL) && !error)
error = lineno;
#else
error = lineno;
#endif
}
}

Expand Down
28 changes: 23 additions & 5 deletions ExternData/Resources/C-Sources/inih/ini.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* inih -- simple .INI file parser
SPDX-License-Identifier: BSD-3-Clause
Copyright (C) 2009-2020, Ben Hoyt
inih is released under the New BSD license (see LICENSE.txt). Go to the project
home page for more info:
Expand Down Expand Up @@ -81,7 +85,7 @@ int ini_parse_string(const char* string, ini_handler handler, void* user);
/* Chars that begin a start-of-line comment. Per Python configparser, allow
both ; and # comments at the start of a line by default. */
#ifndef INI_START_COMMENT_PREFIXES
#define INI_START_COMMENT_PREFIXES ";#!$"
#define INI_START_COMMENT_PREFIXES ";#"
#endif

/* Nonzero to allow inline comments (with valid inline comment characters
Expand All @@ -96,33 +100,47 @@ int ini_parse_string(const char* string, ini_handler handler, void* user);

/* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
#ifndef INI_USE_STACK
#define INI_USE_STACK 0
#define INI_USE_STACK 1
#endif

/* Maximum line length for any line in INI file (stack or heap). Note that
this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
#ifndef INI_MAX_LINE
#define INI_MAX_LINE 512
#define INI_MAX_LINE 200
#endif

/* Nonzero to allow heap line buffer to grow via realloc(), zero for a
fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
zero. */
#ifndef INI_ALLOW_REALLOC
#define INI_ALLOW_REALLOC 1
#define INI_ALLOW_REALLOC 0
#endif

/* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
is zero. */
#ifndef INI_INITIAL_ALLOC
#define INI_INITIAL_ALLOC 128
#define INI_INITIAL_ALLOC 200
#endif

/* Stop parsing on first error (default is to keep parsing). */
#ifndef INI_STOP_ON_FIRST_ERROR
#define INI_STOP_ON_FIRST_ERROR 0
#endif

/* Nonzero to call the handler at the start of each new section (with
name and value NULL). Default is to only call the handler on
each name=value pair. */
#ifndef INI_CALL_HANDLER_ON_NEW_SECTION
#define INI_CALL_HANDLER_ON_NEW_SECTION 0
#endif

/* Nonzero to allow a name without a value (no '=' or ':' on the line) and
call the handler with value NULL in this case. Default is to treat
no-value lines as an error. */
#ifndef INI_ALLOW_NO_VALUE
#define INI_ALLOW_NO_VALUE 0
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 5c2cde7

Please sign in to comment.