From fda0de78c7069d5c86937b77172d323e762690a5 Mon Sep 17 00:00:00 2001 From: klemek Date: Fri, 12 Sep 2025 18:41:18 +0200 Subject: [PATCH] file utility --- Makefile.am | 4 ++-- README.md | 5 +++-- configure.ac | 2 ++ src/file.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/file.h | 12 ++++++++++ src/types.h | 9 ++++++++ 6 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/file.c create mode 100644 src/file.h diff --git a/Makefile.am b/Makefile.am index a995a78..452b943 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ AUTOMAKE_OPTIONS = foreign subdir-objects -Wall bin_PROGRAMS = forge -forge_SOURCES = src/main.c src/args.c src/forge.c +forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c forge_CFLAGS = -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h include/linmath.h include/glad/gl.h \ No newline at end of file +include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h include/linmath.h include/glad/gl.h \ No newline at end of file diff --git a/README.md b/README.md index 79903a4..93779c7 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ make -f Makefile.dev release-arch - [ ] Minimal working fragment sample - [ ] Hot-reload fragment shader - [x] Force fullscreen - - [ ] Select screen as argument / config + - [x] Select screen as argument / config - [ ] Midi - [ ] Read Midi events - [ ] Read midi mapping config file @@ -104,4 +104,5 @@ make -f Makefile.dev release-arch - [ ] Advanced - [ ] Video input - [ ] Feedback input - - [ ] Tap-tempo feature \ No newline at end of file + - [ ] Tap-tempo feature + - [ ] Monitor screen feature (other window) \ No newline at end of file diff --git a/configure.ac b/configure.ac index 1f75c14..8f9f133 100644 --- a/configure.ac +++ b/configure.ac @@ -6,6 +6,8 @@ AC_CHECK_HEADERS([stdlib.h]) AC_CHECK_HEADERS([stdbool.h]) AC_CHECK_HEADERS([string.h]) AC_CHECK_HEADERS([time.h]) +AC_CHECK_HEADERS([sys/stat.h]) +AC_CHECK_HEADERS([sys/types.h]) AC_CHECK_HEADERS([GLFW/glfw3.h]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT \ No newline at end of file diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..8e9ab26 --- /dev/null +++ b/src/file.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +#include "types.h" + +void update_file_time(File *file) { + struct stat attr; + if (stat(file->path, &attr) == 0) { + file->last_write = attr.st_mtime; + } +} + +bool should_update_file(File *file) { + struct stat attr; + if (stat(file->path, &attr) == 0) { + return file->last_write != attr.st_mtime; + } + return false; +} + +void update_file(File *file) { + // free remaining data + free(file->content); + // init empty file + file->content = 0; + file->error = false; + long length; + // open file + FILE *file_pointer = fopen(file->path, "rb"); + if (!file_pointer) { + file->error = true; + return; + } + // read file length + fseek(file_pointer, 0, SEEK_END); + length = ftell(file_pointer); + // init buffer + fseek(file_pointer, 0, SEEK_SET); + file->content = (char *)malloc((length + 1) * sizeof(char)); + if (!file->content) { + file->error = true; + fclose(file_pointer); + return; + } + // read file + fread(file->content, sizeof(char), length, file_pointer); + // close file + fclose(file_pointer); + // append null byte + file->content[length] = '\0'; + // read last update time + update_file_time(file); +} + +File read_file(char *path) { + File file = {path, 0, 0, 0}; + update_file(&file); + return file; +} \ No newline at end of file diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..e9f9239 --- /dev/null +++ b/src/file.h @@ -0,0 +1,12 @@ +#include "types.h" + +#ifndef FILE_H +#define FILE_H + +File read_file(char *path); + +bool should_update_file(File *file); + +void update_file(File *file); + +#endif \ No newline at end of file diff --git a/src/types.h b/src/types.h index 1605973..90e4cbc 100644 --- a/src/types.h +++ b/src/types.h @@ -1,5 +1,7 @@ #include #include +#include +#include #ifndef TYPES_H #define TYPES_H @@ -12,6 +14,13 @@ typedef struct Vertex { vec2 pos; } Vertex; +typedef struct File { + char *path; + char *content; + bool error; + time_t last_write; +} File; + typedef struct ShaderProgram { GLuint program;