twn_util: file_read() :images

This commit is contained in:
veclavtalica
2025-03-10 06:54:10 +03:00
parent 8ed8158ae6
commit 7b8b9416ba
3 changed files with 50 additions and 2 deletions

View File

@ -151,6 +151,39 @@ char *file_to_str(const char *path, size_t *out_len) {
return str_out;
}
struct ImageCollectionData {
char *list;
size_t length;
};
static PHYSFS_EnumerateCallbackResult image_collection_callback(void *data,
const char *origdir,
const char *fname)
{
struct ImageCollectionData *result = data;
(void)origdir;
/* TODO: settle down on supported images */
size_t file_len = SDL_strlen(fname);
size_t dir_len = SDL_strlen(origdir);
if (CHECK_ENDING(fname, file_len, ".png")) {
result->list = SDL_realloc(result->list, result->length + dir_len + file_len + 1);
if (result->length != 0)
result->list[result->length-1] = '\n';
SDL_strlcpy(&result->list[result->length], origdir, dir_len);
result->length += dir_len;
result->list[result->length-1] = '/';
SDL_strlcpy(&result->list[result->length], fname, file_len + 1);
result->length += file_len + 1;
result->list[result->length-1] = '\0';
}
return PHYSFS_ENUM_OK;
}
/* TODO: caching on file and operation inputs */
static String *read_files;
String file_read(char const *file, char const *operation) {
@ -175,9 +208,8 @@ String file_read(char const *file, char const *operation) {
arrpush(read_files, s);
return s;
}
else if (SDL_strncmp(operation, ":binary", sizeof (":binary") - 1) == 0) {
} else if (SDL_strncmp(operation, ":binary", sizeof (":binary") - 1) == 0) {
uint8_t *data;
int64_t length = file_to_bytes(file, &data);
@ -193,6 +225,19 @@ String file_read(char const *file, char const *operation) {
bool exists = file_exists(file);
return exists ? (String){"yes", 3} : (String){"no", 2};
} else if (SDL_strncmp(operation, ":images", sizeof (":images") - 1) == 0) {
struct ImageCollectionData list = {0};
if (PHYSFS_enumerate(file, image_collection_callback, &list) == 0) {
CRY_PHYSFS("Error enumerating images");
if (list.list) SDL_free(list.list);
return (String){NULL, 0};
}
String s = {(char *)list.list, (float)list.length - 1};
arrpush(read_files, s);
return s;
} else
log_warn("No valid operation specified by %s", operation);