From 4e77bc7164070d7ffafdee1ba6ce3bb1aaf10746 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 2 Mar 2026 22:44:17 +0000 Subject: feature(main): loading file + bug fixes structure improvement --- source/base/base_include.h | 3 --- source/base/base_mem.c | 17 ------------- source/base/base_mem.h | 18 +++++++++++++ source/base/base_os.h | 41 +++++++++++++++++++++++++++++- source/base/base_string.c | 63 ---------------------------------------------- source/base/base_string.h | 45 +++++++++++++++++++++++++++------ source/base/base_test.c | 0 7 files changed, 96 insertions(+), 91 deletions(-) delete mode 100644 source/base/base_string.c delete mode 100644 source/base/base_test.c (limited to 'source/base') diff --git a/source/base/base_include.h b/source/base/base_include.h index 0b0f256..40ae5ea 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h @@ -20,11 +20,8 @@ #ifdef BASE_UNITY -#include "base_mem.c" #include "base_arena.c" #include "base_stack.c" -#include "base_test.c" -#include "base_string.c" #endif #endif diff --git a/source/base/base_mem.c b/source/base/base_mem.c index f20ba39..e69de29 100644 --- a/source/base/base_mem.c +++ b/source/base/base_mem.c @@ -1,17 +0,0 @@ -internal inline b8 -is_pow(umm x) -{ - return (x & (x - 1)) == 0; -} - -internal inline u64 -align(u64 pointer, umm alignment) -{ - if ((alignment & (alignment - 1)) == 0) - { - return pointer; - } - - return (pointer + alignment - 1) & ~(alignment - 1); -} - diff --git a/source/base/base_mem.h b/source/base/base_mem.h index 945f0ce..2778fce 100644 --- a/source/base/base_mem.h +++ b/source/base/base_mem.h @@ -5,4 +5,22 @@ #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) +internal inline b8 +is_pow(umm x) +{ + return (x & (x - 1)) == 0; +} + +internal inline u64 +align(u64 pointer, umm alignment) +{ + if ((alignment & (alignment - 1)) == 0) + { + return pointer; + } + + return (pointer + alignment - 1) & ~(alignment - 1); +} + + #endif diff --git a/source/base/base_os.h b/source/base/base_os.h index ce9acae..ec0a3de 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h @@ -9,6 +9,45 @@ print(const char *str) write(STDOUT_FILENO, str, len); } -#define print(Format) print(Format) +internal string8 +load_file(const char *path) +{ + string8 result = {0}; + struct stat sbuf = {0}; + int err = 0; + i32 file = open(path, O_RDONLY); + + if(file) + { + + if(file != -1) + { + err = fstat(file, &sbuf); + check(err != -1); + + result.size = (u64)sbuf.st_size; + + if(result.size != 0) + { + result.data = (u8 *)mmap(0, + result.size, + PROT_READ, + MAP_PRIVATE, + file, + 0); + + check(result.data != MAP_FAILED); + } + + close(file); + } + else + { + // TODO(nasr): logging + } + + } + return result; +} #endif /* BASE_OS_H */ diff --git a/source/base/base_string.c b/source/base/base_string.c deleted file mode 100644 index 9d09914..0000000 --- a/source/base/base_string.c +++ /dev/null @@ -1,63 +0,0 @@ -internal b8 -compare_string(string c1, string c2) -{ - if (c1.size != c2.size) - { - return 0; - } - - for (u64 index = 0; index < c1.size; ++index) - { - if (c1.data[index] != c2.data[index]) - { - return 0; - } - } - - return 1; -} - -internal b8 -compare_u8(u8 *c1, u8 *c2, u64 len) -{ - if (sizeof(c1) != len || sizeof(c2) != len) - { - return 0; - } - - if (sizeof(*c1) != sizeof(*c2)) - { - return 0; - } - - for (u64 word_idx = 0; word_idx <= sizeof(*c1); ++word_idx) - { - if (*c1 != *c2) - { - return 0; - } - - ++c1; - ++c2; - } - - return 1; -} - -internal u64 -to_u64(u8 *buf, umm len) -{ - u64 value = 0; - - for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx) - { - u8 c = buf[buffer_idx]; - if (c < '0' || c > '9') - { - break; - } - value = value * 10 + (c - '0'); - } - - return value; -} diff --git a/source/base/base_string.h b/source/base/base_string.h index 5f875e0..941dc53 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h @@ -1,15 +1,46 @@ #ifndef BASE_STRING_H #define BASE_STRING_H -#define _StringCast (string) -#define StringCast { .data (u8 *)(string), .size = (sizeof((string)) - 1)} -#define PushString (Arena, Size) StringCast{.data = PushArray((arena), PushStruct(u8), (Size)), .size = (u64)(Size)} +#include -typedef struct string string; -struct string +#define StringLit(string) \ + (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } + + #define PushString(arena, size) \ + (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } + +typedef struct string8 string8; +struct string8 { - u8 *data; - u64 size; + u8 *data; + u64 size; }; +internal b8 +string8_cmp(string8 a, string8 b) +{ + if (a.size != b.size) return 0; + return (b8)(memcmp(a.data, b.data, a.size) == 0); +} + +internal u64 +string8_to_u64(u8 *buf, umm len) +{ + u64 value = 0; + for (umm i = 0; i < len; ++i) + { + u8 c = buf[i]; + if (c < '0' || c > '9') break; + value = value * 10 + (c - '0'); + } + return value; +} + +internal void +string8_append_char(string8 *buf, u8 c) +{ + buf->data[buf->size] = c; + buf->size += 1; +} + #endif /* BASE_STRING_H */ diff --git a/source/base/base_test.c b/source/base/base_test.c deleted file mode 100644 index e69de29..0000000 -- cgit v1.3