diff --git a/src/core/file/file.c b/src/core/file/file.c index a0fe059..1885211 100644 --- a/src/core/file/file.c +++ b/src/core/file/file.c @@ -61,13 +61,14 @@ u64 wapp_file_get_length(File *file) { return output; } -u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { - wapp_debug_assert(dst != NULL && dst->items != NULL && file != NULL, - "`dst`, `dst->items` and `file` should not be NULL."); +u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count) { + wapp_debug_assert(dst_byte_array != NULL && file != NULL, + "`dst_byte_array` and `file` should not be NULL."); u64 file_length = wapp_file_get_length(file); - u64 dst_byte_capacity = dst->header.item_size * dst->header.capacity; - u64 req_byte_count = item_count * dst->header.item_size; + u64 item_size = wapp_array_item_size(dst_byte_array); + u64 dst_byte_capacity = wapp_array_capacity(dst_byte_array) * item_size; + u64 req_byte_count = item_count * item_size; u64 copy_byte_count = 0; if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) { @@ -76,20 +77,24 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) { copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; } - dst->header.count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->header.item_size; + u64 count = fread(dst_byte_array, sizeof(u8), copy_byte_count, file); + if (ferror(file)) { return 0; } - return dst->header.count; + wapp_array_set_count(dst_byte_array, count / item_size); + + return wapp_array_count(dst_byte_array); } -u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count) { - wapp_debug_assert(src != NULL && src->items != NULL && file != NULL, - "`src`, `src->items` and `file` should not be NULL."); +u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count) { + wapp_debug_assert(src_byte_array != NULL && file != NULL, + "`src_byte_array` and `file` should not be NULL."); - u64 src_byte_count = src->header.count * src->header.item_size; - u64 req_byte_count = item_count * src->header.item_size; + u64 item_size = wapp_array_item_size(src_byte_array); + u64 src_byte_count = wapp_array_count(src_byte_array) * item_size; + u64 req_byte_count = item_count * item_size; u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count; - return fwrite(src->items, sizeof(u8), to_copy, file); + return fwrite(src_byte_array, sizeof(u8), to_copy, file); } i32 wapp_file_flush(File *file) { diff --git a/src/core/file/file.h b/src/core/file/file.h index 7822e68..0a27bd6 100644 --- a/src/core/file/file.h +++ b/src/core/file/file.h @@ -4,7 +4,6 @@ #define FILE_H #include "../../common/aliases/aliases.h" -#include "../../primitives/array/array.h" #include "../../primitives/strings/str8/str8.h" #include @@ -12,21 +11,6 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP -#ifdef WAPP_PLATFORM_CPP -#define wapp_file_item_to_array(ITEM) (GenericArray{&(ITEM), 1, 1, sizeof(ITEM)}) -#define wapp_file_array_to_item(TYPE, ARRAY) (sizeof(TYPE) == (ARRAY).item_size && (ARRAY).count == 1 ? \ - *((TYPE *)((ARRAY).items)) : \ - TYPE{}) -#else -#define wapp_file_item_to_array(ITEM) ((GenericArray){.items = &(ITEM), \ - .count = 1, \ - .capacity = 1, \ - .item_size = sizeof(ITEM)}) -#define wapp_file_array_to_item(TYPE, ARRAY) (sizeof(TYPE) == (ARRAY).item_size && (ARRAY).count == 1 ? \ - *((TYPE *)((ARRAY).items)) : \ - (TYPE){0}) -#endif // !WAPP_PLATFORM_CPP - typedef FILE File; typedef enum { @@ -60,8 +44,8 @@ File *wapp_file_open(Str8RO *filename, FileAccessMode mode); u64 wapp_file_get_current_position(File *file); i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin); u64 wapp_file_get_length(File *file); -u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count); -u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count); +u64 wapp_file_read(u8 *dst_byte_array, File *file, u64 item_count); +u64 wapp_file_write(const u8 *src_byte_array, File *file, u64 item_count); i32 wapp_file_flush(File *file); i32 wapp_file_close(File *file); diff --git a/src/primitives/array/array.c b/src/primitives/array/array.c index c2287c2..86c5cc1 100644 --- a/src/primitives/array/array.c +++ b/src/primitives/array/array.c @@ -10,36 +10,42 @@ #define _array_header(DATA_PTR) (ArrayHeader *)((u8 *)DATA_PTR - sizeof(ArrayHeader)) -u64 _array_count(u8 *array, u64 item_size) { +u64 _array_count(u8 *array) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); ArrayHeader *arr_header = _array_header(array); wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); return arr_header->count; } -u64 _array_capacity(u8 *array, u64 item_size) { +u64 _array_capacity(u8 *array) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); ArrayHeader *arr_header = _array_header(array); wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); return arr_header->capacity; } -u64 _array_item_size(u8 *array, u64 item_size) { +u64 _array_item_size(u8 *array) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); ArrayHeader *arr_header = _array_header(array); wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); - wapp_runtime_assert(item_size == arr_header->item_size, "Invalid item type provided"); return arr_header->item_size; } +void _array_set_count(u8 *array, u64 count) { + wapp_debug_assert(array != NULL, "`array` should not be NULL"); + + ArrayHeader *arr_header = _array_header(array); + wapp_runtime_assert(WAPP_ARRAY_MAGIC == arr_header->magic, "`array` is not a valid wapp array"); + + arr_header->count = count; +} + u8 *_array_get(u8 *array, u64 index, u64 item_size) { wapp_debug_assert(array != NULL, "`array` should not be NULL"); diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 9159cf6..d8a2162 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -51,7 +51,7 @@ BEGIN_C_LINKAGE return (TYPE *)(header + 1); \ }()) #define wapp_array_pop(TYPE, ARRAY_PTR) \ - (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \ + (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR) > 0 ? \ *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ TYPE{} \ ) @@ -82,18 +82,20 @@ BEGIN_C_LINKAGE }.items \ ) #define wapp_array_pop(TYPE, ARRAY_PTR) \ - (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) > 0 ? \ + (ARRAY_PTR != NULL && _array_count((u8 *)ARRAY_PTR) > 0 ? \ *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))) : \ (TYPE){0} \ ) #endif // !WAPP_PLATFORM_CPP -#define wapp_array_count(TYPE, ARRAY_PTR) \ - _array_count((u8 *)ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_capacity(TYPE, ARRAY_PTR) \ - _array_capacity((u8 *)ARRAY_PTR, sizeof(TYPE)) -#define wapp_array_item_size(TYPE, ARRAY_PTR) \ - _array_item_size((u8 *)ARRAY_PTR, sizeof(TYPE)) +#define wapp_array_count(ARRAY_PTR) \ + _array_count((u8 *)ARRAY_PTR) +#define wapp_array_capacity(ARRAY_PTR) \ + _array_capacity((u8 *)ARRAY_PTR) +#define wapp_array_item_size(ARRAY_PTR) \ + _array_item_size((u8 *)ARRAY_PTR) +#define wapp_array_set_count(ARRAY_PTR, COUNT) \ + _array_set_count((u8 *)ARRAY_PTR, COUNT) #define wapp_array_get(TYPE, ARRAY_PTR, INDEX) \ ((TYPE *)_array_get((u8 *)ARRAY_PTR, INDEX, sizeof(TYPE))) #define wapp_array_set(TYPE, ARRAY_PTR, INDEX, VALUE_PTR) \ @@ -127,9 +129,10 @@ struct GenericArray { void *items; }; -u64 _array_count(u8 *array, u64 item_size); -u64 _array_capacity(u8 *array, u64 item_size); -u64 _array_item_size(u8 *array, u64 item_size); +u64 _array_count(u8 *array); +u64 _array_capacity(u8 *array); +u64 _array_item_size(u8 *array); +void _array_set_count(u8 *array, u64 count); u8 *_array_get(u8 *array, u64 index, u64 item_size); void _array_set(u8 *array, u64 index, u8 *value, u64 item_size); void _array_append_capped(u8 *array, u8 *value, u64 item_size); diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index c03b748..6e14884 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -264,7 +264,7 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) { void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array) { wapp_debug_assert(src_byte_array != NULL && dst != NULL, "`dst` and `src` should not be NULL"); - u64 size = wapp_array_count(u8, src_byte_array) * wapp_array_item_size(u8, src_byte_array); + u64 size = wapp_array_count(src_byte_array) * wapp_array_item_size(src_byte_array); wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); diff --git a/tests/array/test_i32_array.c b/tests/array/test_i32_array.c index da01b7f..f6a6698 100644 --- a/tests/array/test_i32_array.c +++ b/tests/array/test_i32_array.c @@ -5,10 +5,10 @@ TestFuncResult test_i32_array(void) { b8 result; i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); - result = wapp_array_count(i32, array) == 7 && wapp_array_capacity(i32, array) == 16; + result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -26,7 +26,7 @@ TestFuncResult test_i32_array_with_capacity(void) { b8 result; i32 *array = wapp_array_with_capacity(i32, 64); - result = wapp_array_count(i32, array) == 0 && wapp_array_capacity(i32, array) == 64; + result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; return wapp_tester_result(result); } @@ -37,7 +37,7 @@ TestFuncResult test_i32_array_get(void) { i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -57,7 +57,7 @@ TestFuncResult test_i32_array_set(void) { i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -79,14 +79,14 @@ TestFuncResult test_i32_array_append_capped(void) { i32 *array = wapp_array_with_capacity(i32, 64); wapp_array_append_capped(i32, array, &((i32){10})); - result = wapp_array_count(i32, array) == 1; + result = wapp_array_count(array) == 1; i32 *item = wapp_array_get(i32, array, 0); result = result && item && *item == 10; array = wapp_array(i32, 1); wapp_array_append_capped(i32, array, &((i32){10})); - result = result && wapp_array_count(i32, array) == 2; + result = result && wapp_array_count(array) == 2; return wapp_tester_result(result); } @@ -97,11 +97,11 @@ TestFuncResult test_i32_array_extend_capped(void) { i32 *array1 = wapp_array(i32, 1, 2, 3, 4); i32 *array2 = wapp_array(i32, 10, 20); - result = wapp_array_count(i32, array1) == 4 && wapp_array_count(i32, array2) == 2; + result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; wapp_array_extend_capped(i32, array1, array2); - result = result && wapp_array_count(i32, array1) == 6; + result = result && wapp_array_count(array1) == 6; return wapp_tester_result(result); } @@ -115,7 +115,7 @@ TestFuncResult test_i32_array_copy_capped(void) { u64 expected_count = 5; wapp_array_copy_capped(i32, dst1, src); - result = wapp_array_count(i32, dst1) == expected_count; + result = wapp_array_count(dst1) == expected_count; u64 index = 0; b8 running = true; @@ -128,7 +128,7 @@ TestFuncResult test_i32_array_copy_capped(void) { expected_count = 4; wapp_array_copy_capped(i32, dst2, src); - result = result && wapp_array_count(i32, dst2) == expected_count; + result = result && wapp_array_count(dst2) == expected_count; index = 0; running = true; @@ -149,7 +149,7 @@ TestFuncResult test_i32_array_alloc_capacity(void) { u64 capacity = 32; i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); - result = array && wapp_array_capacity(i32, array) == capacity; + result = array && wapp_array_capacity(array) == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -213,7 +213,7 @@ TestFuncResult test_i32_array_copy_alloc(void) { u64 expected_count = 5; array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); - result = wapp_array_count(i32, array_ptr) == expected_count && array_ptr == dst1; + result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; u64 index = 0; b8 running = true; @@ -226,7 +226,7 @@ TestFuncResult test_i32_array_copy_alloc(void) { expected_count = 5; array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); - result = result && wapp_array_count(i32, array_ptr) == expected_count && array_ptr != dst2; + result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; index = 0; running = true; @@ -260,11 +260,11 @@ TestFuncResult test_i32_array_clear(void) { b8 result; i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - result = wapp_array_count(i32, array) == 9; + result = wapp_array_count(array) == 9; wapp_array_clear(i32, array); - result = result && wapp_array_count(i32, array) == 0; + result = result && wapp_array_count(array) == 0; return wapp_tester_result(result); } diff --git a/tests/array/test_i32_array.cc b/tests/array/test_i32_array.cc index 19ff9dc..bb8d066 100644 --- a/tests/array/test_i32_array.cc +++ b/tests/array/test_i32_array.cc @@ -5,10 +5,10 @@ TestFuncResult test_i32_array(void) { b8 result; i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); - result = wapp_array_count(i32, array) == 7 && wapp_array_capacity(i32, array) == 16; + result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -26,7 +26,7 @@ TestFuncResult test_i32_array_with_capacity(void) { b8 result; i32 *array = wapp_array_with_capacity(i32, 64); - result = wapp_array_count(i32, array) == 0 && wapp_array_capacity(i32, array) == 64; + result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; return wapp_tester_result(result); } @@ -37,7 +37,7 @@ TestFuncResult test_i32_array_get(void) { i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -57,7 +57,7 @@ TestFuncResult test_i32_array_set(void) { i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(i32, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { @@ -80,7 +80,7 @@ TestFuncResult test_i32_array_append_capped(void) { i32 item1 = 10; wapp_array_append_capped(i32, array, &item1); - result = wapp_array_count(i32, array) == 1; + result = wapp_array_count(array) == 1; i32 *item = wapp_array_get(i32, array, 0); result = result && item && *item == 10; @@ -88,7 +88,7 @@ TestFuncResult test_i32_array_append_capped(void) { i32 item2 = 10; wapp_array_append_capped(i32, array, &item2); - result = result && wapp_array_count(i32, array) == 2; + result = result && wapp_array_count(array) == 2; return wapp_tester_result(result); } @@ -99,11 +99,11 @@ TestFuncResult test_i32_array_extend_capped(void) { i32 *array1 = wapp_array(i32, 1, 2, 3, 4); i32 *array2 = wapp_array(i32, 10, 20); - result = wapp_array_count(i32, array1) == 4 && wapp_array_count(i32, array2) == 2; + result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; wapp_array_extend_capped(i32, array1, array2); - result = result && wapp_array_count(i32, array1) == 6; + result = result && wapp_array_count(array1) == 6; return wapp_tester_result(result); } @@ -117,7 +117,7 @@ TestFuncResult test_i32_array_copy_capped(void) { u64 expected_count = 5; wapp_array_copy_capped(i32, dst1, src); - result = wapp_array_count(i32, dst1) == expected_count; + result = wapp_array_count(dst1) == expected_count; u64 index = 0; b8 running = true; @@ -130,7 +130,7 @@ TestFuncResult test_i32_array_copy_capped(void) { expected_count = 4; wapp_array_copy_capped(i32, dst2, src); - result = result && wapp_array_count(i32, dst2) == expected_count; + result = result && wapp_array_count(dst2) == expected_count; index = 0; running = true; @@ -151,7 +151,7 @@ TestFuncResult test_i32_array_alloc_capacity(void) { u64 capacity = 32; i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); - result = array && wapp_array_capacity(i32, array) == capacity; + result = array && wapp_array_capacity(array) == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -216,7 +216,7 @@ TestFuncResult test_i32_array_copy_alloc(void) { u64 expected_count = 5; array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); - result = wapp_array_count(i32, array_ptr) == expected_count && array_ptr == dst1; + result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; u64 index = 0; b8 running = true; @@ -229,7 +229,7 @@ TestFuncResult test_i32_array_copy_alloc(void) { expected_count = 5; array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); - result = result && wapp_array_count(i32, array_ptr) == expected_count && array_ptr != dst2; + result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; index = 0; running = true; @@ -249,11 +249,11 @@ TestFuncResult test_i32_array_clear(void) { b8 result; i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - result = wapp_array_count(i32, array) == 9; + result = wapp_array_count(array) == 9; wapp_array_clear(i32, array); - result = result && wapp_array_count(i32, array) == 0; + result = result && wapp_array_count(array) == 0; return wapp_tester_result(result); } diff --git a/tests/array/test_str8_array.c b/tests/array/test_str8_array.c index 64337ba..484691d 100644 --- a/tests/array/test_str8_array.c +++ b/tests/array/test_str8_array.c @@ -7,10 +7,10 @@ TestFuncResult test_str8_array(void) { Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; Str8 *array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); - result = wapp_array_count(Str8, array) == 3 && wapp_array_capacity(Str8, array) == 8; + result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; Str8 *item; - u64 count = wapp_array_count(Str8, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { diff --git a/tests/array/test_str8_array.cc b/tests/array/test_str8_array.cc index 2cf02e1..74fd544 100644 --- a/tests/array/test_str8_array.cc +++ b/tests/array/test_str8_array.cc @@ -12,10 +12,10 @@ TestFuncResult test_str8_array(void) { Str8 str3 = wapp_str8_lit("Bye"); Str8 *array = wapp_array(Str8, str1, str2, str3); - result = wapp_array_count(Str8, array) == 3 && wapp_array_capacity(Str8, array) == 8; + result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; Str8 *item; - u64 count = wapp_array_count(Str8, array); + u64 count = wapp_array_count(array); u64 index = 0; b8 running = true; while (running) { diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 834d415..9694842 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -619,7 +619,7 @@ TestFuncResult test_str8_from_bytes(void) { u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); wapp_str8_from_bytes(&str, bytes); - result = str.size == wapp_array_count(u8, bytes) * wapp_array_item_size(u8, bytes); + result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); return wapp_tester_result(result); diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc index b6b63fc..ffe27ea 100644 --- a/tests/str8/test_str8.cc +++ b/tests/str8/test_str8.cc @@ -620,7 +620,7 @@ TestFuncResult test_str8_from_bytes(void) { u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); wapp_str8_from_bytes(&str, bytes); - result = str.size == wapp_array_count(u8, bytes) * wapp_array_item_size(u8, bytes); + result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); result = result && wapp_str8_equal(&str, &expected); return wapp_tester_result(result);