From 938b15adb224a4bdbf14e4688c30b28e9b8424da Mon Sep 17 00:00:00 2001 From: Tom Sullivan Date: Fri, 1 Jul 2022 13:58:34 +1000 Subject: [PATCH 1/4] Make `fread_and_discard` just a shim for `fseek` --- src/audio/wav_io.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/audio/wav_io.c b/src/audio/wav_io.c index 38deab0..8e725dc 100644 --- a/src/audio/wav_io.c +++ b/src/audio/wav_io.c @@ -18,9 +18,7 @@ static bool expect_data(const char* expected, int expected_size, } static void fread_and_discard(int size, FILE* file) { - uint8_t* data = calloc(1, size); - fread(data, size, 1, file); - free(data); + fseek(file, size, SEEK_CUR); } static uint16_t fread_uint16(FILE* file) { From 4c013a768617119ad125329003e86933dd2f10d8 Mon Sep 17 00:00:00 2001 From: Tom Sullivan Date: Fri, 1 Jul 2022 13:59:51 +1000 Subject: [PATCH 2/4] Replace uses of `fread_and_discard` with `fseek` --- src/audio/wav_io.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/audio/wav_io.c b/src/audio/wav_io.c index 8e725dc..e89cfea 100644 --- a/src/audio/wav_io.c +++ b/src/audio/wav_io.c @@ -68,7 +68,7 @@ bool wav_io_load(const char* filename, AudioBuffer** result) { fread(found_chunk_id, 4, 1, file); while (memcmp(found_chunk_id, "fmt ", 4) != 0) { const uint32_t chunk_size = fread_uint32(file); - fread_and_discard(chunk_size, file); + fseek(file, chunk_size, SEEK_CUR); fread(found_chunk_id, 4, 1, file); } const uint32_t format_chunk_size = fread_uint32(file); @@ -97,13 +97,13 @@ bool wav_io_load(const char* filename, AudioBuffer** result) { return false; } if (format_chunk_size == 18) { - fread_and_discard(2, file); + fseek(file, 2, SEEK_CUR); } fread(found_chunk_id, 4, 1, file); while (memcmp(found_chunk_id, "data", 4) != 0) { const uint32_t chunk_size = fread_uint32(file); - fread_and_discard(chunk_size, file); + fseek(file, chunk_size, SEEK_CUR); } const uint32_t chunk_size = fread_uint32(file); From 364a415664b37438dd626924e066ae6e9571a77b Mon Sep 17 00:00:00 2001 From: Tom Sullivan Date: Fri, 1 Jul 2022 14:01:32 +1000 Subject: [PATCH 3/4] Remove `test_fread_and_discard` As there are no uses of `fread_and_discard`, remove this test, which is basically just testing `fseek` now. --- src/audio/wav_io_test.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/audio/wav_io_test.c b/src/audio/wav_io_test.c index 87c915e..b1df123 100644 --- a/src/audio/wav_io_test.c +++ b/src/audio/wav_io_test.c @@ -19,20 +19,6 @@ void test_expect_data() { fclose(file); } -void test_fread_and_discard() { - const char* test_filename = "/tmp/test_fread_and_discard"; - file_write(test_filename, "FooBarBaz", 9); - - FILE* file = fopen(test_filename, "rb"); - TEST_CHECK(file != NULL); - - TEST_CHECK(expect_data("Foo", 3, file)); - fread_and_discard(3, file); - TEST_CHECK(expect_data("Baz", 3, file)); - - fclose(file); -} - void test_fread_uint16() { const char* test_filename = "/tmp/test_fread_uint16"; uint16_t value = 0x3123; @@ -207,7 +193,6 @@ void test_wav_io_save_listenable() { TEST_LIST = { {"expect_data", test_expect_data}, - {"fread_and_discard", test_fread_and_discard}, {"fread_uint16", test_fread_uint16}, {"fwrite_uint16", test_fwrite_uint16}, {"fread_uint32", test_fread_uint32}, From daa37baabbf926e2b454c4ebab5456f8c5a6cd5f Mon Sep 17 00:00:00 2001 From: Tom Sullivan Date: Fri, 1 Jul 2022 14:02:47 +1000 Subject: [PATCH 4/4] Remove `fread_and_discard` --- src/audio/wav_io.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/audio/wav_io.c b/src/audio/wav_io.c index e89cfea..a67a36b 100644 --- a/src/audio/wav_io.c +++ b/src/audio/wav_io.c @@ -17,10 +17,6 @@ static bool expect_data(const char* expected, int expected_size, return result; } -static void fread_and_discard(int size, FILE* file) { - fseek(file, size, SEEK_CUR); -} - static uint16_t fread_uint16(FILE* file) { uint16_t result; fread(&result, 2, 1, file);