twn_util: final cleaning up, introducton of powerful file_read()
This commit is contained in:
@ -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";
|
||||
|
Reference in New Issue
Block a user