filesystem: move to libtrx

This commit is contained in:
Marcin Kurczewski 2024-04-28 18:21:44 +02:00
parent f2781eb823
commit 9d0809cb58
25 changed files with 18 additions and 452 deletions

View file

@ -88,7 +88,6 @@ sources = [
init,
'src/config.c',
'src/config_map.c',
'src/filesystem.c',
'src/game/anim.c',
'src/game/box.c',
'src/game/camera.c',
@ -275,7 +274,6 @@ sources = [
'src/specific/s_audio_sample.c',
'src/specific/s_audio_stream.c',
'src/specific/s_clock.c',
'src/specific/s_filesystem.c',
'src/specific/s_fmv.c',
'src/specific/s_input.c',
'src/specific/s_output.c',

View file

@ -1,7 +1,6 @@
#include "config.h"
#include "config_map.h"
#include "filesystem.h"
#include "game/input.h"
#include "game/music.h"
#include "game/sound.h"
@ -11,6 +10,7 @@
#include "json/json_base.h"
#include "json/json_parse.h"
#include "json/json_write.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "util.h"

View file

@ -1,207 +0,0 @@
#include "filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "specific/s_filesystem.h"
#include <stdio.h>
#include <string.h>
struct MYFILE {
FILE *fp;
const char *path;
};
static bool File_ExistsRaw(const char *path);
static bool File_ExistsRaw(const char *path)
{
FILE *fp = fopen(path, "rb");
if (fp) {
fclose(fp);
return true;
}
return false;
}
bool File_IsAbsolute(const char *path)
{
return path && (path[0] == '/' || strstr(path, ":\\"));
}
bool File_IsRelative(const char *path)
{
return path && !File_IsAbsolute(path);
}
const char *File_GetGameDirectory(void)
{
return S_File_GetGameDirectory();
}
bool File_Exists(const char *path)
{
char *full_path = File_GetFullPath(path);
bool ret = File_ExistsRaw(full_path);
Memory_FreePointer(&full_path);
return ret;
}
char *File_GetFullPath(const char *path)
{
char *full_path = NULL;
if (File_IsRelative(path)) {
const char *game_dir = File_GetGameDirectory();
if (game_dir) {
full_path = Memory_Alloc(strlen(game_dir) + strlen(path) + 1);
sprintf(full_path, "%s%s", game_dir, path);
}
}
if (!full_path) {
full_path = Memory_DupStr(path);
}
char *case_path = S_File_CasePath(full_path);
if (case_path) {
Memory_FreePointer(&full_path);
return case_path;
}
return full_path;
}
char *File_GuessExtension(const char *path, const char **extensions)
{
if (!File_Exists(path)) {
const char *dot = strrchr(path, '.');
if (dot) {
for (const char **ext = &extensions[0]; *ext; ext++) {
size_t out_size = dot - path + strlen(*ext) + 1;
char *out = Memory_Alloc(out_size);
strncpy(out, path, dot - path);
out[dot - path] = '\0';
strcat(out, *ext);
if (File_Exists(out)) {
return out;
}
Memory_FreePointer(&out);
}
}
}
return Memory_DupStr(path);
}
MYFILE *File_Open(const char *path, FILE_OPEN_MODE mode)
{
char *full_path = File_GetFullPath(path);
MYFILE *file = Memory_Alloc(sizeof(MYFILE));
file->path = Memory_DupStr(path);
switch (mode) {
case FILE_OPEN_WRITE:
file->fp = fopen(full_path, "wb");
break;
case FILE_OPEN_READ:
file->fp = fopen(full_path, "rb");
break;
case FILE_OPEN_READ_WRITE:
file->fp = fopen(full_path, "r+b");
break;
default:
file->fp = NULL;
break;
}
Memory_FreePointer(&full_path);
if (!file->fp) {
Memory_FreePointer(&file);
}
return file;
}
size_t File_Read(void *data, size_t item_size, size_t count, MYFILE *file)
{
return fread(data, item_size, count, file->fp);
}
size_t File_Write(
const void *data, size_t item_size, size_t count, MYFILE *file)
{
return fwrite(data, item_size, count, file->fp);
}
void File_CreateDirectory(const char *path)
{
char *full_path = File_GetFullPath(path);
S_File_CreateDirectory(full_path);
Memory_FreePointer(&full_path);
}
void File_Skip(MYFILE *file, size_t bytes)
{
File_Seek(file, bytes, FILE_SEEK_CUR);
}
void File_Seek(MYFILE *file, size_t pos, FILE_SEEK_MODE mode)
{
switch (mode) {
case FILE_SEEK_SET:
fseek(file->fp, pos, SEEK_SET);
break;
case FILE_SEEK_CUR:
fseek(file->fp, pos, SEEK_CUR);
break;
case FILE_SEEK_END:
fseek(file->fp, pos, SEEK_END);
break;
}
}
size_t File_Pos(MYFILE *file)
{
return ftell(file->fp);
}
size_t File_Size(MYFILE *file)
{
size_t old = ftell(file->fp);
fseek(file->fp, 0, SEEK_END);
size_t size = ftell(file->fp);
fseek(file->fp, old, SEEK_SET);
return size;
}
const char *File_GetPath(MYFILE *file)
{
return file->path;
}
void File_Close(MYFILE *file)
{
fclose(file->fp);
Memory_FreePointer(&file->path);
Memory_FreePointer(&file);
}
bool File_Load(const char *path, char **output_data, size_t *output_size)
{
MYFILE *fp = File_Open(path, FILE_OPEN_READ);
if (!fp) {
LOG_ERROR("Can't open file %s", path);
return false;
}
size_t data_size = File_Size(fp);
char *data = Memory_Alloc(data_size + 1);
if (File_Read(data, sizeof(char), data_size, fp) != data_size) {
LOG_ERROR("Can't read file %s", path);
Memory_FreePointer(&data);
return false;
}
File_Close(fp);
data[data_size] = '\0';
*output_data = data;
if (output_size) {
*output_size = data_size;
}
return true;
}

View file

@ -1,57 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
typedef enum {
FILE_SEEK_SET,
FILE_SEEK_CUR,
FILE_SEEK_END,
} FILE_SEEK_MODE;
typedef enum {
FILE_OPEN_READ,
FILE_OPEN_READ_WRITE,
FILE_OPEN_WRITE,
} FILE_OPEN_MODE;
typedef struct MYFILE MYFILE;
bool File_IsAbsolute(const char *path);
bool File_IsRelative(const char *path);
bool File_Exists(const char *path);
const char *File_GetGameDirectory(void);
// Get the absolute path to the given file, if possible.
// Internaly all operations on files within filesystem.c
// perform this normalization, so calling this function should
// only be necessary when interacting with external libraries.
char *File_GetFullPath(const char *path);
char *File_GuessExtension(const char *path, const char **extensions);
MYFILE *File_Open(const char *path, FILE_OPEN_MODE mode);
size_t File_Read(void *data, size_t item_size, size_t count, MYFILE *file);
size_t File_Write(
const void *data, size_t item_size, size_t count, MYFILE *file);
void File_CreateDirectory(const char *path);
size_t File_Pos(MYFILE *file);
size_t File_Size(MYFILE *file);
const char *File_GetPath(MYFILE *file);
void File_Skip(MYFILE *file, size_t bytes);
void File_Seek(MYFILE *file, size_t pos, FILE_SEEK_MODE mode);
void File_Close(MYFILE *file);
bool File_Load(const char *path, char **output_data, size_t *output_size);

View file

@ -23,7 +23,7 @@
#include "global/vars.h"
#include "math/math.h"
#include "shared/memory.h"
#include "strings.h"
#include "shared/strings.h"
#include "util.h"
#include <assert.h>

View file

@ -1,8 +1,8 @@
#include "game/fmv.h"
#include "filesystem.h"
#include "game/music.h"
#include "game/sound.h"
#include "shared/filesystem.h"
#include "shared/memory.h"
#include "specific/s_fmv.h"

View file

@ -1,7 +1,6 @@
#include "game/gameflow.h"
#include "config.h"
#include "filesystem.h"
#include "game/fmv.h"
#include "game/game.h"
#include "game/game_string.h"
@ -18,6 +17,7 @@
#include "global/vars.h"
#include "json/json_base.h"
#include "json/json_parse.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -1,13 +1,13 @@
#include "game/inject.h"
#include "config.h"
#include "filesystem.h"
#include "game/gamebuf.h"
#include "game/output.h"
#include "game/packer.h"
#include "global/const.h"
#include "global/vars.h"
#include "items.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "util.h"

View file

@ -1,6 +1,5 @@
#include "game/level.h"
#include "filesystem.h"
#include "game/camera.h"
#include "game/carrier.h"
#include "game/effects.h"
@ -24,6 +23,7 @@
#include "global/const.h"
#include "global/types.h"
#include "global/vars.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -1,10 +1,10 @@
#include "game/music.h"
#include "config.h"
#include "filesystem.h"
#include "game/gameflow.h"
#include "game/sound.h"
#include "global/vars.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "specific/s_audio.h"

View file

@ -1,6 +1,6 @@
#include "game/picture.h"
#include "filesystem.h"
#include "shared/filesystem.h"
#include "shared/memory.h"
#include "specific/s_picture.h"

View file

@ -1,7 +1,6 @@
#include "game/savegame.h"
#include "config.h"
#include "filesystem.h"
#include "game/game_string.h"
#include "game/gameflow.h"
#include "game/inventory.h"
@ -20,6 +19,7 @@
#include "global/const.h"
#include "global/types.h"
#include "global/vars.h"
#include "shared/filesystem.h"
#include "shared/memory.h"
#include <assert.h>

View file

@ -1,8 +1,8 @@
#pragma once
#include "filesystem.h"
#include "game/savegame.h"
#include "global/types.h"
#include "shared/filesystem.h"
#include <stdbool.h>
#include <stdint.h>

View file

@ -1,8 +1,8 @@
#pragma once
#include "filesystem.h"
#include "game/savegame.h"
#include "global/types.h"
#include "shared/filesystem.h"
#include <stdbool.h>
#include <stdint.h>

View file

@ -1,7 +1,6 @@
#include "game/shell.h"
#include "config.h"
#include "filesystem.h"
#include "game/clock.h"
#include "game/console.h"
#include "game/fmv.h"
@ -22,6 +21,7 @@
#include "gfx/common.h"
#include "global/types.h"
#include "global/vars.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "specific/s_shell.h"

View file

@ -1,8 +1,8 @@
#include "gfx/gl/program.h"
#include "filesystem.h"
#include "game/shell.h"
#include "gfx/gl/utils.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -1,7 +1,7 @@
#define S_AUDIO_IMPL
#include "specific/s_audio.h"
#include "filesystem.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -1,142 +0,0 @@
#include "specific/s_filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "strings.h"
#include <SDL2/SDL_filesystem.h>
#include <assert.h>
#include <dirent.h>
#include <stdbool.h>
#include <string.h>
#if defined(_WIN32)
#include <direct.h>
#define PATH_SEPARATOR "\\"
#else
#include <sys/stat.h>
#define PATH_SEPARATOR "/"
#endif
const char *m_GameDir = NULL;
static bool S_File_StringEndsWith(const char *str, const char *suffix);
static void S_File_PathAppendSeparator(char *path);
static void S_File_PathAppendPart(char *path, const char *part);
static bool S_File_StringEndsWith(const char *str, const char *suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
if (suffix_len > str_len) {
return 0;
}
return strcmp(str + str_len - suffix_len, suffix) == 0;
}
static void S_File_PathAppendSeparator(char *path)
{
if (!S_File_StringEndsWith(path, PATH_SEPARATOR)) {
strcat(path, PATH_SEPARATOR);
}
}
static void S_File_PathAppendPart(char *path, const char *part)
{
S_File_PathAppendSeparator(path);
strcat(path, part);
}
const char *S_File_GetGameDirectory(void)
{
if (!m_GameDir) {
m_GameDir = SDL_GetBasePath();
if (!m_GameDir) {
LOG_ERROR("Can't get module handle");
return NULL;
}
}
return m_GameDir;
}
void S_File_CreateDirectory(const char *path)
{
assert(path);
#if defined(_WIN32)
_mkdir(path);
#else
mkdir(path, 0775);
#endif
}
char *S_File_CasePath(char const *path)
{
assert(path);
char *current_path = Memory_Alloc(strlen(path) + 2);
char *path_copy = Memory_DupStr(path);
char *path_piece = path_copy;
if (path_copy[0] == '/') {
strcpy(current_path, "/");
path_piece++;
} else if (strstr(path_copy, ":\\")) {
strcpy(current_path, path_copy);
strstr(current_path, ":\\")[1] = '\0';
path_piece += 3;
} else {
strcpy(current_path, ".");
}
while (path_piece) {
char *delim = strpbrk(path_piece, "/\\");
char old_delim = delim ? *delim : '\0';
if (delim) {
*delim = '\0';
}
DIR *path_dir = opendir(current_path);
if (!path_dir) {
Memory_FreePointer(&path_copy);
Memory_FreePointer(&current_path);
return NULL;
}
struct dirent *cur_file = readdir(path_dir);
while (cur_file) {
if (String_Equivalent(path_piece, cur_file->d_name)) {
S_File_PathAppendPart(current_path, cur_file->d_name);
break;
}
cur_file = readdir(path_dir);
}
closedir(path_dir);
if (!cur_file) {
S_File_PathAppendPart(current_path, path_piece);
}
if (delim) {
*delim = old_delim;
path_piece = delim + 1;
} else {
break;
}
}
Memory_FreePointer(&path_copy);
char *result;
if (current_path[0] == '.'
&& strcmp(current_path + 1, PATH_SEPARATOR)
== 0) { /* strip leading ./ */
result = Memory_DupStr(current_path + 1 + strlen(PATH_SEPARATOR));
} else {
result = Memory_DupStr(current_path);
}
Memory_FreePointer(&current_path);
return result;
}

View file

@ -1,5 +0,0 @@
#pragma once
const char *S_File_GetGameDirectory(void);
void S_File_CreateDirectory(const char *path);
char *S_File_CasePath(char const *path);

View file

@ -23,7 +23,6 @@
#include "specific/s_fmv.h"
#include "config.h"
#include "filesystem.h"
#include "game/input.h"
#include "game/screen.h"
#include "game/shell.h"
@ -32,6 +31,7 @@
#include "gfx/2d/2d_surface.h"
#include "gfx/context.h"
#include "global/types.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"
#include "specific/s_audio.h"

View file

@ -1,7 +1,7 @@
#include "specific/s_picture.h"
#include "filesystem.h"
#include "game/picture.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -1,7 +1,6 @@
#include "specific/s_shell.h"
#include "config.h"
#include "filesystem.h"
#include "game/console.h"
#include "game/input.h"
#include "game/music.h"
@ -9,6 +8,7 @@
#include "game/random.h"
#include "game/shell.h"
#include "game/sound.h"
#include "shared/filesystem.h"
#include "shared/log.h"
#include "shared/memory.h"

View file

@ -6,26 +6,6 @@
#include <pcre2.h>
#include <string.h>
bool String_Equivalent(const char *a, const char *b)
{
if (a == NULL || b == NULL) {
return false;
}
size_t a_size = strlen(a);
size_t b_size = strlen(b);
if (a_size != b_size) {
return false;
}
for (size_t i = 0; i < a_size; i++) {
if (tolower(a[i]) != tolower(b[i])) {
return false;
}
}
return true;
}
const char *String_CaseSubstring(const char *subject, const char *pattern)
{
if (subject == NULL || pattern == NULL) {

View file

@ -3,6 +3,5 @@
#include <stdbool.h>
#include <stdint.h>
bool String_Equivalent(const char *a, const char *b);
const char *String_CaseSubstring(const char *subject, const char *pattern);
int32_t String_Match(const char *subject, const char *pattern);

@ -1 +1 @@
Subproject commit 1be41c3684b410545dd443f0def6f4fb32e9ac38
Subproject commit f0b32cbe912627159de7af4ba6db441b3221e9bd