From 4b4cee0c0c99f531a45157fd27f5441e511db109 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 25 Sep 2020 21:06:59 +0200 Subject: Replace file/getline usage with istream Signed-off-by: David Oberhollenzer --- lib/compat/Makemodule.am | 2 +- lib/compat/getline.c | 60 ---------------------------------- lib/fstree/fstree_from_file.c | 76 ++++++++++++++----------------------------- 3 files changed, 25 insertions(+), 113 deletions(-) delete mode 100644 lib/compat/getline.c (limited to 'lib') diff --git a/lib/compat/Makemodule.am b/lib/compat/Makemodule.am index c1be522..4f4fc9c 100644 --- a/lib/compat/Makemodule.am +++ b/lib/compat/Makemodule.am @@ -1,4 +1,4 @@ -libcompat_a_SOURCES = lib/compat/getline.c lib/compat/getsubopt.c +libcompat_a_SOURCES = lib/compat/getsubopt.c libcompat_a_SOURCES += lib/compat/strndup.c lib/compat/mockups.c libcompat_a_SOURCES += lib/compat/chdir.c include/compat.h libcompat_a_SOURCES += lib/compat/path_to_windows.c diff --git a/lib/compat/getline.c b/lib/compat/getline.c deleted file mode 100644 index e63c50e..0000000 --- a/lib/compat/getline.c +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * getline.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "compat.h" - -#include -#include - -#ifndef HAVE_GETLINE -ssize_t getline(char **line, size_t *n, FILE *fp) -{ - size_t new_cap, len = 0, cap = 0; - char *buffer = NULL, *new; - int c; - - if (feof(fp) || ferror(fp)) - return -1; - - do { - c = fgetc(fp); - - if (ferror(fp)) - return -1; - - if (c == EOF) - c = '\n'; - - if (len == cap) { - new_cap = cap ? cap * 2 : 32; - new = realloc(buffer, new_cap); - - if (new == NULL) - return -1; - - buffer = new; - cap = new_cap; - } - - buffer[len++] = c; - } while (c != '\n'); - - if (len == cap) { - new = realloc(buffer, cap ? cap * 2 : 32); - if (new == NULL) - return -1; - - buffer = new; - } - - buffer[len] = '\0'; - - *line = buffer; - *n = len; - return len; -} -#endif diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index bf11755..678d565 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -7,12 +7,12 @@ #include "config.h" #include "fstree.h" +#include "fstream.h" #include -#include #include -#include #include +#include static int add_generic(fstree_t *fs, const char *filename, size_t line_num, const char *path, struct stat *sb, const char *extra) @@ -93,28 +93,6 @@ static const struct { #define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0])) -static void trim_line(char *line) -{ - size_t i; - - for (i = 0; isspace(line[i]); ++i) - ; - - if (line[i] == '#') { - line[0] = '\0'; - return; - } - - if (i > 0) - memmove(line, line + i, strlen(line + i) + 1); - - i = strlen(line); - while (i > 0 && isspace(line[i - 1])) - --i; - - line[i] = '\0'; -} - static int handle_line(fstree_t *fs, const char *filename, size_t line_num, char *line) { @@ -279,44 +257,38 @@ out_desc: return -1; } -int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp) +int fstree_from_file(fstree_t *fs, const char *filename) { - size_t n, line_num = 0; - ssize_t ret; + size_t line_num = 1; + istream_t *fp; char *line; + int ret; - for (;;) { - line = NULL; - n = 0; - errno = 0; - - ret = getline(&line, &n, fp); - ++line_num; - - if (ret < 0) { - if (errno == 0) { - free(line); - break; - } - - perror(filename); - goto fail_line; - } - - trim_line(line); + fp = istream_open_file(filename); + if (fp == NULL) + return -1; - if (line[0] == '\0') { - free(line); - continue; + for (;;) { + ret = istream_get_line(fp, &line, &line_num, + ISTREAM_LINE_LTRIM | ISTREAM_LINE_SKIP_EMPTY); + if (ret < 0) + return -1; + if (ret > 0) + break; + + if (line[0] != '#') { + if (handle_line(fs, filename, line_num, line)) + goto fail_line; } - if (handle_line(fs, filename, line_num, line)) - goto fail_line; - free(line); + ++line_num; } + + sqfs_destroy(fp); return 0; fail_line: free(line); + sqfs_destroy(fp); return -1; } -- cgit v1.2.3