Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fread_and_discard vs fseek #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/audio/wav_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ static bool expect_data(const char* expected, int expected_size,
return result;
}

static void fread_and_discard(int size, FILE* file) {
uint8_t* data = calloc(1, size);
fread(data, size, 1, file);
free(data);
}

static uint16_t fread_uint16(FILE* file) {
uint16_t result;
fread(&result, 2, 1, file);
Expand Down Expand Up @@ -70,7 +64,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);
Expand Down Expand Up @@ -99,13 +93,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);
Expand Down
15 changes: 0 additions & 15 deletions src/audio/wav_io_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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},
Expand Down