Commit | Line | Data |
---|---|---|
bac6245e JG |
1 | /* |
2 | * inih -- simple .INI file parser | |
3 | * | |
4 | * The "inih" library is distributed under the New BSD license: | |
5 | * | |
ab5be9fa MJ |
6 | * Copyright (C) 2009 Brush Technology - All rights reserved. |
7 | * | |
8 | * SPDX-License-Identifier: BSD-3-Clause | |
bac6245e JG |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions are met: | |
12 | * | |
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. | |
21 | * | |
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. | |
32 | * | |
33 | * http://code.google.com/p/inih/ | |
34 | */ | |
35 | ||
28ab034a JG |
36 | #include "ini.hpp" |
37 | ||
c9e313bc | 38 | #include <common/common.hpp> |
bac6245e | 39 | |
28ab034a JG |
40 | #include <ctype.h> |
41 | #include <stdio.h> | |
42 | #include <string.h> | |
bac6245e JG |
43 | |
44 | #if !INI_USE_STACK | |
45 | #include <stdlib.h> | |
46 | #endif | |
47 | ||
48 | #define MAX_SECTION 50 | |
28ab034a | 49 | #define MAX_NAME 50 |
bac6245e JG |
50 | |
51 | /* Strip whitespace chars off end of given string, in place. Return s. */ | |
28ab034a | 52 | static char *rstrip(char *s) |
bac6245e | 53 | { |
28ab034a | 54 | char *p = s + strlen(s); |
bac6245e | 55 | |
28ab034a | 56 | while (p > s && isspace((unsigned char) (*--p))) |
bac6245e JG |
57 | *p = '\0'; |
58 | return s; | |
59 | } | |
60 | ||
61 | /* Return pointer to first non-whitespace char in given string. */ | |
28ab034a | 62 | static char *lskip(const char *s) |
bac6245e | 63 | { |
28ab034a | 64 | while (*s && isspace((unsigned char) (*s))) |
bac6245e | 65 | s++; |
28ab034a | 66 | return (char *) s; |
bac6245e JG |
67 | } |
68 | ||
69 | /* | |
70 | * Return pointer to first char c or ';' comment in given string, or pointer to | |
71 | * null at end of string if neither found. ';' must be prefixed by a whitespace | |
72 | * character to register as a comment. | |
73 | */ | |
28ab034a | 74 | static char *find_char_or_comment(const char *s, char c) |
bac6245e JG |
75 | { |
76 | int was_whitespace = 0; | |
77 | ||
78 | while (*s && *s != c && !(was_whitespace && *s == ';')) { | |
28ab034a | 79 | was_whitespace = isspace((unsigned char) (*s)); |
bac6245e JG |
80 | s++; |
81 | } | |
28ab034a | 82 | return (char *) s; |
bac6245e JG |
83 | } |
84 | ||
85 | /* Version of strncpy that ensures dest (size bytes) is null-terminated. */ | |
28ab034a | 86 | static char *strncpy0(char *dest, const char *src, size_t size) |
bac6245e | 87 | { |
9ff0b09b | 88 | strncpy(dest, src, size - 1); |
bac6245e JG |
89 | dest[size - 1] = '\0'; |
90 | return dest; | |
91 | } | |
92 | ||
93 | /* See documentation in header file. */ | |
28ab034a | 94 | int ini_parse_file(FILE *file, ini_entry_handler handler, void *user) |
bac6245e JG |
95 | { |
96 | /* Uses a fair bit of stack (use heap instead if you need to) */ | |
97 | #if INI_USE_STACK | |
98 | char line[INI_MAX_LINE]; | |
99 | #else | |
28ab034a | 100 | char *line; |
bac6245e JG |
101 | #endif |
102 | char section[MAX_SECTION] = ""; | |
103 | char prev_name[MAX_NAME] = ""; | |
104 | ||
28ab034a JG |
105 | char *start; |
106 | char *end; | |
107 | char *name; | |
108 | char *value; | |
bac6245e JG |
109 | int lineno = 0; |
110 | int error = 0; | |
111 | ||
112 | #if !INI_USE_STACK | |
64803277 | 113 | line = zmalloc<char>(INI_MAX_LINE); |
bac6245e JG |
114 | if (!line) { |
115 | return -2; | |
116 | } | |
117 | #endif | |
118 | ||
119 | /* Scan through file line by line */ | |
cd9adb8b | 120 | while (fgets(line, INI_MAX_LINE, file) != nullptr) { |
bac6245e JG |
121 | lineno++; |
122 | ||
123 | start = line; | |
124 | #if INI_ALLOW_BOM | |
28ab034a JG |
125 | if (lineno == 1 && (unsigned char) start[0] == 0xEF && |
126 | (unsigned char) start[1] == 0xBB && (unsigned char) start[2] == 0xBF) { | |
bac6245e JG |
127 | start += 3; |
128 | } | |
129 | #endif | |
130 | start = lskip(rstrip(start)); | |
131 | ||
132 | if (*start == ';' || *start == '#') { | |
133 | /* | |
134 | * Per Python ConfigParser, allow '#' comments at | |
135 | * start of line. | |
136 | */ | |
137 | } | |
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). | |
143 | */ | |
28ab034a | 144 | if (handler(user, section, prev_name, start) < 0 && !error) { |
bac6245e JG |
145 | error = lineno; |
146 | } | |
147 | } | |
148 | #endif | |
149 | else if (*start == '[') { | |
150 | /* A "[section]" line */ | |
151 | end = find_char_or_comment(start + 1, ']'); | |
152 | if (*end == ']') { | |
153 | *end = '\0'; | |
154 | strncpy0(section, start + 1, sizeof(section)); | |
155 | *prev_name = '\0'; | |
28ab034a | 156 | } else if (!error) { |
bac6245e JG |
157 | /* No ']' found on section line */ |
158 | error = lineno; | |
159 | } | |
28ab034a | 160 | } else if (*start && *start != ';') { |
bac6245e JG |
161 | /* Not a comment, must be a name[=:]value pair */ |
162 | end = find_char_or_comment(start, '='); | |
163 | if (*end != '=') { | |
164 | end = find_char_or_comment(start, ':'); | |
165 | } | |
166 | if (*end == '=' || *end == ':') { | |
167 | *end = '\0'; | |
168 | name = rstrip(start); | |
169 | value = lskip(end + 1); | |
170 | end = find_char_or_comment(value, '\0'); | |
171 | if (*end == ';') { | |
172 | *end = '\0'; | |
173 | } | |
174 | ||
175 | rstrip(value); | |
176 | ||
177 | /* | |
178 | * Valid name[=:]value pair found, call | |
179 | * handler | |
180 | */ | |
1a125510 JG |
181 | if (strlen(name) >= sizeof(prev_name)) { |
182 | /* Truncation occurs, report an error. */ | |
183 | error = lineno; | |
184 | } | |
bac6245e | 185 | strncpy0(prev_name, name, sizeof(prev_name)); |
28ab034a | 186 | if (handler(user, section, name, value) < 0 && !error) { |
bac6245e JG |
187 | error = lineno; |
188 | } | |
28ab034a | 189 | } else if (!error) { |
bac6245e JG |
190 | /* No '=' or ':' found on name[=:]value line */ |
191 | error = lineno; | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | #if !INI_USE_STACK | |
197 | free(line); | |
198 | #endif | |
199 | ||
200 | return error; | |
201 | } | |
202 | ||
203 | /* See documentation in header file. */ | |
28ab034a | 204 | int ini_parse(const char *filename, ini_entry_handler handler, void *user) |
bac6245e | 205 | { |
28ab034a | 206 | FILE *file; |
bac6245e JG |
207 | int error; |
208 | ||
209 | file = fopen(filename, "r"); | |
210 | if (!file) { | |
211 | return -1; | |
212 | } | |
213 | ||
214 | error = ini_parse_file(file, handler, user); | |
215 | fclose(file); | |
216 | return error; | |
217 | } |