twn_util: final cleaning up, introducton of powerful file_read()

This commit is contained in:
veclavtalica
2025-03-10 05:19:58 +03:00
parent f86f3dd41a
commit 56530f9864
11 changed files with 145 additions and 82 deletions

View File

@ -265,7 +265,7 @@ static void resolve_pack_dependencies(const char *pack_name) {
/* no package manifest provided, abort */
goto OK_NO_MANIFEST;
char *manifest_file = file_to_str(path);
char *manifest_file = file_to_str(path, NULL);
if (!manifest_file) {
CRY_PHYSFS("Pack manifest file loading failed");
goto ERR_PACK_MANIFEST_FILE_LOADING;
@ -375,7 +375,7 @@ static bool initialize(void) {
/* load the config file into an opaque table */
{
char *config_file = file_to_str("/twn.toml");
char *config_file = file_to_str("/twn.toml", NULL);
if (config_file == NULL) {
CRY("Configuration file loading failed", "Cannot access /twn.toml");
goto fail;

View File

@ -109,12 +109,6 @@ void *ccalloc(size_t num, size_t size) {
}
float clampf(float f, float min, float max) {
const float t = f < min ? min : f;
return t > max ? max : t;
}
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
@ -133,7 +127,7 @@ int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
}
char *file_to_str(const char *path) {
char *file_to_str(const char *path, size_t *out_len) {
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
if (handle == NULL) {
@ -151,23 +145,62 @@ char *file_to_str(const char *path) {
str_out[len] = '\0';
if (out_len)
*out_len = len;
return str_out;
}
static char **read_files;
static String *read_files;
char const *file_read(char const *file) {
char *s = file_to_str(file);
String file_read(char const *file, char const *operation) {
if (!file) {
log_warn("No file specified");
return (String){NULL, 0};
}
if (s) arrpush(read_files, s);
if (!operation) {
log_warn("No operation specified");
return (String){NULL, 0};
}
return s;
if (SDL_strncmp(operation, ":string", sizeof (":string") - 1) == 0) {
size_t length;
char *data = file_to_str(file, &length);
if (!data)
return (String){NULL, 0};
String s = {data, (float)length};
arrpush(read_files, s);
return s;
}
if (SDL_strncmp(operation, ":binary", sizeof (":binary") - 1) == 0) {
uint8_t *data;
int64_t length = file_to_bytes(file, &data);
if (length == -1)
return (String){NULL, 0};
String s = {(char *)data, (float)length};
arrpush(read_files, s);
return s;
} else
log_warn("No valid operation specified by %s", operation);
return (String){NULL, 0};
}
void file_read_garbage_collect(void) {
for (size_t i = 0; i < arrlenu(read_files); ++i)
SDL_free(read_files[i]);
for (size_t i = 0; i < arrlenu(read_files); ++i) {
SDL_assert_always(read_files[i].data && read_files[i].length >= 0);
SDL_free(read_files[i].data);
}
arrfree(read_files);
read_files = NULL;
}
@ -342,6 +375,18 @@ void profile_list_stats(void) {
}
}
void log_string(char const *value, char const *message) {
if (!value) return;
if (!message)
log_info("%s", value);
else
log_info("%s: %s", message, value);
}
void log_float(float value, char const *message) {
if (!message) message = "float";
log_info("%s = %f", message, (double)value);
}
void log_vec2(Vec2 vector, char const *message) {
if (!message) message = "Vec2";

View File

@ -21,6 +21,10 @@ void cry_impl(const char *file, const int line, const char *title, const char *t
#define CRY_PHYSFS(title) \
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
void log_info(const char *restrict format, ...);
void log_critical(const char *restrict format, ...);
void log_warn(const char *restrict format, ...);
/* for when there's absolutely no way to continue */
_Noreturn void die_abruptly(void);
@ -31,6 +35,23 @@ void profile_list_stats(void);
void file_read_garbage_collect(void);
/* sets buf_out to a pointer to a byte buffer which must be freed. */
/* returns the size of this buffer. */
int64_t file_to_bytes(const char *path, unsigned char **buf_out);
/* returns a pointer to a string which must be freed */
char *file_to_str(const char *path, size_t *out_len);
/* saves all texture atlases as BMP files in the write directory */
void textures_dump_atlases(void);
bool file_exists(const char *path);
static inline float clampf(float f, float min, float max) {
const float t = f < min ? min : f;
return t > max ? max : t;
}
/* http://www.azillionmonkeys.com/qed/sqroot.html */
static inline float fast_sqrt(float x)
{