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 | * | |
6 | * Copyright (c) 2009, Brush Technology - 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 | * | |
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. | |
19 | * | |
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. | |
30 | * | |
31 | * http://code.google.com/p/inih/ | |
32 | */ | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <ctype.h> | |
36 | #include <string.h> | |
37 | ||
38 | #include "ini.h" | |
39 | ||
40 | #if !INI_USE_STACK | |
41 | #include <stdlib.h> | |
42 | #endif | |
43 | ||
44 | #define MAX_SECTION 50 | |
45 | #define MAX_NAME 50 | |
46 | ||
47 | /* Strip whitespace chars off end of given string, in place. Return s. */ | |
48 | static char* rstrip(char* s) | |
49 | { | |
50 | char* p = s + strlen(s); | |
51 | ||
52 | while (p > s && isspace((unsigned char)(*--p))) | |
53 | *p = '\0'; | |
54 | return s; | |
55 | } | |
56 | ||
57 | /* Return pointer to first non-whitespace char in given string. */ | |
58 | static char* lskip(const char* s) | |
59 | { | |
60 | while (*s && isspace((unsigned char)(*s))) | |
61 | s++; | |
62 | return (char*)s; | |
63 | } | |
64 | ||
65 | /* | |
66 | * Return pointer to first char c or ';' comment in given string, or pointer to | |
67 | * null at end of string if neither found. ';' must be prefixed by a whitespace | |
68 | * character to register as a comment. | |
69 | */ | |
70 | static char* find_char_or_comment(const char* s, char c) | |
71 | { | |
72 | int was_whitespace = 0; | |
73 | ||
74 | while (*s && *s != c && !(was_whitespace && *s == ';')) { | |
75 | was_whitespace = isspace((unsigned char)(*s)); | |
76 | s++; | |
77 | } | |
78 | return (char*)s; | |
79 | } | |
80 | ||
81 | /* Version of strncpy that ensures dest (size bytes) is null-terminated. */ | |
82 | static char* strncpy0(char* dest, const char* src, size_t size) | |
83 | { | |
84 | strncpy(dest, src, size); | |
85 | dest[size - 1] = '\0'; | |
86 | return dest; | |
87 | } | |
88 | ||
89 | /* See documentation in header file. */ | |
90 | int ini_parse_file(FILE* file, ini_entry_handler handler, void* user) | |
91 | { | |
92 | /* Uses a fair bit of stack (use heap instead if you need to) */ | |
93 | #if INI_USE_STACK | |
94 | char line[INI_MAX_LINE]; | |
95 | #else | |
96 | char* line; | |
97 | #endif | |
98 | char section[MAX_SECTION] = ""; | |
99 | char prev_name[MAX_NAME] = ""; | |
100 | ||
101 | char* start; | |
102 | char* end; | |
103 | char* name; | |
104 | char* value; | |
105 | int lineno = 0; | |
106 | int error = 0; | |
107 | ||
108 | #if !INI_USE_STACK | |
109 | line = (char*)malloc(INI_MAX_LINE); | |
110 | if (!line) { | |
111 | return -2; | |
112 | } | |
113 | #endif | |
114 | ||
115 | /* Scan through file line by line */ | |
116 | while (fgets(line, INI_MAX_LINE, file) != NULL) { | |
117 | lineno++; | |
118 | ||
119 | start = line; | |
120 | #if INI_ALLOW_BOM | |
121 | if (lineno == 1 && (unsigned char)start[0] == 0xEF && | |
122 | (unsigned char)start[1] == 0xBB && | |
123 | (unsigned char)start[2] == 0xBF) { | |
124 | start += 3; | |
125 | } | |
126 | #endif | |
127 | start = lskip(rstrip(start)); | |
128 | ||
129 | if (*start == ';' || *start == '#') { | |
130 | /* | |
131 | * Per Python ConfigParser, allow '#' comments at | |
132 | * start of line. | |
133 | */ | |
134 | } | |
135 | #if INI_ALLOW_MULTILINE | |
136 | else if (*prev_name && *start && start > line) { | |
137 | /* Non-black line with leading whitespace, treat as | |
138 | * continuation of previous name's value | |
139 | * (as per Python ConfigParser). | |
140 | */ | |
141 | if (handler(user, section, prev_name, start) < 0 && | |
142 | !error) { | |
143 | error = lineno; | |
144 | } | |
145 | } | |
146 | #endif | |
147 | else if (*start == '[') { | |
148 | /* A "[section]" line */ | |
149 | end = find_char_or_comment(start + 1, ']'); | |
150 | if (*end == ']') { | |
151 | *end = '\0'; | |
152 | strncpy0(section, start + 1, sizeof(section)); | |
153 | *prev_name = '\0'; | |
154 | } | |
155 | else if (!error) { | |
156 | /* No ']' found on section line */ | |
157 | error = lineno; | |
158 | } | |
159 | } | |
160 | else if (*start && *start != ';') { | |
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 | */ | |
181 | strncpy0(prev_name, name, sizeof(prev_name)); | |
182 | if (handler(user, section, name, value) < 0 && | |
183 | !error) { | |
184 | error = lineno; | |
185 | } | |
186 | } | |
187 | else if (!error) { | |
188 | /* No '=' or ':' found on name[=:]value line */ | |
189 | error = lineno; | |
190 | } | |
191 | } | |
192 | } | |
193 | ||
194 | #if !INI_USE_STACK | |
195 | free(line); | |
196 | #endif | |
197 | ||
198 | return error; | |
199 | } | |
200 | ||
201 | /* See documentation in header file. */ | |
202 | int ini_parse(const char* filename, ini_entry_handler handler, void* user) | |
203 | { | |
204 | FILE* file; | |
205 | int error; | |
206 | ||
207 | file = fopen(filename, "r"); | |
208 | if (!file) { | |
209 | return -1; | |
210 | } | |
211 | ||
212 | error = ini_parse_file(file, handler, user); | |
213 | fclose(file); | |
214 | return error; | |
215 | } |