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 | ||
36 | #include <stdio.h> | |
37 | #include <ctype.h> | |
38 | #include <string.h> | |
c9e313bc | 39 | #include <common/common.hpp> |
bac6245e | 40 | |
c9e313bc | 41 | #include "ini.hpp" |
bac6245e JG |
42 | |
43 | #if !INI_USE_STACK | |
44 | #include <stdlib.h> | |
45 | #endif | |
46 | ||
47 | #define MAX_SECTION 50 | |
48 | #define MAX_NAME 50 | |
49 | ||
50 | /* Strip whitespace chars off end of given string, in place. Return s. */ | |
51 | static char* rstrip(char* s) | |
52 | { | |
53 | char* p = s + strlen(s); | |
54 | ||
55 | while (p > s && isspace((unsigned char)(*--p))) | |
56 | *p = '\0'; | |
57 | return s; | |
58 | } | |
59 | ||
60 | /* Return pointer to first non-whitespace char in given string. */ | |
61 | static char* lskip(const char* s) | |
62 | { | |
63 | while (*s && isspace((unsigned char)(*s))) | |
64 | s++; | |
65 | return (char*)s; | |
66 | } | |
67 | ||
68 | /* | |
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. | |
72 | */ | |
73 | static char* find_char_or_comment(const char* s, char c) | |
74 | { | |
75 | int was_whitespace = 0; | |
76 | ||
77 | while (*s && *s != c && !(was_whitespace && *s == ';')) { | |
78 | was_whitespace = isspace((unsigned char)(*s)); | |
79 | s++; | |
80 | } | |
81 | return (char*)s; | |
82 | } | |
83 | ||
84 | /* Version of strncpy that ensures dest (size bytes) is null-terminated. */ | |
85 | static char* strncpy0(char* dest, const char* src, size_t size) | |
86 | { | |
9ff0b09b | 87 | strncpy(dest, src, size - 1); |
bac6245e JG |
88 | dest[size - 1] = '\0'; |
89 | return dest; | |
90 | } | |
91 | ||
92 | /* See documentation in header file. */ | |
93 | int ini_parse_file(FILE* file, ini_entry_handler handler, void* user) | |
94 | { | |
95 | /* Uses a fair bit of stack (use heap instead if you need to) */ | |
96 | #if INI_USE_STACK | |
97 | char line[INI_MAX_LINE]; | |
98 | #else | |
99 | char* line; | |
100 | #endif | |
101 | char section[MAX_SECTION] = ""; | |
102 | char prev_name[MAX_NAME] = ""; | |
103 | ||
104 | char* start; | |
105 | char* end; | |
106 | char* name; | |
107 | char* value; | |
108 | int lineno = 0; | |
109 | int error = 0; | |
110 | ||
111 | #if !INI_USE_STACK | |
64803277 | 112 | line = zmalloc<char>(INI_MAX_LINE); |
bac6245e JG |
113 | if (!line) { |
114 | return -2; | |
115 | } | |
116 | #endif | |
117 | ||
118 | /* Scan through file line by line */ | |
119 | while (fgets(line, INI_MAX_LINE, file) != NULL) { | |
120 | lineno++; | |
121 | ||
122 | start = line; | |
123 | #if INI_ALLOW_BOM | |
124 | if (lineno == 1 && (unsigned char)start[0] == 0xEF && | |
125 | (unsigned char)start[1] == 0xBB && | |
126 | (unsigned char)start[2] == 0xBF) { | |
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 | */ | |
144 | if (handler(user, section, prev_name, start) < 0 && | |
145 | !error) { | |
146 | error = lineno; | |
147 | } | |
148 | } | |
149 | #endif | |
150 | else if (*start == '[') { | |
151 | /* A "[section]" line */ | |
152 | end = find_char_or_comment(start + 1, ']'); | |
153 | if (*end == ']') { | |
154 | *end = '\0'; | |
155 | strncpy0(section, start + 1, sizeof(section)); | |
156 | *prev_name = '\0'; | |
157 | } | |
158 | else if (!error) { | |
159 | /* No ']' found on section line */ | |
160 | error = lineno; | |
161 | } | |
162 | } | |
163 | else if (*start && *start != ';') { | |
164 | /* Not a comment, must be a name[=:]value pair */ | |
165 | end = find_char_or_comment(start, '='); | |
166 | if (*end != '=') { | |
167 | end = find_char_or_comment(start, ':'); | |
168 | } | |
169 | if (*end == '=' || *end == ':') { | |
170 | *end = '\0'; | |
171 | name = rstrip(start); | |
172 | value = lskip(end + 1); | |
173 | end = find_char_or_comment(value, '\0'); | |
174 | if (*end == ';') { | |
175 | *end = '\0'; | |
176 | } | |
177 | ||
178 | rstrip(value); | |
179 | ||
180 | /* | |
181 | * Valid name[=:]value pair found, call | |
182 | * handler | |
183 | */ | |
184 | strncpy0(prev_name, name, sizeof(prev_name)); | |
185 | if (handler(user, section, name, value) < 0 && | |
186 | !error) { | |
187 | error = lineno; | |
188 | } | |
189 | } | |
190 | else if (!error) { | |
191 | /* No '=' or ':' found on name[=:]value line */ | |
192 | error = lineno; | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | #if !INI_USE_STACK | |
198 | free(line); | |
199 | #endif | |
200 | ||
201 | return error; | |
202 | } | |
203 | ||
204 | /* See documentation in header file. */ | |
205 | int ini_parse(const char* filename, ini_entry_handler handler, void* user) | |
206 | { | |
207 | FILE* file; | |
208 | int error; | |
209 | ||
210 | file = fopen(filename, "r"); | |
211 | if (!file) { | |
212 | return -1; | |
213 | } | |
214 | ||
215 | error = ini_parse_file(file, handler, user); | |
216 | fclose(file); | |
217 | return error; | |
218 | } |