file utility

This commit is contained in:
2025-09-12 18:41:18 +02:00
parent 45ea7731f0
commit fda0de78c7
6 changed files with 90 additions and 4 deletions
+2 -2
View File
@@ -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
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
+3 -2
View File
@@ -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
- [ ] Tap-tempo feature
- [ ] Monitor screen feature (other window)
+2
View File
@@ -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
+62
View File
@@ -0,0 +1,62 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#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;
}
+12
View File
@@ -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
+9
View File
@@ -1,5 +1,7 @@
#include <glad/gl.h>
#include <linmath.h>
#include <stdbool.h>
#include <time.h>
#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;