2 * inih -- simple .INI file parser
4 * The "inih" library is distributed under the New BSD license:
6 * Copyright (C) 2009 Brush Technology - All rights reserved.
8 * SPDX-License-Identifier: BSD-3-Clause
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * * Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * * Neither the name of Brush Technology nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
25 * EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * http://code.google.com/p/inih/
39 #include <common/common.h>
47 #define MAX_SECTION 50
50 /* Strip whitespace chars off end of given string, in place. Return s. */
51 static char* rstrip(char* s
)
53 char* p
= s
+ strlen(s
);
55 while (p
> s
&& isspace((unsigned char)(*--p
)))
60 /* Return pointer to first non-whitespace char in given string. */
61 static char* lskip(const char* s
)
63 while (*s
&& isspace((unsigned char)(*s
)))
69 * Return pointer to first char c or ';' comment in given string, or pointer to
70 * null at end of string if neither found. ';' must be prefixed by a whitespace
71 * character to register as a comment.
73 static char* find_char_or_comment(const char* s
, char c
)
75 int was_whitespace
= 0;
77 while (*s
&& *s
!= c
&& !(was_whitespace
&& *s
== ';')) {
78 was_whitespace
= isspace((unsigned char)(*s
));
84 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
85 static char* strncpy0(char* dest
, const char* src
, size_t size
)
87 strncpy(dest
, src
, size
- 1);
88 dest
[size
- 1] = '\0';
92 /* See documentation in header file. */
94 int ini_parse_file(FILE* file
, ini_entry_handler handler
, void* user
)
96 /* Uses a fair bit of stack (use heap instead if you need to) */
98 char line
[INI_MAX_LINE
];
102 char section
[MAX_SECTION
] = "";
103 char prev_name
[MAX_NAME
] = "";
113 line
= (char*)zmalloc(INI_MAX_LINE
);
119 /* Scan through file line by line */
120 while (fgets(line
, INI_MAX_LINE
, file
) != NULL
) {
125 if (lineno
== 1 && (unsigned char)start
[0] == 0xEF &&
126 (unsigned char)start
[1] == 0xBB &&
127 (unsigned char)start
[2] == 0xBF) {
131 start
= lskip(rstrip(start
));
133 if (*start
== ';' || *start
== '#') {
135 * Per Python ConfigParser, allow '#' comments at
139 #if INI_ALLOW_MULTILINE
140 else if (*prev_name
&& *start
&& start
> line
) {
141 /* Non-black line with leading whitespace, treat as
142 * continuation of previous name's value
143 * (as per Python ConfigParser).
145 if (handler(user
, section
, prev_name
, start
) < 0 &&
151 else if (*start
== '[') {
152 /* A "[section]" line */
153 end
= find_char_or_comment(start
+ 1, ']');
156 strncpy0(section
, start
+ 1, sizeof(section
));
160 /* No ']' found on section line */
164 else if (*start
&& *start
!= ';') {
165 /* Not a comment, must be a name[=:]value pair */
166 end
= find_char_or_comment(start
, '=');
168 end
= find_char_or_comment(start
, ':');
170 if (*end
== '=' || *end
== ':') {
172 name
= rstrip(start
);
173 value
= lskip(end
+ 1);
174 end
= find_char_or_comment(value
, '\0');
182 * Valid name[=:]value pair found, call
185 strncpy0(prev_name
, name
, sizeof(prev_name
));
186 if (handler(user
, section
, name
, value
) < 0 &&
192 /* No '=' or ':' found on name[=:]value line */
205 /* See documentation in header file. */
207 int ini_parse(const char* filename
, ini_entry_handler handler
, void* user
)
212 file
= fopen(filename
, "r");
217 error
= ini_parse_file(file
, handler
, user
);