file utility
This commit is contained in:
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
||||||
bin_PROGRAMS = forge
|
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
|
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
|
||||||
@@ -91,7 +91,7 @@ make -f Makefile.dev release-arch
|
|||||||
- [ ] Minimal working fragment sample
|
- [ ] Minimal working fragment sample
|
||||||
- [ ] Hot-reload fragment shader
|
- [ ] Hot-reload fragment shader
|
||||||
- [x] Force fullscreen
|
- [x] Force fullscreen
|
||||||
- [ ] Select screen as argument / config
|
- [x] Select screen as argument / config
|
||||||
- [ ] Midi
|
- [ ] Midi
|
||||||
- [ ] Read Midi events
|
- [ ] Read Midi events
|
||||||
- [ ] Read midi mapping config file
|
- [ ] Read midi mapping config file
|
||||||
@@ -105,3 +105,4 @@ make -f Makefile.dev release-arch
|
|||||||
- [ ] Video input
|
- [ ] Video input
|
||||||
- [ ] Feedback input
|
- [ ] Feedback input
|
||||||
- [ ] Tap-tempo feature
|
- [ ] Tap-tempo feature
|
||||||
|
- [ ] Monitor screen feature (other window)
|
||||||
@@ -6,6 +6,8 @@ AC_CHECK_HEADERS([stdlib.h])
|
|||||||
AC_CHECK_HEADERS([stdbool.h])
|
AC_CHECK_HEADERS([stdbool.h])
|
||||||
AC_CHECK_HEADERS([string.h])
|
AC_CHECK_HEADERS([string.h])
|
||||||
AC_CHECK_HEADERS([time.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_CHECK_HEADERS([GLFW/glfw3.h])
|
||||||
AC_CONFIG_FILES([Makefile])
|
AC_CONFIG_FILES([Makefile])
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
+62
@@ -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
@@ -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
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
#include <linmath.h>
|
#include <linmath.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#ifndef TYPES_H
|
#ifndef TYPES_H
|
||||||
#define TYPES_H
|
#define TYPES_H
|
||||||
@@ -12,6 +14,13 @@ typedef struct Vertex {
|
|||||||
vec2 pos;
|
vec2 pos;
|
||||||
} Vertex;
|
} Vertex;
|
||||||
|
|
||||||
|
typedef struct File {
|
||||||
|
char *path;
|
||||||
|
char *content;
|
||||||
|
bool error;
|
||||||
|
time_t last_write;
|
||||||
|
} File;
|
||||||
|
|
||||||
typedef struct ShaderProgram {
|
typedef struct ShaderProgram {
|
||||||
GLuint program;
|
GLuint program;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user