add some more file fns and a Ctrl-C handler

This commit is contained in:
Tenari
2026-04-09 18:55:52 -05:00
parent f67e4a3d9b
commit 7ec8dc5815
4 changed files with 78 additions and 0 deletions
+10
View File
@@ -288,6 +288,11 @@ typedef u32 b32;
#define false 0
// Structs
typedef struct Resulti64 {
bool success;
i64 value;
} Resulti64;
typedef struct Arena {
u8* memory;
u64 max;
@@ -462,6 +467,7 @@ union Range1f32
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
#else
# include <signal.h>
# include <poll.h>
# include <sys/socket.h>
# include <netinet/in.h>
@@ -598,6 +604,7 @@ fn bool isSimplePrintable(u8 c);
///// OS-wrapped apis
void osInit();
void osSetCtrlCCallback(void (*handler)());
void* osThreadContextGet();
void osThreadContextSet(void* ctx);
bool osThreadJoin(Thread handle, u64 endt_us);
@@ -620,6 +627,9 @@ fn String osFileRead(Arena* arena, ptr filepath);
fn bool osFileCreate(String filename);
fn bool osFileCreateWrite(String filename, String data);
fn bool osFileWrite(String filename, String data);
fn Resulti64 osFileOpenForWriting(String filename);
fn Resulti64 osFileClose(Resulti64 handle);
fn bool osFileWriteOpenFile(Resulti64 handle, String data);
fn void osDebugPrint(bool debug_mode, const char* format, ...);
+27
View File
@@ -124,6 +124,33 @@ fn bool osFileWrite(String filename, String data) {
return result;
}
fn Resulti64 osFileOpenForWriting(String filename) {
size_t handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH);
Resulti64 result = {
.success = handle != -1,
.value = (i64)handle,
};
return result;
}
fn Resulti64 osFileClose(Resulti64 handle) {
i32 close_result = close((size_t)handle.value);
Resulti64 result = {
.success = close_result != -1,
.value = (i64)close_result,
};
return result;
}
fn bool osFileWriteOpenFile(Resulti64 handle, String data) {
assert(handle.success == true);
i32 wrote_this_round = 0;
for (i32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) {
wrote_this_round = write((size_t)handle.value, data.bytes + bytes_written, data.length - bytes_written);
if (wrote_this_round == -1) return false;
}
return true;
}
// Misc
fn void osDebugPrint(bool debug_mode, const char * format, ... ) {
+12
View File
@@ -1,11 +1,23 @@
#include "all.h"
global pthread_key_t linux_thread_context_key;
global void (*_ctrl_c_handler_fn_ptr)() = NULL;
// ThreadContext
void osInit() {
pthread_key_create(&linux_thread_context_key, NULL);
}
void _osGenericSignalHandler(i32 signal) {
if (signal == SIGINT && _ctrl_c_handler_fn_ptr != NULL) {
_ctrl_c_handler_fn_ptr();
}
}
void osSetCtrlCCallback(void (*handler)()) {
_ctrl_c_handler_fn_ptr = handler;
signal(SIGINT, _osGenericSignalHandler);
}
void* osThreadContextGet() {
return pthread_getspecific(linux_thread_context_key);
}
+29
View File
@@ -5,6 +5,7 @@
static u64 w32_ticks_per_sec = 1;
static u32 w32_thread_context_index;
global void (*_ctrl_c_handler_fn_ptr)() = NULL;
void osInit() {
LARGE_INTEGER perf_freq = {0};
@@ -16,6 +17,19 @@ void osInit() {
w32_thread_context_index = TlsAlloc();
}
BOOL WINAPI _osGenericSignalHandler(DWORD event) {
if (event == CTRL_C_EVENT && _ctrl_c_handler_fn_ptr != NULL) {
_ctrl_c_handler_fn_ptr();
return TRUE;
}
return FALSE;
}
void osSetCtrlCCallback(void (*handler)()) {
_ctrl_c_handler_fn_ptr = handler;
SetConsoleCtrlHandler(_osGenericSignalHandler, TRUE);
}
void* osThreadContextGet() {
return TlsGetValue(w32_thread_context_index);
}
@@ -90,6 +104,21 @@ fn bool osFileWrite(String filename, String data) {
return result;
}
fn Resulti64 osFileOpenForWriting(String filename) {
assert(false && "Not Implemented");
return (Resulti64) {};
}
fn Resulti64 osFileClose(Resulti64 handle) {
assert(false && "Not Implemented");
return (Resulti64) {};
}
fn bool osFileWriteOpenFile(Resulti64 handle, String data) {
assert(false && "Not Implemented");
return false;
}
// Misc
fn void osDebugPrint(bool debug_mode, const char * format, ... ) {