|
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 |
34 | 37 |
|
35 | 38 | #include <cstdio> |
36 | 39 | #include <cctype> |
37 | 40 | #include <cstring> |
38 | 41 |
|
39 | 42 | #include "ini.h" |
40 | 43 |
|
41 | | -#define MAX_LINE 200 |
| 44 | +#if !INI_USE_STACK |
| 45 | +#include <cstdlib> |
| 46 | +#endif |
| 47 | + |
42 | 48 | #define MAX_SECTION 50 |
43 | 49 | #define MAX_NAME 50 |
44 | 50 |
|
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) |
51 | 53 | { |
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))) |
54 | 56 | *p = '\0'; |
55 | 57 | return s; |
56 | 58 | } |
57 | 59 |
|
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) |
62 | 62 | { |
63 | | - while (*s && isspace(*s)) |
| 63 | + while (*s && isspace((unsigned char)(*s))) |
64 | 64 | s++; |
65 | 65 | return (char*)s; |
66 | 66 | } |
67 | 67 |
|
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) |
74 | 72 | { |
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))) { |
78 | 82 | s++; |
79 | 83 | } |
| 84 | +#endif |
80 | 85 | return (char*)s; |
81 | 86 | } |
82 | 87 |
|
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. */ |
86 | 89 | static char* strncpy0(char* dest, const char* src, size_t size) |
87 | 90 | { |
88 | 91 | strncpy(dest, src, size); |
89 | 92 | dest[size - 1] = '\0'; |
90 | 93 | return dest; |
91 | 94 | } |
92 | 95 |
|
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) |
97 | 99 | { |
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 |
100 | 106 | char section[MAX_SECTION] = ""; |
101 | 107 | char prev_name[MAX_NAME] = ""; |
102 | 108 |
|
103 | | - FILE* file; |
104 | 109 | char* start; |
105 | 110 | char* end; |
106 | 111 | char* name; |
107 | 112 | char* value; |
108 | 113 | int lineno = 0; |
109 | 114 | int error = 0; |
110 | 115 |
|
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 |
114 | 128 |
|
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) { |
117 | 131 | lineno++; |
118 | | - start = lskip(rstrip((unsigned char*)line)); |
119 | 132 |
|
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; |
126 | 139 | } |
127 | | - else |
128 | 140 | #endif |
| 141 | + start = lskip(rstrip(start)); |
| 142 | + |
129 | 143 | 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; |
131 | 153 | } |
| 154 | +#endif |
132 | 155 | 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, "]"); |
135 | 158 | if (*end == ']') { |
136 | 159 | *end = '\0'; |
137 | 160 | strncpy0(section, start + 1, sizeof(section)); |
138 | 161 | *prev_name = '\0'; |
139 | 162 | } |
140 | 163 | else if (!error) { |
141 | | - // No ']' found on section line |
| 164 | + /* No ']' found on section line */ |
142 | 165 | error = lineno; |
143 | 166 | } |
144 | 167 | } |
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 == ':') { |
149 | 172 | *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) |
154 | 178 | *end = '\0'; |
155 | | - rstrip((unsigned char*)value); |
| 179 | +#endif |
| 180 | + value = lskip(value); |
| 181 | + rstrip(value); |
156 | 182 |
|
157 | | - // Valid name=value pair found, call handler |
| 183 | + /* Valid name[=:]value pair found, call handler */ |
158 | 184 | strncpy0(prev_name, name, sizeof(prev_name)); |
159 | | - if (!handler(user, section, name, value) && !error) |
| 185 | + if (!HANDLER(user, section, name, value) && !error) |
160 | 186 | error = lineno; |
161 | 187 | } |
162 | 188 | else if (!error) { |
163 | | - // No '=' found on name=value line |
| 189 | + /* No '=' or ':' found on name[=:]value line */ |
164 | 190 | error = lineno; |
165 | 191 | } |
166 | 192 | } |
| 193 | + |
| 194 | +#if INI_STOP_ON_FIRST_ERROR |
| 195 | + if (error) |
| 196 | + break; |
| 197 | +#endif |
167 | 198 | } |
168 | 199 |
|
169 | | - fclose(file); |
| 200 | +#if !INI_USE_STACK |
| 201 | + free(line); |
| 202 | +#endif |
170 | 203 |
|
171 | 204 | return error; |
172 | 205 | } |
| 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