Skip to content

Commit 3437ffc

Browse files
committed
1 parent eee3156 commit 3437ffc

4 files changed

Lines changed: 380 additions & 254 deletions

File tree

src/ini.cpp

Lines changed: 149 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,225 @@
1-
/*
2-
* inih -- simple .INI file parser
3-
*
4-
* Go to the project home page for more info:
5-
* http://code.google.com/p/inih/
6-
*
7-
* inih and INIReader are released under the New BSD license:
8-
*
9-
* Copyright (c) 2009, Brush Technology
10-
* All rights reserved.
11-
*
12-
* Redistribution and use in source and binary forms, with or without
13-
* modification, are permitted provided that the following conditions are met:
14-
* * Redistributions of source code must retain the above copyright
15-
* notice, this list of conditions and the following disclaimer.
16-
* * Redistributions in binary form must reproduce the above copyright
17-
* notice, this list of conditions and the following disclaimer in the
18-
* documentation and/or other materials provided with the distribution.
19-
* * Neither the name of Brush Technology nor the names of its contributors
20-
* may be used to endorse or promote products derived from this software
21-
* without specific prior written permission.
22-
*
23-
* THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
24-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26-
* DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
27-
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28-
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29-
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30-
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32-
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33-
*/
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+
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
35+
#define _CRT_SECURE_NO_WARNINGS
36+
#endif
3437

3538
#include <cstdio>
3639
#include <cctype>
3740
#include <cstring>
3841

3942
#include "ini.h"
4043

41-
#define MAX_LINE 200
44+
#if !INI_USE_STACK
45+
#include <cstdlib>
46+
#endif
47+
4248
#define MAX_SECTION 50
4349
#define MAX_NAME 50
4450

45-
/**
46-
* Strip whitespace chars off end of given string, in place.
47-
*
48-
* @returns s.
49-
*/
50-
static unsigned char* rstrip(unsigned char* s)
51+
/* Strip whitespace chars off end of given string, in place. Return s. */
52+
static char* rstrip(char* s)
5153
{
52-
unsigned char* p = s + strlen((char*)s);
53-
while (p > s && isspace(*--p))
54+
char* p = s + strlen(s);
55+
while (p > s && isspace((unsigned char)(*--p)))
5456
*p = '\0';
5557
return s;
5658
}
5759

58-
/**
59-
* @returns pointer to first non-whitespace char in given string.
60-
*/
61-
static char* lskip(const unsigned char* s)
60+
/* Return pointer to first non-whitespace char in given string. */
61+
static char* lskip(const char* s)
6262
{
63-
while (*s && isspace(*s))
63+
while (*s && isspace((unsigned char)(*s)))
6464
s++;
6565
return (char*)s;
6666
}
6767

68-
/**
69-
* @returns pointer to first char c or ';' comment in given string, or pointer
70-
* to null at end of string if neither found. ';' must be prefixed by
71-
* a whitespace character to register as a comment.
72-
*/
73-
static char* find_char_or_comment(const unsigned char* s, char c)
68+
/* Return pointer to first char (of chars) or inline comment in given string,
69+
or pointer to null at end of string if neither found. Inline comment must
70+
be prefixed by a whitespace character to register as a comment. */
71+
static char* find_chars_or_comment(const char* s, const char* chars)
7472
{
75-
int was_whitespace = 0;
76-
while (*s && *s != c && !(was_whitespace && *s == ';')) {
77-
was_whitespace = isspace(*s);
73+
#if INI_ALLOW_INLINE_COMMENTS
74+
int was_space = 0;
75+
while (*s && (!chars || !strchr(chars, *s)) &&
76+
!(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) {
77+
was_space = isspace((unsigned char)(*s));
78+
s++;
79+
}
80+
#else
81+
while (*s && (!chars || !strchr(chars, *s))) {
7882
s++;
7983
}
84+
#endif
8085
return (char*)s;
8186
}
8287

83-
/**
84-
* Version of strncpy that ensures dest (size bytes) is null-terminated.
85-
*/
88+
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
8689
static char* strncpy0(char* dest, const char* src, size_t size)
8790
{
8891
strncpy(dest, src, size);
8992
dest[size - 1] = '\0';
9093
return dest;
9194
}
9295

93-
// See documentation in header file.
94-
int ini_parse(const char* filename,
95-
int (*handler)(void*, const char*, const char*, const char*),
96-
void* user)
96+
/* See documentation in header file. */
97+
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
98+
void* user)
9799
{
98-
// Uses a fair bit of stack (use heap instead if you need to)
99-
char line[MAX_LINE];
100+
/* Uses a fair bit of stack (use heap instead if you need to) */
101+
#if INI_USE_STACK
102+
char line[INI_MAX_LINE];
103+
#else
104+
char* line;
105+
#endif
100106
char section[MAX_SECTION] = "";
101107
char prev_name[MAX_NAME] = "";
102108

103-
FILE* file;
104109
char* start;
105110
char* end;
106111
char* name;
107112
char* value;
108113
int lineno = 0;
109114
int error = 0;
110115

111-
file = fopen(filename, "r");
112-
if (!file)
113-
return -1;
116+
#if !INI_USE_STACK
117+
line = (char*)malloc(INI_MAX_LINE);
118+
if (!line) {
119+
return -2;
120+
}
121+
#endif
122+
123+
#if INI_HANDLER_LINENO
124+
#define HANDLER(u, s, n, v) handler(u, s, n, v, lineno)
125+
#else
126+
#define HANDLER(u, s, n, v) handler(u, s, n, v)
127+
#endif
114128

115-
// Scan through file line by line
116-
while (fgets(line, sizeof(line), file) != NULL) {
129+
/* Scan through stream line by line */
130+
while (reader(line, INI_MAX_LINE, stream) != NULL) {
117131
lineno++;
118-
start = lskip(rstrip((unsigned char*)line));
119132

120-
#if INI_ALLOW_MULTILINE
121-
if (*prev_name && *start && start > line) {
122-
// Non-black line with leading whitespace, treat as continuation
123-
// of previous name's value (as per Python ConfigParser).
124-
if (!handler(user, section, prev_name, start) && !error)
125-
error = lineno;
133+
start = line;
134+
#if INI_ALLOW_BOM
135+
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
136+
(unsigned char)start[1] == 0xBB &&
137+
(unsigned char)start[2] == 0xBF) {
138+
start += 3;
126139
}
127-
else
128140
#endif
141+
start = lskip(rstrip(start));
142+
129143
if (*start == ';' || *start == '#') {
130-
// Per Python ConfigParser, allow '#' comments at start of line
144+
/* Per Python configparser, allow both ; and # comments at the
145+
start of a line */
146+
}
147+
#if INI_ALLOW_MULTILINE
148+
else if (*prev_name && *start && start > line) {
149+
/* Non-blank line with leading whitespace, treat as continuation
150+
of previous name's value (as per Python configparser). */
151+
if (!HANDLER(user, section, prev_name, start) && !error)
152+
error = lineno;
131153
}
154+
#endif
132155
else if (*start == '[') {
133-
// A "[section]" line
134-
end = find_char_or_comment((unsigned char*)start + 1, ']');
156+
/* A "[section]" line */
157+
end = find_chars_or_comment(start + 1, "]");
135158
if (*end == ']') {
136159
*end = '\0';
137160
strncpy0(section, start + 1, sizeof(section));
138161
*prev_name = '\0';
139162
}
140163
else if (!error) {
141-
// No ']' found on section line
164+
/* No ']' found on section line */
142165
error = lineno;
143166
}
144167
}
145-
else if (*start && *start != ';') {
146-
// Not a comment, must be a name=value pair
147-
end = find_char_or_comment((unsigned char*)start, '=');
148-
if (*end == '=') {
168+
else if (*start) {
169+
/* Not a comment, must be a name[=:]value pair */
170+
end = find_chars_or_comment(start, "=:");
171+
if (*end == '=' || *end == ':') {
149172
*end = '\0';
150-
name = (char*)rstrip((unsigned char*)start);
151-
value = lskip((unsigned char*)end + 1);
152-
end = find_char_or_comment((unsigned char*)value, '\0');
153-
if (*end == ';')
173+
name = rstrip(start);
174+
value = end + 1;
175+
#if INI_ALLOW_INLINE_COMMENTS
176+
end = find_chars_or_comment(value, NULL);
177+
if (*end)
154178
*end = '\0';
155-
rstrip((unsigned char*)value);
179+
#endif
180+
value = lskip(value);
181+
rstrip(value);
156182

157-
// Valid name=value pair found, call handler
183+
/* Valid name[=:]value pair found, call handler */
158184
strncpy0(prev_name, name, sizeof(prev_name));
159-
if (!handler(user, section, name, value) && !error)
185+
if (!HANDLER(user, section, name, value) && !error)
160186
error = lineno;
161187
}
162188
else if (!error) {
163-
// No '=' found on name=value line
189+
/* No '=' or ':' found on name[=:]value line */
164190
error = lineno;
165191
}
166192
}
193+
194+
#if INI_STOP_ON_FIRST_ERROR
195+
if (error)
196+
break;
197+
#endif
167198
}
168199

169-
fclose(file);
200+
#if !INI_USE_STACK
201+
free(line);
202+
#endif
170203

171204
return error;
172205
}
206+
207+
/* See documentation in header file. */
208+
int ini_parse_file(FILE* file, ini_handler handler, void* user)
209+
{
210+
return ini_parse_stream((ini_reader)fgets, file, handler, user);
211+
}
212+
213+
/* See documentation in header file. */
214+
int ini_parse(const char* filename, ini_handler handler, void* user)
215+
{
216+
FILE* file;
217+
int error;
218+
219+
file = fopen(filename, "r");
220+
if (!file)
221+
return -1;
222+
error = ini_parse_file(file, handler, user);
223+
fclose(file);
224+
return error;
225+
}

0 commit comments

Comments
 (0)