Update wapp array C API

This commit is contained in:
2025-12-07 21:56:16 +00:00
committed by Abdelrahman Said
parent 551836d4c8
commit 2b49bfcdbf
10 changed files with 365 additions and 3991 deletions

View File

@@ -66,8 +66,8 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) {
"`dst`, `dst->items` and `file` should not be NULL.");
u64 file_length = wapp_file_get_length(file);
u64 dst_byte_capacity = dst->item_size * dst->capacity;
u64 req_byte_count = item_count * dst->item_size;
u64 dst_byte_capacity = dst->header.item_size * dst->header.capacity;
u64 req_byte_count = item_count * dst->header.item_size;
u64 copy_byte_count = 0;
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
@@ -76,17 +76,17 @@ 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->count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->item_size;
dst->header.count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->header.item_size;
return dst->count;
return dst->header.count;
}
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 src_byte_count = src->count * src->item_size;
u64 req_byte_count = item_count * src->item_size;
u64 src_byte_count = src->header.count * src->header.item_size;
u64 req_byte_count = item_count * src->header.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);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "str8.h"
#include "../../array/array.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
#include "../../mem_allocator/mem_allocator.h"
@@ -260,14 +261,15 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) {
}
}
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) {
u64 size = src->count * src->item_size;
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);
wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL");
wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity");
dst->size = size;
memcpy(dst->buf, src->items, size);
memcpy(dst->buf, src_byte_array, size);
}
i64 wapp_str8_find(Str8RO *str, Str8RO substr) {

View File

@@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
void wapp_str8_format(Str8 *dst, const char *format, ...);
void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
void wapp_str8_to_upper(Str8 *dst, Str8RO *src);
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src);
void wapp_str8_from_bytes(Str8 *dst, const u8 *src);
/**
* Str8 find functions