refactor: add helpers for file I/O, main script detection, safe strncpy, & make hal generic

This commit is contained in:
asrael 2025-12-06 15:04:53 -06:00
parent a33d4c0068
commit db82efe269
No known key found for this signature in database
GPG key ID: 2786557804DFAE24
19 changed files with 327 additions and 457 deletions

View file

@ -98,31 +98,14 @@ pxl8_result pxl8_bsp_load(const char* path, pxl8_bsp* bsp) {
memset(bsp, 0, sizeof(*bsp));
FILE* f = fopen(path, "rb");
if (!f) {
u8* file_data = NULL;
size_t file_size = 0;
pxl8_result result = pxl8_io_read_binary_file(path, &file_data, &file_size);
if (result != PXL8_OK) {
pxl8_error("Failed to load BSP file: %s", path);
return PXL8_ERROR_FILE_NOT_FOUND;
return result;
}
fseek(f, 0, SEEK_END);
size_t file_size = ftell(f);
fseek(f, 0, SEEK_SET);
u8* file_data = (u8*)malloc(file_size);
if (!file_data) {
fclose(f);
pxl8_error("Failed to allocate memory for BSP file: %s", path);
return PXL8_ERROR_OUT_OF_MEMORY;
}
if (fread(file_data, 1, file_size, f) != file_size) {
free(file_data);
fclose(f);
pxl8_error("Failed to read BSP file: %s", path);
return PXL8_ERROR_INVALID_FORMAT;
}
fclose(f);
if (file_size < sizeof(pxl8_bsp_header)) {
pxl8_error("BSP file too small: %s", path);
free(file_data);