6 * Copyright (C) 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/assert.h"
29 #include "wimlib/encoding.h"
30 #include "wimlib/error.h"
31 #include "wimlib/file_io.h"
32 #include "wimlib/textfile.h"
33 #include "wimlib/util.h"
43 read_file_contents(const tchar *path, void **buf_ret, size_t *bufsize_ret)
53 return WIMLIB_ERR_INVALID_PARAM;
55 raw_fd = topen(path, O_RDONLY | O_BINARY);
57 ERROR_WITH_ERRNO("Can't open \"%"TS"\"", path);
58 return WIMLIB_ERR_OPEN;
60 if (fstat(raw_fd, &st)) {
61 ERROR_WITH_ERRNO("Can't stat \"%"TS"\"", path);
63 return WIMLIB_ERR_STAT;
65 if ((size_t)st.st_size != st.st_size ||
66 (buf = MALLOC(st.st_size)) == NULL)
69 ERROR("Not enough memory to read \"%"TS"\"", path);
70 return WIMLIB_ERR_NOMEM;
73 filedes_init(&fd, raw_fd);
74 ret = full_read(&fd, buf, st.st_size);
79 ERROR_WITH_ERRNO("Error reading \"%"TS"\"", path);
85 *bufsize_ret = st.st_size;
90 translate_text_buffer(const u8 *buf_raw, size_t bufsize_raw,
91 tchar **tstr_ret, size_t *tstr_nchars_ret)
99 /* Guess the encoding: UTF-8 or UTF-16LE. (Something weirder and you're
100 * out of luck, sorry...) */
101 if (bufsize_raw >= 2 &&
102 buf_raw[0] == 0xFF &&
108 else if (bufsize_raw >= 2 &&
109 buf_raw[0] <= 0x7F &&
115 else if (bufsize_raw >= 3 &&
116 buf_raw[0] == 0xEF &&
117 buf_raw[1] == 0xBB &&
130 ret = utf8_to_tstr((const char *)(buf_raw + offset_raw),
131 bufsize_raw - offset_raw,
132 &buf_tstr, &bufsize_tstr);
134 ret = utf16le_to_tstr((const utf16lechar *)(buf_raw + offset_raw),
135 bufsize_raw - offset_raw,
136 &buf_tstr, &bufsize_tstr);
141 *tstr_ret = buf_tstr;
142 *tstr_nchars_ret = bufsize_tstr / sizeof(tchar);
147 string_set_append(struct string_set *set, tchar *str)
149 size_t num_alloc_strings = set->num_alloc_strings;
151 if (set->num_strings == num_alloc_strings) {
154 num_alloc_strings = max(num_alloc_strings * 3 / 2,
155 num_alloc_strings + 4);
156 new_strings = REALLOC(set->strings,
157 sizeof(set->strings[0]) * num_alloc_strings);
159 return WIMLIB_ERR_NOMEM;
160 set->strings = new_strings;
161 set->num_alloc_strings = num_alloc_strings;
163 set->strings[set->num_strings++] = str;
167 #define NOT_IN_SECTION -1
168 #define IN_UNKNOWN_SECTION -2
171 parse_text_file(const tchar *path, tchar *buf, size_t buflen,
172 const struct text_file_section *pos_sections,
173 int num_pos_sections, int flags, line_mangle_t mangle_line)
175 int current_section = NOT_IN_SECTION;
176 bool have_named_sections = false;
179 unsigned long line_no = 1;
181 for (int i = 0; i < num_pos_sections; i++) {
182 if (*pos_sections[i].name)
183 have_named_sections = true;
188 for (p = buf; p != buf + buflen; p = nl + 1, line_no++) {
189 tchar *line_begin, *line_end;
193 nl = tmemchr(p, T('\n'), buf + buflen - p);
200 /* Ignore leading whitespace. */
201 while (line_begin < nl && istspace(*line_begin))
204 /* Ignore trailing whitespace. */
205 while (line_end > line_begin && istspace(*(line_end - 1)))
208 line_len = line_end - line_begin;
210 /* Ignore comments and empty lines. */
211 if (line_len == 0 || *line_begin == T(';') || *line_begin == T('#'))
214 line_begin[line_len] = T('\0');
216 /* Check for beginning of new section. */
217 if (line_begin[0] == T('[') &&
218 line_begin[line_len - 1] == T(']') &&
221 line_begin[line_len - 1] = T('\0');
222 current_section = IN_UNKNOWN_SECTION;
223 for (int i = 0; i < num_pos_sections; i++) {
224 if (!tstrcmp(line_begin + 1,
225 pos_sections[i].name))
231 line_begin[line_len - 1] = T(']');
232 if (current_section < 0) {
233 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
234 WARNING("%"TS":%lu: Unrecognized section \"%"TS"\"",
235 path, line_no, line_begin);
241 if (current_section < 0) {
242 if (current_section == NOT_IN_SECTION) {
243 if (!(flags & LOAD_TEXT_FILE_NO_WARNINGS)) {
244 WARNING("%"TS":%lu: Not in a bracketed section!",
251 if (flags & LOAD_TEXT_FILE_REMOVE_QUOTES) {
252 if (line_begin[0] == T('"') || line_begin[0] == T('\'')) {
253 tchar quote = line_begin[0];
255 line_begin[line_len - 1] == quote)
259 line_begin[line_len] = T('\0');
265 ret = (*mangle_line)(line_begin, path, line_no);
270 ret = string_set_append(pos_sections[current_section].strings,
279 * do_load_text_file -
281 * Read and parse lines from a text file from an on-disk file or a buffer.
282 * The file may contain sections, like in an INI file.
285 * Path to the file on disk to read, or a dummy name for the buffer.
287 * If NULL, the data will be read from the @path file. Otherwise the data
288 * will be read from this buffer.
290 * Length of buffer in bytes; ignored if @buf is NULL.
292 * On success, a pointer to a buffer backing the parsed lines is stored
293 * here. This must be freed after the parsed lines are done being used.
295 * Specifications of allowed sections in the file. Each such specification
296 * consists of the name of the section (e.g. [ExclusionList], like in the
297 * INI file format), along with a pointer to the list of lines parsed for
298 * that section. Use an empty name to indicate the destination of lines
299 * not in any section. Each list must be initialized to an empty string
302 * Number of entries in the @pos_sections array.
304 * Flags: LOAD_TEXT_FILE_REMOVE_QUOTES, LOAD_TEXT_FILE_NO_WARNINGS.
306 * Optional callback to validate and/or modify each line being read.
308 * Returns 0 on success; nonzero on failure.
310 * Unknown sections are ignored, but a warning is printed for each, unless
311 * LOAD_TEXT_FILE_NO_WARNINGS is specified.
314 do_load_text_file(const tchar *path,
315 const void *buf, size_t bufsize,
317 const struct text_file_section *pos_sections,
318 int num_pos_sections,
320 line_mangle_t mangle_line)
323 bool pathmode = (buf == NULL);
328 ret = read_file_contents(path, (void **)&buf, &bufsize);
333 ret = translate_text_buffer(buf, bufsize, &tstr, &tstr_nchars);
339 tstr[tstr_nchars++] = T('\n');
341 ret = parse_text_file(path, tstr, tstr_nchars, pos_sections,
342 num_pos_sections, flags, mangle_line);
344 for (int i = 0; i < num_pos_sections; i++)
345 FREE(pos_sections[i].strings->strings);