6 * Copyright (C) 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
26 #include "wimlib/assert.h"
27 #include "wimlib/encoding.h"
28 #include "wimlib/error.h"
29 #include "wimlib/file_io.h"
30 #include "wimlib/textfile.h"
31 #include "wimlib/util.h"
41 read_file_contents(const tchar *path, void **buf_ret, size_t *bufsize_ret)
51 return WIMLIB_ERR_INVALID_PARAM;
53 raw_fd = topen(path, O_RDONLY | O_BINARY);
55 ERROR_WITH_ERRNO("Can't open \"%"TS"\"", path);
56 return WIMLIB_ERR_OPEN;
58 if (fstat(raw_fd, &st)) {
59 ERROR_WITH_ERRNO("Can't stat \"%"TS"\"", path);
61 return WIMLIB_ERR_STAT;
63 if ((size_t)st.st_size != st.st_size ||
64 (buf = MALLOC(st.st_size)) == NULL)
67 ERROR("Not enough memory to read \"%"TS"\"", path);
68 return WIMLIB_ERR_NOMEM;
71 filedes_init(&fd, raw_fd);
72 ret = full_read(&fd, buf, st.st_size);
77 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", path);
83 *bufsize_ret = st.st_size;
88 translate_text_buffer(const u8 *buf_raw, size_t bufsize_raw,
89 tchar **tstr_ret, size_t *tstr_nchars_ret)
97 /* Guess the encoding: UTF-8 or UTF-16LE. (Something weirder and you're
98 * out of luck, sorry...) */
99 if (bufsize_raw >= 2 &&
100 buf_raw[0] == 0xFF &&
106 else if (bufsize_raw >= 2 &&
107 buf_raw[0] <= 0x7F &&
113 else if (bufsize_raw >= 3 &&
114 buf_raw[0] == 0xEF &&
115 buf_raw[1] == 0xBB &&
128 ret = utf8_to_tstr((const char *)(buf_raw + offset_raw),
129 bufsize_raw - offset_raw,
130 &buf_tstr, &bufsize_tstr);
132 ret = utf16le_to_tstr((const utf16lechar *)(buf_raw + offset_raw),
133 bufsize_raw - offset_raw,
134 &buf_tstr, &bufsize_tstr);
139 *tstr_ret = buf_tstr;
140 *tstr_nchars_ret = bufsize_tstr / sizeof(tchar);
145 string_set_append(struct string_set *set, tchar *str)
147 size_t num_alloc_strings = set->num_alloc_strings;
149 if (set->num_strings == num_alloc_strings) {
152 num_alloc_strings = max(num_alloc_strings * 3 / 2,
153 num_alloc_strings + 4);
154 new_strings = REALLOC(set->strings,
155 sizeof(set->strings[0]) * num_alloc_strings);
157 return WIMLIB_ERR_NOMEM;
158 set->strings = new_strings;
159 set->num_alloc_strings = num_alloc_strings;
161 set->strings[set->num_strings++] = str;
165 #define NOT_IN_SECTION -1
166 #define IN_UNKNOWN_SECTION -2
169 parse_text_file(const tchar *path, tchar *buf, size_t buflen,
170 const struct text_file_section *pos_sections,
171 int num_pos_sections, int flags, line_mangle_t mangle_line)
173 int current_section = NOT_IN_SECTION;
174 bool have_named_sections = false;
177 unsigned long line_no = 1;
179 for (int i = 0; i < num_pos_sections; i++) {
180 if (*pos_sections[i].name)
181 have_named_sections = true;
186 for (p = buf; p != buf + buflen; p = nl + 1, line_no++) {
187 tchar *line_begin, *line_end;
191 nl = tmemchr(p, T('\n'), buf + buflen - p);
198 /* Ignore leading whitespace. */
199 while (line_begin < nl && istspace(*line_begin))
202 /* Ignore trailing whitespace. */
203 while (line_end > line_begin && istspace(*(line_end - 1)))
206 line_len = line_end - line_begin;
208 /* Ignore comments and empty lines. */
209 if (line_len == 0 || *line_begin == T(';') || *line_begin == T('#'))
212 line_begin[line_len] = T('\0');
214 /* Check for beginning of new section. */
215 if (line_begin[0] == T('[') &&
216 line_begin[line_len - 1] == T(']') &&
219 line_begin[line_len - 1] = T('\0');
220 current_section = IN_UNKNOWN_SECTION;
221 for (int i = 0; i < num_pos_sections; i++) {
222 if (!tstrcmp(line_begin + 1,
223 pos_sections[i].name))
229 line_begin[line_len - 1] = T(']');
230 if (current_section < 0) {
231 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
232 WARNING("%"TS":%lu: Unrecognized section \"%"TS"\"",
233 path, line_no, line_begin);
239 if (current_section < 0) {
240 if (current_section == NOT_IN_SECTION) {
241 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
242 WARNING("%"TS":%lu: Not in a bracketed section!",
249 if (flags & LOAD_TEXT_FILE_REMOVE_QUOTES) {
250 if (line_begin[0] == T('"') || line_begin[0] == T('\'')) {
251 tchar quote = line_begin[0];
253 line_begin[line_len - 1] == quote)
257 line_begin[line_len] = T('\0');
263 ret = (*mangle_line)(line_begin, path, line_no);
268 ret = string_set_append(pos_sections[current_section].strings,
277 * do_load_text_file -
279 * Read and parse lines from a text file from an on-disk file or a buffer.
280 * The file may contain sections, like in an INI file.
283 * Path to the file on disk to read, or a dummy name for the buffer.
285 * If NULL, the data will be read from the @path file. Otherwise the data
286 * will be read from this buffer.
288 * Length of buffer in bytes; ignored if @buf is NULL.
290 * On success, a pointer to a buffer backing the parsed lines is stored
291 * here. This must be freed after the parsed lines are done being used.
293 * Specifications of allowed sections in the file. Each such specification
294 * consists of the name of the section (e.g. [ExclusionList], like in the
295 * INI file format), along with a pointer to the list of lines parsed for
296 * that section. Use an empty name to indicate the destination of lines
297 * not in any section. Each list must be initialized to an empty string
300 * Number of entries in the @pos_sections array.
302 * Flags: LOAD_TEXT_FILE_REMOVE_QUOTES, LOAD_TEXT_FILE_NO_WARNINGS.
304 * Optional callback to validate and/or modify each line being read.
306 * Returns 0 on success; nonzero on failure.
308 * Unknown sections are ignored, but a warning is printed for each, unless
309 * LOAD_TEXT_FILE_NO_WARNINGS is specified.
312 do_load_text_file(const tchar *path,
313 const void *buf, size_t bufsize,
315 const struct text_file_section *pos_sections,
316 int num_pos_sections,
318 line_mangle_t mangle_line)
321 bool pathmode = (buf == NULL);
326 ret = read_file_contents(path, (void **)&buf, &bufsize);
331 ret = translate_text_buffer(buf, bufsize, &tstr, &tstr_nchars);
337 tstr[tstr_nchars++] = T('\n');
339 ret = parse_text_file(path, tstr, tstr_nchars, pos_sections,
340 num_pos_sections, flags, mangle_line);
342 for (int i = 0; i < num_pos_sections; i++)
343 FREE(pos_sections[i].strings->strings);