From 8a316a12481282cb2ab966c4cbc770656c258383 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 30 Oct 2021 04:22:07 +0200 Subject: Add option to make mgl_file_load result null terminated --- include/mgl/graphics/texture.h | 2 +- include/mgl/system/fileutils.h | 8 +++++++- src/graphics/font.c | 2 +- src/graphics/shader.c | 2 +- src/system/fileutils.c | 9 +++++++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/mgl/graphics/texture.h b/include/mgl/graphics/texture.h index 7891dab..9e7a423 100644 --- a/include/mgl/graphics/texture.h +++ b/include/mgl/graphics/texture.h @@ -22,7 +22,7 @@ struct mgl_texture { }; typedef struct { - bool compressed; + bool compressed; /* false by default */ } mgl_texture_load_options; /* |load_options| can be null, in which case the default options are used */ diff --git a/include/mgl/system/fileutils.h b/include/mgl/system/fileutils.h index 71af147..7696679 100644 --- a/include/mgl/system/fileutils.h +++ b/include/mgl/system/fileutils.h @@ -2,13 +2,19 @@ #define MGL_FILEUTILS_H #include +#include typedef struct { unsigned char *data; size_t size; } mgl_filedata; -int mgl_load_file(const char *filepath, mgl_filedata *filedata); +typedef struct { + bool null_terminated; /* false by default */ +} mgl_file_load_options; + +/* |load_options| can be null, in which case the default options are used */ +int mgl_load_file(const char *filepath, mgl_filedata *filedata, mgl_file_load_options *load_options); void mgl_filedata_free(mgl_filedata *self); #endif /* MGL_FILEUTILS_H */ diff --git a/src/graphics/font.c b/src/graphics/font.c index 669c2ac..8344a04 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -22,7 +22,7 @@ int mgl_font_load_from_file(mgl_font *self, const char *filepath, unsigned int c self->num_packed_chars = 0; mgl_filedata filedata; - if(mgl_load_file(filepath, &filedata) != 0) { + if(mgl_load_file(filepath, &filedata, NULL) != 0) { fprintf(stderr, "Error: failed to load font %s, error: mgl_load_file failed\n", filepath); return -1; } diff --git a/src/graphics/shader.c b/src/graphics/shader.c index 4aedb87..9afde0e 100644 --- a/src/graphics/shader.c +++ b/src/graphics/shader.c @@ -71,7 +71,7 @@ static int mgl_shader_load_from_file(mgl_shader *self, const char *filepath, mgl self->shader_type = shader_type; mgl_filedata filedata; - if(mgl_load_file(filepath, &filedata) != 0) { + if(mgl_load_file(filepath, &filedata, NULL) != 0) { fprintf(stderr, "Error: failed to load shader %s, error: mgl_load_file failed\n", filepath); return -1; } diff --git a/src/system/fileutils.c b/src/system/fileutils.c index 7bd6859..dc8eaca 100644 --- a/src/system/fileutils.c +++ b/src/system/fileutils.c @@ -4,7 +4,9 @@ #include #include -int mgl_load_file(const char *filepath, mgl_filedata *filedata) { +int mgl_load_file(const char *filepath, mgl_filedata *filedata, mgl_file_load_options *load_options) { + const bool null_terminated = load_options ? load_options->null_terminated : false; + int fd = open(filepath, O_RDONLY); if(fd == -1) return -1; @@ -20,7 +22,7 @@ int mgl_load_file(const char *filepath, mgl_filedata *filedata) { return -1; } - unsigned char *data = malloc(st.st_size); + unsigned char *data = malloc(st.st_size + (null_terminated ? 1 : 0)); if(!data) { close(fd); return -1; @@ -32,6 +34,9 @@ int mgl_load_file(const char *filepath, mgl_filedata *filedata) { return -1; } + if(null_terminated) + data[st.st_size] = '\0'; + filedata->data = data; filedata->size = st.st_size; -- cgit v1.2.3