126 lines
3.1 KiB
C
126 lines
3.1 KiB
C
|
|
#include "pxl8_io.h"
|
||
|
|
#include "pxl8_mem.h"
|
||
|
|
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#ifndef _WIN32
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
#else
|
||
|
|
#include <direct.h>
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <SDL3/SDL.h>
|
||
|
|
|
||
|
|
pxl8_result pxl8_io_create_directory(const char* path) {
|
||
|
|
if (!path) return PXL8_ERROR_NULL_POINTER;
|
||
|
|
return SDL_CreateDirectory(path) ? PXL8_OK : PXL8_ERROR_SYSTEM_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
pxl8_io_dir_callback callback;
|
||
|
|
void* userdata;
|
||
|
|
} pxl8_io_enum_ctx;
|
||
|
|
|
||
|
|
static SDL_EnumerationResult pxl8_io_enum_adapter(void* userdata, const char* dirname, const char* fname) {
|
||
|
|
pxl8_io_enum_ctx* ctx = userdata;
|
||
|
|
if (ctx->callback(ctx->userdata, dirname, fname)) {
|
||
|
|
return SDL_ENUM_CONTINUE;
|
||
|
|
}
|
||
|
|
return SDL_ENUM_SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool pxl8_io_enumerate_directory(const char* path, pxl8_io_dir_callback callback, void* userdata) {
|
||
|
|
if (!path || !callback) return false;
|
||
|
|
pxl8_io_enum_ctx ctx = { .callback = callback, .userdata = userdata };
|
||
|
|
return SDL_EnumerateDirectory(path, pxl8_io_enum_adapter, &ctx);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool pxl8_io_file_exists(const char* path) {
|
||
|
|
if (!path) return false;
|
||
|
|
return SDL_GetPathInfo(path, NULL);
|
||
|
|
}
|
||
|
|
|
||
|
|
char* pxl8_io_get_cwd(void) {
|
||
|
|
char* sdl_cwd = SDL_GetCurrentDirectory();
|
||
|
|
if (!sdl_cwd) return NULL;
|
||
|
|
char* cwd = pxl8_malloc(strlen(sdl_cwd) + 1);
|
||
|
|
if (cwd) strcpy(cwd, sdl_cwd);
|
||
|
|
SDL_free(sdl_cwd);
|
||
|
|
return cwd;
|
||
|
|
}
|
||
|
|
|
||
|
|
f64 pxl8_io_get_file_modified_time(const char* path) {
|
||
|
|
if (!path) return 0.0;
|
||
|
|
SDL_PathInfo info;
|
||
|
|
if (SDL_GetPathInfo(path, &info)) {
|
||
|
|
return (f64)info.modify_time / 1000000000.0;
|
||
|
|
}
|
||
|
|
return 0.0;
|
||
|
|
}
|
||
|
|
|
||
|
|
usize pxl8_io_get_file_size(const char* path) {
|
||
|
|
if (!path) return 0;
|
||
|
|
SDL_PathInfo info;
|
||
|
|
if (SDL_GetPathInfo(path, &info)) return (usize)info.size;
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
char* pxl8_io_get_pref_path(const char* org, const char* app) {
|
||
|
|
char* sdl_path = SDL_GetPrefPath(org, app);
|
||
|
|
if (!sdl_path) return NULL;
|
||
|
|
char* path = pxl8_malloc(strlen(sdl_path) + 1);
|
||
|
|
if (path) strcpy(path, sdl_path);
|
||
|
|
SDL_free(sdl_path);
|
||
|
|
return path;
|
||
|
|
}
|
||
|
|
|
||
|
|
char* pxl8_io_get_real_path(const char* path) {
|
||
|
|
if (!path) return NULL;
|
||
|
|
|
||
|
|
if (path[0] == '/') {
|
||
|
|
char* result = pxl8_malloc(strlen(path) + 1);
|
||
|
|
if (result) strcpy(result, path);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
char* cwd = SDL_GetCurrentDirectory();
|
||
|
|
if (!cwd) return NULL;
|
||
|
|
|
||
|
|
usize cwd_len = strlen(cwd);
|
||
|
|
usize path_len = strlen(path);
|
||
|
|
char* result = pxl8_malloc(cwd_len + path_len + 2);
|
||
|
|
if (result) {
|
||
|
|
strcpy(result, cwd);
|
||
|
|
if (cwd_len > 0 && cwd[cwd_len - 1] != '/') {
|
||
|
|
strcat(result, "/");
|
||
|
|
}
|
||
|
|
strcat(result, path);
|
||
|
|
}
|
||
|
|
|
||
|
|
SDL_free(cwd);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool pxl8_io_set_cwd(const char* path) {
|
||
|
|
if (!path) return false;
|
||
|
|
#ifdef _WIN32
|
||
|
|
return _chdir(path) == 0;
|
||
|
|
#else
|
||
|
|
return chdir(path) == 0;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
void pxl8_io_set_executable(const char* path) {
|
||
|
|
if (!path) return;
|
||
|
|
#ifndef _WIN32
|
||
|
|
chmod(path, 0755);
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
bool pxl8_io_is_directory(const char* path) {
|
||
|
|
if (!path) return false;
|
||
|
|
SDL_PathInfo info;
|
||
|
|
return SDL_GetPathInfo(path, &info) && info.type == SDL_PATHTYPE_DIRECTORY;
|
||
|
|
}
|