aboutsummaryrefslogtreecommitdiff
path: root/lib/io
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-19 18:57:38 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-20 10:57:38 +0200
commitca9b6ba17257f88b8d575f18cab0b1e23660cfa5 (patch)
tree355d8dbedb496f0235b08aba1bb73e41ba4083ba /lib/io
parentcb5473418b1f3b26555e26840a87a6feed3f583b (diff)
Move istream_get_line function to libutil
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io')
-rw-r--r--lib/io/Makemodule.am11
-rw-r--r--lib/io/src/get_line.c125
-rw-r--r--lib/io/src/internal.h1
-rw-r--r--lib/io/test/get_line.c177
4 files changed, 3 insertions, 311 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am
index 4f2c180..732c15b 100644
--- a/lib/io/Makemodule.am
+++ b/lib/io/Makemodule.am
@@ -1,7 +1,6 @@
-libio_a_SOURCES = include/io/istream.h include/io/xfrm.h \
- include/io/std.h include/io/dir_entry.h \
+libio_a_SOURCES = include/io/xfrm.h include/io/std.h include/io/dir_entry.h \
include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \
- lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \
+ lib/io/src/xfrm/ostream.c \
lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \
lib/io/src/dir_entry.c lib/io/src/mem.c lib/io/src/std.c
libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS)
@@ -16,17 +15,13 @@ endif
noinst_LIBRARIES += libio.a
-LIBIO_TESTS = test_istream_mem test_get_line test_dir_iterator \
+LIBIO_TESTS = test_istream_mem test_dir_iterator \
test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3
test_istream_mem_SOURCES = lib/io/test/istream_mem.c
test_istream_mem_LDADD = libio.a libcompat.a
test_istream_mem_CPPFLAGS = $(AM_CPPFLAGS)
-test_get_line_SOURCES = lib/io/test/get_line.c
-test_get_line_LDADD = libio.a libcompat.a
-test_get_line_CPPFLAGS = $(AM_CPPFLAGS)
-
test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c
test_dir_iterator_LDADD = libio.a libutil.a libcompat.a
test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c
deleted file mode 100644
index 3178592..0000000
--- a/lib/io/src/get_line.c
+++ /dev/null
@@ -1,125 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * get_line.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "internal.h"
-#include "sqfs/io.h"
-#include "sqfs/error.h"
-
-static void ltrim(char *buffer)
-{
- size_t i = 0;
-
- while (isspace(buffer[i]))
- ++i;
-
- if (i > 0)
- memmove(buffer, buffer + i, strlen(buffer + i) + 1);
-}
-
-static void rtrim(char *buffer)
-{
- size_t i = strlen(buffer);
-
- while (i > 0 && isspace(buffer[i - 1]))
- --i;
-
- buffer[i] = '\0';
-}
-
-static size_t trim(char *buffer, int flags)
-{
- if (flags & ISTREAM_LINE_LTRIM)
- ltrim(buffer);
-
- if (flags & ISTREAM_LINE_RTRIM)
- rtrim(buffer);
-
- return strlen(buffer);
-}
-
-int istream_get_line(sqfs_istream_t *strm, char **out,
- size_t *line_num, int flags)
-{
- char *line = NULL, *new;
- size_t line_len = 0;
- int err;
-
- for (;;) {
- bool have_line = false;
- size_t i, count, avail;
- const sqfs_u8 *ptr;
-
- err = strm->get_buffered_data(strm, &ptr, &avail, 0);
- if (err < 0)
- goto fail;
- if (err > 0) {
- if (line_len == 0)
- goto out_eof;
-
- line_len = trim(line, flags);
- if (line_len > 0 ||!(flags & ISTREAM_LINE_SKIP_EMPTY))
- break;
-
- goto out_eof;
- }
-
- for (i = 0; i < avail; ++i) {
- if (ptr[i] == '\n')
- break;
- }
-
- if (i < avail) {
- count = i++;
- have_line = true;
- } else {
- count = i;
- }
-
- new = realloc(line, line_len + count + 1);
- if (new == NULL) {
- err = SQFS_ERROR_ALLOC;
- goto fail;
- }
-
- line = new;
- memcpy(line + line_len, ptr, count);
- line_len += count;
- line[line_len] = '\0';
-
- strm->advance_buffer(strm, i);
-
- if (have_line) {
- if (line_len > 0 && line[line_len - 1] == '\r')
- line[--line_len] = '\0';
-
- line_len = trim(line, flags);
- if (line_len > 0 || !(flags & ISTREAM_LINE_SKIP_EMPTY))
- break;
-
- free(line);
- line = NULL;
- *line_num += 1;
- }
- }
-
- new = realloc(line, line_len + 1);
- if (new != NULL)
- line = new;
-
- *out = line;
- return 0;
-fail: {
- int temp = errno;
- free(line);
- *out = NULL;
- errno = temp;
- return err;
-}
-out_eof:
- free(line);
- *out = NULL;
- return 1;
-}
diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h
index a618ed3..e9804b9 100644
--- a/lib/io/src/internal.h
+++ b/lib/io/src/internal.h
@@ -9,7 +9,6 @@
#include "config.h"
#include "compat.h"
-#include "io/istream.h"
#include "io/xfrm.h"
#include "io/std.h"
#include "xfrm/compress.h"
diff --git a/lib/io/test/get_line.c b/lib/io/test/get_line.c
deleted file mode 100644
index d300a3a..0000000
--- a/lib/io/test/get_line.c
+++ /dev/null
@@ -1,177 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * get_line.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "io/mem.h"
-#include "util/test.h"
-
-typedef struct {
- size_t line_num;
- const char *str;
-} line_t;
-
-static void run_test_case(const char *raw, const line_t *lines, size_t count,
- int flags)
-{
- size_t i, line_num, old_line_num;
- sqfs_istream_t *fp;
- char *line;
- int ret;
-
- fp = istream_memory_create("lines.txt", 2, raw, strlen(raw));
- TEST_NOT_NULL(fp);
-
- line_num = 1;
- line = NULL;
-
- for (i = 0; i < count; ++i) {
- old_line_num = line_num;
- ret = istream_get_line(fp, &line, &line_num, flags);
-
- TEST_ASSERT(line_num >= old_line_num);
- TEST_EQUAL_I(ret, 0);
- TEST_NOT_NULL(line);
-
- TEST_EQUAL_UI(line_num, lines[i].line_num);
- TEST_STR_EQUAL(line, lines[i].str);
-
- free(line);
- line = NULL;
- line_num += 1;
- }
-
- ret = istream_get_line(fp, &line, &line_num, flags);
- TEST_ASSERT(ret > 0);
-
- sqfs_drop(fp);
-}
-
-static const char *file =
-"\r\n"
-"The quick\r\n"
-" \r\n"
-" brown fox \r\n"
-"\r\n"
-"jumps over\r\n"
-"the\r\n"
-"lazy\r\n"
-"\r\n"
-"dog\r\n"
-"\r\n";
-
-static const line_t lines_raw[] = {
- { 1, "" },
- { 2, "The quick" },
- { 3, " " },
- { 4, " brown fox " },
- { 5, "" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 9, "" },
- { 10, "dog" },
- { 11, "" },
-};
-
-static const line_t lines_ltrim[] = {
- { 1, "" },
- { 2, "The quick" },
- { 3, "" },
- { 4, "brown fox " },
- { 5, "" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 9, "" },
- { 10, "dog" },
- { 11, "" },
-};
-
-static const line_t lines_rtrim[] = {
- { 1, "" },
- { 2, "The quick" },
- { 3, "" },
- { 4, " brown fox" },
- { 5, "" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 9, "" },
- { 10, "dog" },
- { 11, "" },
-};
-
-static const line_t lines_trim[] = {
- { 1, "" },
- { 2, "The quick" },
- { 3, "" },
- { 4, "brown fox" },
- { 5, "" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 9, "" },
- { 10, "dog" },
- { 11, "" },
-};
-
-static const line_t lines_no_empty[] = {
- { 2, "The quick" },
- { 3, " " },
- { 4, " brown fox " },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 10, "dog" },
-};
-
-static const line_t lines_no_empty_ltrim[] = {
- { 2, "The quick" },
- { 4, "brown fox " },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 10, "dog" },
-};
-
-static const line_t lines_no_empty_rtrim[] = {
- { 2, "The quick" },
- { 4, " brown fox" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 10, "dog" },
-};
-
-static const line_t lines_no_empty_trim[] = {
- { 2, "The quick" },
- { 4, "brown fox" },
- { 6, "jumps over" },
- { 7, "the" },
- { 8, "lazy" },
- { 10, "dog" },
-};
-
-int main(int argc, char **argv)
-{
- (void)argc; (void)argv;
-
- run_test_case(file, lines_raw, 11, 0);
- run_test_case(file, lines_ltrim, 11, ISTREAM_LINE_LTRIM);
- run_test_case(file, lines_rtrim, 11, ISTREAM_LINE_RTRIM);
- run_test_case(file, lines_trim, 11,
- ISTREAM_LINE_LTRIM | ISTREAM_LINE_RTRIM);
-
- run_test_case(file, lines_no_empty, 7, ISTREAM_LINE_SKIP_EMPTY);
- run_test_case(file, lines_no_empty_ltrim, 6,
- ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM);
- run_test_case(file, lines_no_empty_rtrim, 6,
- ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_RTRIM);
- run_test_case(file, lines_no_empty_trim, 6,
- ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM |
- ISTREAM_LINE_RTRIM);
-
- return EXIT_SUCCESS;
-}