X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fpathlist.c;h=b0bd820bd1fc61ad9d3820247c31b1475592dfea;hp=3bfec2ef062687b1810cf65a8eb3c6ff348c3fd5;hb=51df3b63c594a7e35446d2e2e19637e54240b0b2;hpb=d9675dd5814394373d9871c6e9b7b35325b3d21d diff --git a/src/pathlist.c b/src/pathlist.c index 3bfec2ef..b0bd820b 100644 --- a/src/pathlist.c +++ b/src/pathlist.c @@ -7,185 +7,46 @@ /* * Copyright (C) 2013 Eric Biggers * - * This file is part of wimlib, a library for working with WIM files. + * This file is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. * - * wimlib is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free - * Software Foundation; either version 3 of the License, or (at your option) - * any later version. - * - * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more + * This file is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * - * You should have received a copy of the GNU General Public License - * along with wimlib; if not, see http://www.gnu.org/licenses/. + * You should have received a copy of the GNU Lesser General Public License + * along with this file; if not, see http://www.gnu.org/licenses/. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif -#include "wimlib/encoding.h" -#include "wimlib/error.h" -#include "wimlib/file_io.h" #include "wimlib/pathlist.h" -#include "wimlib/util.h" - -#include -#include -#include -#include -#include -#include - -static int -read_file_contents(const tchar *path, char **buf_ret, size_t *bufsize_ret) -{ - int raw_fd; - struct filedes fd; - struct stat st; - void *buf; - int ret; - int errno_save; - - raw_fd = topen(path, O_RDONLY | O_BINARY); - if (raw_fd < 0) { - ERROR_WITH_ERRNO("Can't open \"%"TS"\"", path); - return WIMLIB_ERR_OPEN; - } - if (fstat(raw_fd, &st)) { - ERROR_WITH_ERRNO("Can't stat \"%"TS"\"", path); - close(raw_fd); - return WIMLIB_ERR_STAT; - } - if ((size_t)st.st_size != st.st_size || - (buf = MALLOC(st.st_size)) == NULL) - { - close(raw_fd); - ERROR("Not enough memory to read \"%"TS"\"", path); - return WIMLIB_ERR_NOMEM; - } - - filedes_init(&fd, raw_fd); - ret = full_read(&fd, buf, st.st_size); - errno_save = errno; - filedes_close(&fd); - errno = errno_save; - if (ret) { - ERROR_WITH_ERRNO("Error reading \"%"TS"\"", path); - FREE(buf); - return ret; - } - - *buf_ret = buf; - *bufsize_ret = st.st_size; - return 0; -} - -static int -read_utf8_file_contents(const tchar *path, tchar **buf_ret, size_t *buflen_ret) -{ - int ret; - char *buf_utf8; - size_t bufsize_utf8; - tchar *buf_tstr; - size_t bufsize_tstr; - - ret = read_file_contents(path, &buf_utf8, &bufsize_utf8); - if (ret) - return ret; - - ret = utf8_to_tstr(buf_utf8, bufsize_utf8, &buf_tstr, &bufsize_tstr); - FREE(buf_utf8); - if (ret) - return ret; - - *buf_ret = buf_tstr; - *buflen_ret = bufsize_tstr / sizeof(tchar); - return 0; -} - -static int -parse_path_list_file(tchar *buf, size_t buflen, - tchar ***paths_ret, size_t *num_paths_ret) -{ - tchar **paths = NULL; - size_t num_paths = 0; - size_t num_alloc_paths = 0; - tchar *nl; - tchar *p; - - for (p = buf; p != buf + buflen; p = nl + 1) { - tchar *line_begin, *line_end; - size_t line_len; - - nl = tmemchr(p, T('\n'), buf + buflen - p); - if (nl == NULL) - break; - - line_begin = p; - line_end = nl; - - /* Ignore leading whitespace. */ - while (line_begin < nl && istspace(*line_begin)) - line_begin++; - - /* Ignore trailing whitespace. */ - while (line_end > line_begin && istspace(*(line_end - 1))) - line_end--; - - line_len = line_end - line_begin; - - /* Ignore comments and empty lines. */ - if (line_len == 0 || *line_begin == T(';')) - continue; - - if (num_paths == num_alloc_paths) { - tchar **new_paths; - size_t new_num_alloc_paths = max(num_alloc_paths + 8, - num_alloc_paths * 3 / 2); - - new_paths = REALLOC(paths, new_num_alloc_paths * - sizeof(paths[0])); - if (new_paths == NULL) - goto oom; - paths = new_paths; - num_alloc_paths = new_num_alloc_paths; - } - - *line_end = T('\0'); - paths[num_paths++] = line_begin; - } - - *paths_ret = paths; - *num_paths_ret = num_paths; - return 0; - -oom: - FREE(paths); - return WIMLIB_ERR_NOMEM; -} +#include "wimlib/textfile.h" int read_path_list_file(const tchar *listfile, tchar ***paths_ret, size_t *num_paths_ret, void **mem_ret) { + STRING_SET(paths); + struct text_file_section tmp = { + .name = T(""), + .strings = &paths, + }; + void *buf; int ret; - tchar *buf; - size_t buflen; - ret = read_utf8_file_contents(listfile, &buf, &buflen); + ret = load_text_file(listfile, &buf, &tmp, 1, NULL); if (ret) return ret; - ret = parse_path_list_file(buf, buflen, paths_ret, num_paths_ret); - if (ret) { - FREE(buf); - return ret; - } + *paths_ret = paths.strings; + *num_paths_ret = paths.num_strings; *mem_ret = buf; return 0; }