Arrays without code generation (#6)
Co-authored-by: Abdelrahman Said <said.abdelrahman@flawlessai.com> Reviewed-on: #6 Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com> Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit was merged in pull request #6.
This commit is contained in:
@@ -14,7 +14,12 @@ BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#define wapp_static_assert(EXPR, MSG) extern char ASSERTION_FAILED[EXPR ? 1 : -1]
|
||||
#define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG)
|
||||
|
||||
#ifndef WAPP_NO_RUNTIME_ASSERT
|
||||
#define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG)
|
||||
#else
|
||||
#define wapp_runtime_assert(EXPR, MSG)
|
||||
#endif
|
||||
|
||||
#ifdef WAPP_DEBUG_ASSERT
|
||||
#define wapp_debug_assert(EXPR, MSG) wapp_runtime_assert(EXPR, MSG)
|
||||
|
||||
@@ -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->item_size * dst->capacity;
|
||||
u64 req_byte_count = item_count * dst->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->count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->item_size;
|
||||
u64 count = fread(dst_byte_array, sizeof(u8), copy_byte_count, file);
|
||||
if (ferror(file)) { return 0; }
|
||||
|
||||
return dst->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->count * src->item_size;
|
||||
u64 req_byte_count = item_count * src->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) {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#define FILE_H
|
||||
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../primitives/array/array.h"
|
||||
#include "../../primitives/strings/str8/str8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -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(src_byte_array) * wapp_array_item_size(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) {
|
||||
|
||||
@@ -37,14 +37,14 @@ typedef const Str8 Str8RO;
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
// Uses a lambda to achieve the same behaviour achieved by the C macro
|
||||
#define wapp_str8_buf(CAPACITY) ([&](){ \
|
||||
wapp_persist c8 buf[CAPACITY] = {}; \
|
||||
wapp_persist c8 buf[CAPACITY] = {}; \
|
||||
memset(buf, 0, CAPACITY); \
|
||||
return Str8{CAPACITY, 0, buf}; \
|
||||
}())
|
||||
|
||||
// Uses a lambda to achieve the same behaviour achieved by the C macro
|
||||
#define wapp_str8_lit(STRING) ([&]() { \
|
||||
wapp_persist c8 buf[sizeof(STRING) * 2] = {}; \
|
||||
wapp_persist c8 buf[sizeof(STRING) * 2] = {}; \
|
||||
memcpy(buf, STRING, sizeof(STRING)); \
|
||||
return Str8{(sizeof(STRING) - 1) * 2, sizeof(STRING) - 1, buf}; \
|
||||
}())
|
||||
@@ -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_byte_array);
|
||||
|
||||
/**
|
||||
* Str8 find functions
|
||||
|
||||
Reference in New Issue
Block a user