From ca9b6ba17257f88b8d575f18cab0b1e23660cfa5 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 19 Jun 2023 18:57:38 +0200 Subject: Move istream_get_line function to libutil Signed-off-by: David Oberhollenzer --- bin/gensquashfs/src/mkfs.h | 1 + include/fstree.h | 1 - include/io/dir_iterator.h | 2 +- include/io/istream.h | 55 -------------- include/io/mem.h | 2 +- include/io/std.h | 2 +- include/io/xfrm.h | 2 +- include/tar/tar.h | 1 - include/util/parse.h | 55 ++++++++++++++ lib/io/Makemodule.am | 11 +-- lib/io/src/get_line.c | 125 ------------------------------- lib/io/src/internal.h | 1 - lib/io/test/get_line.c | 177 -------------------------------------------- lib/util/Makemodule.am | 10 ++- lib/util/src/get_line.c | 130 +++++++++++++++++++++++++++++++++ lib/util/test/get_line.c | 178 +++++++++++++++++++++++++++++++++++++++++++++ lib/util/test/str_table.c | 2 +- 17 files changed, 379 insertions(+), 376 deletions(-) delete mode 100644 include/io/istream.h create mode 100644 include/util/parse.h delete mode 100644 lib/io/src/get_line.c delete mode 100644 lib/io/test/get_line.c create mode 100644 lib/util/src/get_line.c create mode 100644 lib/util/test/get_line.c diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h index fd6de7f..9edd004 100644 --- a/bin/gensquashfs/src/mkfs.h +++ b/bin/gensquashfs/src/mkfs.h @@ -13,6 +13,7 @@ #include "common.h" #include "io/dir_iterator.h" #include "util/util.h" +#include "util/parse.h" #ifdef HAVE_SYS_XATTR_H #include diff --git a/include/fstree.h b/include/fstree.h index 779cd28..3c3782e 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -16,7 +16,6 @@ #include "sqfs/predef.h" #include "io/dir_entry.h" -#include "io/istream.h" #include "compat.h" typedef struct fstree_defaults_t fstree_defaults_t; diff --git a/include/io/dir_iterator.h b/include/io/dir_iterator.h index 152eb93..1ea3321 100644 --- a/include/io/dir_iterator.h +++ b/include/io/dir_iterator.h @@ -8,7 +8,7 @@ #define IO_DIR_ITERATOR_H #include "io/dir_entry.h" -#include "io/istream.h" +#include "sqfs/predef.h" /** * @interface dir_iterator_t diff --git a/include/io/istream.h b/include/io/istream.h deleted file mode 100644 index f41be2e..0000000 --- a/include/io/istream.h +++ /dev/null @@ -1,55 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.h - * - * Copyright (C) 2019 David Oberhollenzer - */ -#ifndef IO_ISTREAM_H -#define IO_ISTREAM_H - -#include "sqfs/predef.h" - -enum { - ISTREAM_LINE_LTRIM = 0x01, - ISTREAM_LINE_RTRIM = 0x02, - ISTREAM_LINE_SKIP_EMPTY = 0x04, -}; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Read a line of text from an input stream - * - * @memberof sqfs_istream_t - * - * The line returned is allocated using malloc and must subsequently be - * freed when it is no longer needed. The line itself is always null-terminated - * and never includes the line break characters (LF or CR-LF). - * - * If the flag @ref ISTREAM_LINE_LTRIM is set, leading white space characters - * are removed. If the flag @ref ISTREAM_LINE_RTRIM is set, trailing white space - * characters are remvoed. - * - * If the flag @ref ISTREAM_LINE_SKIP_EMPTY is set and a line is discovered to - * be empty (after the optional trimming), the function discards the empty line - * and retries. The given line_num pointer is used to increment the line - * number. - * - * @param strm A pointer to an input stream. - * @param out Returns a pointer to a line on success. - * @param line_num This is incremented if lines are skipped. - * @param flags A combination of flags controling the functions behaviour. - * - * @return Zero on success, a negative value on error, a positive value if - * end-of-file was reached without reading any data. - */ -SQFS_INTERNAL int istream_get_line(sqfs_istream_t *strm, char **out, - size_t *line_num, int flags); - -#ifdef __cplusplus -} -#endif - -#endif /* IO_ISTREAM_H */ diff --git a/include/io/mem.h b/include/io/mem.h index c848ef0..20b04ab 100644 --- a/include/io/mem.h +++ b/include/io/mem.h @@ -7,7 +7,7 @@ #ifndef IO_MEM_STREAM_H #define IO_MEM_STREAM_H -#include "io/istream.h" +#include "sqfs/predef.h" #ifdef __cplusplus extern "C" { diff --git a/include/io/std.h b/include/io/std.h index c5dddfa..a9f436a 100644 --- a/include/io/std.h +++ b/include/io/std.h @@ -7,7 +7,7 @@ #ifndef IO_STD_H #define IO_STD_H -#include "io/istream.h" +#include "sqfs/io.h" #ifdef __cplusplus extern "C" { diff --git a/include/io/xfrm.h b/include/io/xfrm.h index f0ff1ce..21ffa57 100644 --- a/include/io/xfrm.h +++ b/include/io/xfrm.h @@ -7,7 +7,7 @@ #ifndef IO_XFRM_H #define IO_XFRM_H -#include "io/istream.h" +#include "sqfs/predef.h" #include "xfrm/stream.h" #ifdef __cplusplus diff --git a/include/tar/tar.h b/include/tar/tar.h index 1f1b1b4..52a0562 100644 --- a/include/tar/tar.h +++ b/include/tar/tar.h @@ -9,7 +9,6 @@ #include "config.h" #include "compat.h" -#include "io/istream.h" #include "io/dir_iterator.h" #include "sqfs/io.h" diff --git a/include/util/parse.h b/include/util/parse.h new file mode 100644 index 0000000..bc635ad --- /dev/null +++ b/include/util/parse.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * parse.h + * + * Copyright (C) 2019 David Oberhollenzer + */ +#ifndef UTIL_PARSE_H +#define UTIL_PARSE_H + +#include "sqfs/predef.h" + +enum { + ISTREAM_LINE_LTRIM = 0x01, + ISTREAM_LINE_RTRIM = 0x02, + ISTREAM_LINE_SKIP_EMPTY = 0x04, +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Read a line of text from an input stream + * + * @memberof sqfs_istream_t + * + * The line returned is allocated using malloc and must subsequently be + * freed when it is no longer needed. The line itself is always null-terminated + * and never includes the line break characters (LF or CR-LF). + * + * If the flag @ref ISTREAM_LINE_LTRIM is set, leading white space characters + * are removed. If the flag @ref ISTREAM_LINE_RTRIM is set, trailing white space + * characters are remvoed. + * + * If the flag @ref ISTREAM_LINE_SKIP_EMPTY is set and a line is discovered to + * be empty (after the optional trimming), the function discards the empty line + * and retries. The given line_num pointer is used to increment the line + * number. + * + * @param strm A pointer to an input stream. + * @param out Returns a pointer to a line on success. + * @param line_num This is incremented if lines are skipped. + * @param flags A combination of flags controling the functions behaviour. + * + * @return Zero on success, a negative value on error, a positive value if + * end-of-file was reached without reading any data. + */ +SQFS_INTERNAL int istream_get_line(sqfs_istream_t *strm, char **out, + size_t *line_num, int flags); + +#ifdef __cplusplus +} +#endif + +#endif /* UTIL_PARSE_H */ 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 - */ -#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 - */ -#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; -} diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index a797930..1ca4802 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -1,6 +1,6 @@ libutil_a_SOURCES = include/util/util.h include/util/str_table.h \ include/util/hash_table.h include/util/test.h include/util/rbtree.h \ - include/util/array.h include/util/threadpool.h \ + include/util/array.h include/util/threadpool.h include/util/parse.h \ include/util/w32threadwrap.h include/util/mempool.h \ lib/util/src/str_table.c lib/util/src/alloc.c lib/util/src/rbtree.c \ lib/util/src/array.c lib/util/src/xxhash.c lib/util/src/hash_table.c \ @@ -8,7 +8,8 @@ libutil_a_SOURCES = include/util/util.h include/util/str_table.h \ lib/util/src/is_memory_zero.c lib/util/src/mkdir_p.c \ lib/util/src/canonicalize_name.c lib/util/src/filename_sane.c \ lib/util/src/source_date_epoch.c lib/util/src/file_cmp.c \ - lib/util/src/hex_decode.c lib/util/src/base64_decode.c + lib/util/src/hex_decode.c lib/util/src/base64_decode.c \ + lib/util/src/get_line.c libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS) @@ -76,10 +77,13 @@ test_hex_decode_LDADD = libutil.a libcompat.a test_base64_decode_SOURCES = lib/util/test/base64_decode.c test_base64_decode_LDADD = libutil.a libcompat.a +test_get_line_SOURCES = lib/util/test/get_line.c +test_get_line_LDADD = libutil.a libio.a libcompat.a + LIBUTIL_TESTS = \ test_str_table test_rbtree test_xxhash test_threadpool test_ismemzero \ test_canonicalize_name test_filename_sane test_filename_sane_w32 \ - test_sdate_epoch test_hex_decode test_base64_decode + test_sdate_epoch test_hex_decode test_base64_decode test_get_line check_PROGRAMS += $(LIBUTIL_TESTS) TESTS += $(LIBUTIL_TESTS) diff --git a/lib/util/src/get_line.c b/lib/util/src/get_line.c new file mode 100644 index 0000000..5a17b62 --- /dev/null +++ b/lib/util/src/get_line.c @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * get_line.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "util/parse.h" +#include "sqfs/io.h" +#include "sqfs/error.h" +#include "compat.h" + +#include +#include +#include + +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: { + os_error_t temp = get_os_error_state(); + free(line); + *out = NULL; + set_os_error_state(temp); + return err; +} +out_eof: + free(line); + *out = NULL; + return 1; +} diff --git a/lib/util/test/get_line.c b/lib/util/test/get_line.c new file mode 100644 index 0000000..6189d31 --- /dev/null +++ b/lib/util/test/get_line.c @@ -0,0 +1,178 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * get_line.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "util/parse.h" +#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; +} diff --git a/lib/util/test/str_table.c b/lib/util/test/str_table.c index b146773..153d09e 100644 --- a/lib/util/test/str_table.c +++ b/lib/util/test/str_table.c @@ -7,8 +7,8 @@ #include "config.h" #include "util/str_table.h" +#include "util/parse.h" #include "compat.h" -#include "io/istream.h" #include "util/test.h" #include "sqfs/io.h" -- cgit v1.2.3