diff options
author | EuAndreh <eu@euandre.org> | 2024-04-07 11:49:25 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-04-07 11:49:25 -0300 |
commit | 381492a000454822398a8737c73ea586464ee56a (patch) | |
tree | 9e28124ba2373b157356f0d5172b60009a20041e | |
parent | src/lib.c: Print project metadata on pindaiba_main (diff) | |
download | pindaiba-381492a000454822398a8737c73ea586464ee56a.tar.gz pindaiba-381492a000454822398a8737c73ea586464ee56a.tar.xz |
src/util.c: Add slurp(), with a simple test
-rw-r--r-- | deps.mk | 9 | ||||
-rw-r--r-- | src/util.c | 74 | ||||
-rw-r--r-- | src/util.h | 2 | ||||
-rw-r--r-- | tests/logerr.c | 1 | ||||
-rw-r--r-- | tests/slurp.c | 37 | ||||
-rw-r--r-- | tests/slurp.h | 2 | ||||
-rw-r--r-- | tests/util.c | 66 |
7 files changed, 174 insertions, 17 deletions
@@ -9,6 +9,7 @@ sources.c = \ src/logerr.c \ src/random.c \ src/testing.c \ + src/util.c \ tests.c = \ tests/catalog.c \ @@ -17,6 +18,7 @@ tests.c = \ tests/logerr.c \ tests/random.c \ tests/testing.c \ + tests/util.c \ src/catalog.o: src/catalog.h src/i18n.o: src/i18n.h @@ -24,6 +26,7 @@ src/lib.o: src/lib.h src/logerr.o: src/logerr.h src/random.o: src/random.h src/testing.o: src/testing.h +src/util.o: src/util.h tests/catalog.o: src/catalog.c src/catalog.h tests/i18n.o: src/i18n.c src/i18n.h @@ -31,6 +34,7 @@ tests/lib.o: src/lib.c src/lib.h tests/logerr.o: src/logerr.c src/logerr.h tests/random.o: src/random.c src/random.h tests/testing.o: src/testing.c src/testing.h +tests/util.o: src/util.c src/util.h tests/catalog.a: tests/catalog.o tests/i18n.a: tests/i18n.o @@ -38,6 +42,7 @@ tests/lib.a: tests/lib.o tests/logerr.a: tests/logerr.o tests/random.a: tests/random.o tests/testing.a: tests/testing.o +tests/util.a: tests/util.o tests/catalog.bin-check: tests/catalog.bin tests/i18n.bin-check: tests/i18n.bin @@ -45,6 +50,7 @@ tests/lib.bin-check: tests/lib.bin tests/logerr.bin-check: tests/logerr.bin tests/random.bin-check: tests/random.bin tests/testing.bin-check: tests/testing.bin +tests/util.bin-check: tests/util.bin src/catalog.o: src/logerr.h @@ -53,6 +59,7 @@ src/lib.o: src/logerr.h src/logerr.o: src/random.o: src/logerr.h src/testing.o: +src/util.o: src/logerr.h tests/catalog.o: src/logerr.h src/testing.h tests/slurp.h tests/i18n.o: src/catalog.h src/logerr.h @@ -60,6 +67,7 @@ tests/lib.o: src/logerr.h tests/logerr.o: src/testing.h tests/slurp.h tests/random.o: src/logerr.h src/testing.h tests/testing.o: +tests/util.o: src/logerr.h src/testing.h tests/slurp.h tests/catalog.a: src/logerr.o src/testing.o tests/slurp.o tests/i18n.a: src/catalog.o src/logerr.o @@ -67,3 +75,4 @@ tests/lib.a: src/logerr.o tests/logerr.a: src/testing.o tests/slurp.o tests/random.a: src/logerr.o src/testing.o tests/testing.a: +tests/util.a: src/logerr.o src/testing.o tests/slurp.o diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..3d56c14 --- /dev/null +++ b/src/util.c @@ -0,0 +1,74 @@ +#include "config.h" + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" +#include "logerr.h" + + +static const size_t +NULL_TERMINATOR = sizeof((char)'\0'); + +int +slurp(const char *const filename, char **out) { + int rc = -1; + + FILE *file = NULL; + char *str = NULL; + + file = fopen(filename, "r"); + if (file == NULL) { + logerr("fopen(\"%s\"): %s\n", filename, strerror(errno)); + goto out; + } + + if (fseek(file, 0L, SEEK_END)) { + logerr("fseek(): %s\n", strerror(errno)); + goto out; + } + + const long lsize = ftell(file); + if (lsize == -1) { + logerr("ftell(): %s\n", strerror(errno)); + goto out; + } + const size_t size = (size_t)lsize; + + errno = 0; + rewind(file); + if (errno) { + logerr("rewind(file): %s\n", strerror(errno)); + goto out; + } + + str = malloc(size + NULL_TERMINATOR); + if (str == NULL) { + logerr("malloc(%ld): %s\n", size + NULL_TERMINATOR, strerror(errno)); + goto out; + } + + if (fread(str, sizeof(char), size, file) != size) { + logerr("fread(\"%s\"): %s\n", filename, strerror(errno)); + goto out; + } + str[size] = '\0'; + *out = str; + + rc = 0; +out: + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(\"%s\"): %s\n", filename, strerror(errno)); + rc = -1; + } + } + if (rc) { + if (str != NULL) { + free(str); + } + } + return rc; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..8465790 --- /dev/null +++ b/src/util.h @@ -0,0 +1,2 @@ +int +slurp(const char *const filename, char **out); diff --git a/tests/logerr.c b/tests/logerr.c index 86b6b1c..c692bdc 100644 --- a/tests/logerr.c +++ b/tests/logerr.c @@ -1,6 +1,5 @@ #include "../src/logerr.c" - #include <assert.h> #include <errno.h> #include <string.h> diff --git a/tests/slurp.c b/tests/slurp.c index 6ea8618..6b6a02e 100644 --- a/tests/slurp.c +++ b/tests/slurp.c @@ -1,20 +1,25 @@ #include "../src/config.h" +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include "slurp.h" + +static const size_t +NULL_TERMINATOR = sizeof((char)'\0'); + int -slurp_for_tests(const char *const FNAME, char **strref) { +slurp_for_tests(const char *const filename, char **out) { int rc = -1; FILE *file = NULL; - char *str = NULL; + char *str = NULL; - file = fopen(FNAME, "r"); - if (!file) { - perror("fopen(FNAME, \"r\")"); + file = fopen(filename, "r"); + if (file == NULL) { + perror("fopen(filename, \"r\")"); goto out; } @@ -28,29 +33,31 @@ slurp_for_tests(const char *const FNAME, char **strref) { perror("ftell(file)"); goto out; } - const size_t size = (size_t)lsize + sizeof(char); + const size_t size = (size_t)lsize; - if (fseek(file, 0L, SEEK_SET)) { - perror("fseek(file, 0L, SEEK_SET)"); + errno = 0; + rewind(file); + if (errno) { + perror("rewind(file)"); goto out; } - str = malloc(size); - if (!str) { + str = malloc(size + NULL_TERMINATOR); + if (str == NULL) { perror("malloc(...)"); goto out; } - if (fread(str, sizeof(char), size - 1, file) != size - 1) { + if (fread(str, sizeof(char), size, file) != size) { perror("fread(...)"); goto out; } - str[size - 1] = '\0'; - *strref = str; + str[size] = '\0'; + *out = str; rc = 0; out: - if (file) { + if (file != NULL) { if (fclose(file)) { perror("flcose(file"); rc = -1; @@ -58,7 +65,7 @@ out: } if (rc) { - if (str) { + if (str != NULL) { free(str); } } diff --git a/tests/slurp.h b/tests/slurp.h index fd6c082..cc8bf52 100644 --- a/tests/slurp.h +++ b/tests/slurp.h @@ -1,2 +1,2 @@ int -slurp_for_tests(const char *const FNAME, char **strref); +slurp_for_tests(const char *const filename, char **out); diff --git a/tests/util.c b/tests/util.c new file mode 100644 index 0000000..5e2e203 --- /dev/null +++ b/tests/util.c @@ -0,0 +1,66 @@ +#include "../src/util.c" + +#include <assert.h> + +#include "../src/testing.h" +#include "slurp.h" + + +static int +test_slurp(void) { + int rc = -1; + + char *given = NULL; + char *expected = NULL; + + { + testing("non-existent file"); + + const char *const filename = __FILE__ ".non-existant"; + const int ret_given = slurp(filename, &given); + const int ret_expected = slurp_for_tests(filename, &expected); + + assert(given == NULL); + assert(expected == NULL); + assert(!!ret_given == !!ret_expected); + + test_ok(); + } + { + testing("slurp() == slurp_for_tests()"); + + if (slurp(__FILE__, &given)) { + logerr("slurp(__FILE__, &given);\n"); + goto out; + } + + if (slurp_for_tests(__FILE__, &expected)) { + logerr("slurp_for_tests(__FILE__, &expected);\n"); + goto out; + } + + assert(given != NULL); + assert(expected != NULL); + assert(strcmp(given, expected) == 0); + test_ok(); + } + + rc = 0; +out: + return rc; +} + + +int +main(void) { + int rc = -1; + + if (test_slurp()) { + logerr("test_slurp();\n"); + goto out; + } + + rc = 0; +out: + return !!rc; +} |