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/encoding.h"
27 #include "wimlib/error.h"
28 #include "wimlib/file_io.h"
29 #include "wimlib/textfile.h"
30 #include "wimlib/util.h"
40 read_file_contents(const tchar *path, void **buf_ret, size_t *bufsize_ret)
50 return WIMLIB_ERR_INVALID_PARAM;
52 raw_fd = topen(path, O_RDONLY | O_BINARY);
54 ERROR_WITH_ERRNO("Can't open \"%"TS"\"", path);
55 return WIMLIB_ERR_OPEN;
57 if (fstat(raw_fd, &st)) {
58 ERROR_WITH_ERRNO("Can't stat \"%"TS"\"", path);
60 return WIMLIB_ERR_STAT;
62 if ((size_t)st.st_size != st.st_size ||
63 (buf = MALLOC(st.st_size)) == NULL)
66 ERROR("Not enough memory to read \"%"TS"\"", path);
67 return WIMLIB_ERR_NOMEM;
70 filedes_init(&fd, raw_fd);
71 ret = full_read(&fd, buf, st.st_size);
76 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", path);
82 *bufsize_ret = st.st_size;
87 translate_text_buffer(const u8 *buf_raw, size_t bufsize_raw,
88 tchar **tstr_ret, size_t *tstr_nchars_ret)
96 /* Guess the encoding: UTF-8 or UTF-16LE. (Something weirder and you're
97 * out of luck, sorry...) */
98 if (bufsize_raw >= 2 &&
105 else if (bufsize_raw >= 2 &&
106 buf_raw[0] <= 0x7F &&
112 else if (bufsize_raw >= 3 &&
113 buf_raw[0] == 0xEF &&
114 buf_raw[1] == 0xBB &&
127 ret = utf8_to_tstr((const char *)(buf_raw + offset_raw),
128 bufsize_raw - offset_raw,
129 &buf_tstr, &bufsize_tstr);
131 ret = utf16le_to_tstr((const utf16lechar *)(buf_raw + offset_raw),
132 bufsize_raw - offset_raw,
133 &buf_tstr, &bufsize_tstr);
138 *tstr_ret = buf_tstr;
139 *tstr_nchars_ret = bufsize_tstr / sizeof(tchar);
144 string_set_append(struct string_set *set, tchar *str)
146 size_t num_alloc_strings = set->num_alloc_strings;
148 if (set->num_strings == num_alloc_strings) {
151 num_alloc_strings = max(num_alloc_strings * 3 / 2,
152 num_alloc_strings + 4);
153 new_strings = REALLOC(set->strings,
154 sizeof(set->strings[0]) * num_alloc_strings);
156 return WIMLIB_ERR_NOMEM;
157 set->strings = new_strings;
158 set->num_alloc_strings = num_alloc_strings;
160 set->strings[set->num_strings++] = str;
164 #define NOT_IN_SECTION -1
165 #define IN_UNKNOWN_SECTION -2
168 parse_text_file(const tchar *path, tchar *buf, size_t buflen,
169 const struct text_file_section *pos_sections,
170 int num_pos_sections, int flags, line_mangle_t mangle_line)
172 int current_section = NOT_IN_SECTION;
173 bool have_named_sections = false;
176 unsigned long line_no = 1;
178 for (int i = 0; i < num_pos_sections; i++) {
179 if (*pos_sections[i].name)
180 have_named_sections = true;
185 for (p = buf; p != buf + buflen; p = nl + 1, line_no++) {
186 tchar *line_begin, *line_end;
190 nl = tmemchr(p, T('\n'), buf + buflen - p);
197 /* Ignore leading whitespace. */
198 while (line_begin < nl && istspace(*line_begin))
201 /* Ignore trailing whitespace. */
202 while (line_end > line_begin && istspace(*(line_end - 1)))
205 line_len = line_end - line_begin;
207 /* Ignore comments and empty lines. */
208 if (line_len == 0 || *line_begin == T(';') || *line_begin == T('#'))
211 line_begin[line_len] = T('\0');
213 /* Check for beginning of new section. */
214 if (line_begin[0] == T('[') &&
215 line_begin[line_len - 1] == T(']') &&
218 line_begin[line_len - 1] = T('\0');
219 current_section = IN_UNKNOWN_SECTION;
220 for (int i = 0; i < num_pos_sections; i++) {
221 if (!tstrcmp(line_begin + 1,
222 pos_sections[i].name))
228 line_begin[line_len - 1] = T(']');
229 if (current_section < 0) {
230 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
231 WARNING("%"TS":%lu: Unrecognized section \"%"TS"\"",
232 path, line_no, line_begin);
238 if (current_section < 0) {
239 if (current_section == NOT_IN_SECTION) {
240 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
241 WARNING("%"TS":%lu: Not in a bracketed section!",
248 if (flags & LOAD_TEXT_FILE_REMOVE_QUOTES) {
249 if (line_begin[0] == T('"') || line_begin[0] == T('\'')) {
250 tchar quote = line_begin[0];
252 line_begin[line_len - 1] == quote)
256 line_begin[line_len] = T('\0');
262 ret = (*mangle_line)(line_begin, path, line_no);
267 ret = string_set_append(pos_sections[current_section].strings,
276 * do_load_text_file -
278 * Read and parse lines from a text file from an on-disk file or a buffer.
279 * The file may contain sections, like in an INI file.
282 * Path to the file on disk to read, or a dummy name for the buffer.
284 * If NULL, the data will be read from the @path file. Otherwise the data
285 * will be read from this buffer.
287 * Length of buffer in bytes; ignored if @buf is NULL.
289 * On success, a pointer to a buffer backing the parsed lines is stored
290 * here. This must be freed after the parsed lines are done being used.
292 * Specifications of allowed sections in the file. Each such specification
293 * consists of the name of the section (e.g. [ExclusionList], like in the
294 * INI file format), along with a pointer to the list of lines parsed for
295 * that section. Use an empty name to indicate the destination of lines
296 * not in any section. Each list must be initialized to an empty string
299 * Number of entries in the @pos_sections array.
301 * Flags: LOAD_TEXT_FILE_REMOVE_QUOTES, LOAD_TEXT_FILE_NO_WARNINGS.
303 * Optional callback to validate and/or modify each line being read.
305 * Returns 0 on success; nonzero on failure.
307 * Unknown sections are ignored, but a warning is printed for each, unless
308 * LOAD_TEXT_FILE_NO_WARNINGS is specified.
311 do_load_text_file(const tchar *path,
312 const void *buf, size_t bufsize,
314 const struct text_file_section *pos_sections,
315 int num_pos_sections,
317 line_mangle_t mangle_line)
320 bool pathmode = (buf == NULL);
325 ret = read_file_contents(path, (void **)&buf, &bufsize);
330 ret = translate_text_buffer(buf, bufsize, &tstr, &tstr_nchars);
336 tstr[tstr_nchars++] = T('\n');
338 ret = parse_text_file(path, tstr, tstr_nchars, pos_sections,
339 num_pos_sections, flags, mangle_line);
341 for (int i = 0; i < num_pos_sections; i++)
342 FREE(pos_sections[i].strings->strings);