From 71ced998122c357bc62e54d9bb4e124c88acf94b Mon Sep 17 00:00:00 2001 From: nasr Date: Sun, 8 Mar 2026 21:01:43 +0000 Subject: refactor(main): worked on string handling in C and other stuff --- source/base/base.h | 16 ++++++++-------- source/base/base_include.h | 2 ++ source/base/base_io.h | 29 ++++++++++++++++++++++++++++- source/base/base_os.h | 10 +--------- source/base/base_string.h | 15 +++++++++------ source/base/base_test.h | 2 +- 6 files changed, 49 insertions(+), 25 deletions(-) (limited to 'source/base') diff --git a/source/base/base.h b/source/base/base.h index ef23391..cf2a15f 100755 --- a/source/base/base.h +++ b/source/base/base.h @@ -52,22 +52,22 @@ typedef uint32_t u32; typedef uint16_t u16; typedef uint8_t u8; -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; typedef float f32; typedef double f64; -typedef i32 b32; -typedef i16 b16; +typedef s32 b32; +typedef s16 b16; typedef u8 b8; typedef uintptr_t umm; typedef intptr_t smm; -#define TRUE (0 == 0) -#define FALSE (0 != 0) +#define TRUE (1 == 1) +#define FALSE (1 != 1) #endif diff --git a/source/base/base_include.h b/source/base/base_include.h index 40ae5ea..07856d0 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include "base_stack.h" #include "base_test.h" #include "base_string.h" +#include "base_io.h" #include "base_os.h" #ifdef BASE_UNITY diff --git a/source/base/base_io.h b/source/base/base_io.h index ece4d7c..85fedc7 100644 --- a/source/base/base_io.h +++ b/source/base/base_io.h @@ -1,11 +1,38 @@ #ifndef BASE_IO_H #define BASE_IO_H +#define STDIN_FD 0 +#define STDOUT_FD 1 +#define STDERR_FD 2 + +internal s64 +os_write(s32 fd, void const *buf, u64 count) +{ + return syscall(SYS_write, fd, buf, count); +} + +internal s64 +os_read(s32 fd, void *buf, u64 count) +{ + return syscall(SYS_read, fd, buf, count); +} + internal void -input_read() +print_s8(string8 s) { + os_write(STDOUT_FILENO, s.data, s.size); +} +internal void +print(const char *str) +{ + s32 len = 0; + while (str[len]) len++; + os_write(STDOUT_FILENO, str, len); } +#define Os_read(buffer, buffer_count) + + #endif /* BASE_IO_H */ diff --git a/source/base/base_os.h b/source/base/base_os.h index 23587c6..abe8628 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h @@ -1,21 +1,13 @@ #ifndef BASE_OS_H #define BASE_OS_H -internal void -print(const char *str) -{ - i32 len = 0; - while (str[len]) len++; - write(STDOUT_FILENO, str, len); -} - internal string8 load_file(const char *path) { string8 result = {0}; struct stat sbuf = {0}; - i32 file = open(path, O_RDONLY); + s32 file = open(path, O_RDONLY); if(file == -1) return result; if(fstat(file, &sbuf) == -1) diff --git a/source/base/base_string.h b/source/base/base_string.h index 189b38a..4e0d923 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h @@ -1,13 +1,18 @@ #ifndef BASE_STRING_H #define BASE_STRING_H -#include - #define StringLit(string) \ (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } - #define PushString(arena, size) \ - (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } +#define PushString(arena, size) \ + ({ \ + string8 *result = PushStruct((arena), string8); \ + result->data = PushArray((arena), u8, (size)); \ + result->size = (u64)(size); \ + result; \ + }) + +#define String8Cast(literal, literal_count) ( string8 ){( u8 * )( literal ),( u64 )( literal_count ) } #define StringFmt "%.*s" #define ULongFmt "%lu" @@ -50,10 +55,8 @@ string8_append_char(string8 *buf, u8 c) read_only global_variable string8 nil_string = { - .data = NULL, .size = 0, - }; #endif /* BASE_STRING_H */ diff --git a/source/base/base_test.h b/source/base/base_test.h index 412797b..7619cfb 100644 --- a/source/base/base_test.h +++ b/source/base/base_test.h @@ -9,7 +9,7 @@ #define LEN(s) (sizeof(s) - 1) internal void -write_int(i32 num) +write_int(s32 num) { if (num < 0) -- cgit v1.3