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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Brush Technology nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
23 * EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * http://code.google.com/p/inih/
38 #include <common/common.h>
46 #define MAX_SECTION 50
49 /* Strip whitespace chars off end of given string, in place. Return s. */
50 static char* rstrip(char* s
)
52 char* p
= s
+ strlen(s
);
54 while (p
> s
&& isspace((unsigned char)(*--p
)))
59 /* Return pointer to first non-whitespace char in given string. */
60 static char* lskip(const char* s
)
62 while (*s
&& isspace((unsigned char)(*s
)))
68 * Return pointer to first char c or ';' comment in given string, or pointer to
69 * null at end of string if neither found. ';' must be prefixed by a whitespace
70 * character to register as a comment.
72 static char* find_char_or_comment(const char* s
, char c
)
74 int was_whitespace
= 0;
76 while (*s
&& *s
!= c
&& !(was_whitespace
&& *s
== ';')) {
77 was_whitespace
= isspace((unsigned char)(*s
));
83 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
84 static char* strncpy0(char* dest
, const char* src
, size_t size
)
86 strncpy(dest
, src
, size
);
87 dest
[size
- 1] = '\0';
91 /* See documentation in header file. */
93 int ini_parse_file(FILE* file
, ini_entry_handler handler
, void* user
)
95 /* Uses a fair bit of stack (use heap instead if you need to) */
97 char line
[INI_MAX_LINE
];
101 char section
[MAX_SECTION
] = "";
102 char prev_name
[MAX_NAME
] = "";
112 line
= (char*)zmalloc(INI_MAX_LINE
);
118 /* Scan through file line by line */
119 while (fgets(line
, INI_MAX_LINE
, file
) != NULL
) {
124 if (lineno
== 1 && (unsigned char)start
[0] == 0xEF &&
125 (unsigned char)start
[1] == 0xBB &&
126 (unsigned char)start
[2] == 0xBF) {
130 start
= lskip(rstrip(start
));
132 if (*start
== ';' || *start
== '#') {
134 * Per Python ConfigParser, allow '#' comments at
138 #if INI_ALLOW_MULTILINE
139 else if (*prev_name
&& *start
&& start
> line
) {
140 /* Non-black line with leading whitespace, treat as
141 * continuation of previous name's value
142 * (as per Python ConfigParser).
144 if (handler(user
, section
, prev_name
, start
) < 0 &&
150 else if (*start
== '[') {
151 /* A "[section]" line */
152 end
= find_char_or_comment(start
+ 1, ']');
155 strncpy0(section
, start
+ 1, sizeof(section
));
159 /* No ']' found on section line */
163 else if (*start
&& *start
!= ';') {
164 /* Not a comment, must be a name[=:]value pair */
165 end
= find_char_or_comment(start
, '=');
167 end
= find_char_or_comment(start
, ':');
169 if (*end
== '=' || *end
== ':') {
171 name
= rstrip(start
);
172 value
= lskip(end
+ 1);
173 end
= find_char_or_comment(value
, '\0');
181 * Valid name[=:]value pair found, call
184 strncpy0(prev_name
, name
, sizeof(prev_name
));
185 if (handler(user
, section
, name
, value
) < 0 &&
191 /* No '=' or ':' found on name[=:]value line */
204 /* See documentation in header file. */
206 int ini_parse(const char* filename
, ini_entry_handler handler
, void* user
)
211 file
= fopen(filename
, "r");
216 error
= ini_parse_file(file
, handler
, user
);