refactor(optimize): use fixed size strings everywhere except for files
This commit is contained in:
+10
-9
@@ -72,7 +72,9 @@ static void invalid_value(char *arg, char *value) {
|
|||||||
print_help(EXIT_FAILURE);
|
print_help(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_arg(char *arg, char *ref) { return strcoll(arg, ref) == 0; }
|
static bool is_arg(char *arg, const char *ref) {
|
||||||
|
return strcoll(arg, ref) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
static char *split_arg_value(char *arg) {
|
static char *split_arg_value(char *arg) {
|
||||||
strtok(arg, "=");
|
strtok(arg, "=");
|
||||||
@@ -101,9 +103,9 @@ Parameters args_parse(int argc, char **argv) {
|
|||||||
params.output_screen = 0;
|
params.output_screen = 0;
|
||||||
params.monitor = false;
|
params.monitor = false;
|
||||||
params.monitor_screen = 0;
|
params.monitor_screen = 0;
|
||||||
params.frag_path = DATADIR "/shaders";
|
strncpy(params.frag_path, DATADIR "/shaders", STR_LEN);
|
||||||
params.config_path = DATADIR "/default.cfg";
|
strncpy(params.config_path, DATADIR "/default.cfg", STR_LEN);
|
||||||
params.state_file = "forge_saved_state.txt";
|
strncpy(params.state_file, "forge_saved_state.txt", STR_LEN);
|
||||||
params.load_state = true;
|
params.load_state = true;
|
||||||
params.save_state = false;
|
params.save_state = false;
|
||||||
params.internal_size = 720;
|
params.internal_size = 720;
|
||||||
@@ -126,11 +128,11 @@ Parameters args_parse(int argc, char **argv) {
|
|||||||
} else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) {
|
} else if (is_arg(arg, "-s") || is_arg(arg, "--screen")) {
|
||||||
params.output_screen = parse_uint(arg, value);
|
params.output_screen = parse_uint(arg, value);
|
||||||
} else if (is_arg(arg, "-f") || is_arg(arg, "--frag")) {
|
} else if (is_arg(arg, "-f") || is_arg(arg, "--frag")) {
|
||||||
params.frag_path = value;
|
strncpy(params.frag_path, value, STR_LEN);
|
||||||
} else if (is_arg(arg, "-c") || is_arg(arg, "--config")) {
|
} else if (is_arg(arg, "-c") || is_arg(arg, "--config")) {
|
||||||
params.config_path = value;
|
strncpy(params.config_path, value, STR_LEN);
|
||||||
} else if (is_arg(arg, "-sf") || is_arg(arg, "--state-file")) {
|
} else if (is_arg(arg, "-sf") || is_arg(arg, "--state-file")) {
|
||||||
params.state_file = value;
|
strncpy(params.state_file, value, STR_LEN);
|
||||||
} else if (is_arg(arg, "-ls") || is_arg(arg, "--load-state")) {
|
} else if (is_arg(arg, "-ls") || is_arg(arg, "--load-state")) {
|
||||||
params.load_state = true;
|
params.load_state = true;
|
||||||
} else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) {
|
} else if (is_arg(arg, "-nls") || is_arg(arg, "--no-load-state")) {
|
||||||
@@ -149,8 +151,7 @@ Parameters args_parse(int argc, char **argv) {
|
|||||||
log_error("maximum video input reached");
|
log_error("maximum video input reached");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
strncpy(params.video_in.values[params.video_in.length++], value, STR_LEN);
|
||||||
params.video_in.values[params.video_in.length++] = value;
|
|
||||||
} else if (is_arg(arg, "-vs") || is_arg(arg, "--video-size")) {
|
} else if (is_arg(arg, "-vs") || is_arg(arg, "--video-size")) {
|
||||||
params.video_size = parse_uint(arg, value);
|
params.video_size = parse_uint(arg, value);
|
||||||
if (params.video_size == 0) {
|
if (params.video_size == 0) {
|
||||||
|
|||||||
@@ -15,6 +15,12 @@
|
|||||||
#define DATADIR "."
|
#define DATADIR "."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* STRINGS */
|
||||||
|
|
||||||
|
#ifndef STR_LEN
|
||||||
|
#define STR_LEN 1024
|
||||||
|
#endif
|
||||||
|
|
||||||
/* TYPES */
|
/* TYPES */
|
||||||
|
|
||||||
#ifndef MAX_VIDEO
|
#ifndef MAX_VIDEO
|
||||||
|
|||||||
+5
-4
@@ -3,6 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "config_file.h"
|
#include "config_file.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
@@ -61,7 +62,7 @@ static void parse_config_file_line(ConfigFile config, char *line) {
|
|||||||
hashmap_set(config.map, &item);
|
hashmap_set(config.map, &item);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigFile config_file_read(char *path, bool free_path) {
|
ConfigFile config_file_read(char *path) {
|
||||||
File file;
|
File file;
|
||||||
ConfigFile config;
|
ConfigFile config;
|
||||||
char *line;
|
char *line;
|
||||||
@@ -82,7 +83,7 @@ ConfigFile config_file_read(char *path, bool free_path) {
|
|||||||
line = strtok(NULL, "\n");
|
line = strtok(NULL, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
file_free(&file, free_path);
|
file_free(&file);
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
@@ -91,7 +92,7 @@ char *config_file_get_str(ConfigFile config, char *key, char *default_value) {
|
|||||||
ConfigFileItem c_key;
|
ConfigFileItem c_key;
|
||||||
ConfigFileItem *item;
|
ConfigFileItem *item;
|
||||||
|
|
||||||
strcpy(c_key.key, key);
|
strncpy(c_key.key, key, STR_LEN);
|
||||||
|
|
||||||
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
||||||
|
|
||||||
@@ -107,7 +108,7 @@ unsigned int config_file_get_int(ConfigFile config, char *key,
|
|||||||
ConfigFileItem c_key;
|
ConfigFileItem c_key;
|
||||||
ConfigFileItem *item;
|
ConfigFileItem *item;
|
||||||
|
|
||||||
strcpy(c_key.key, key);
|
strncpy(c_key.key, key, STR_LEN);
|
||||||
|
|
||||||
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
item = (ConfigFileItem *)hashmap_get(config.map, &c_key);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
#ifndef CONFIG_FILE_H
|
#ifndef CONFIG_FILE_H
|
||||||
#define CONFIG_FILE_H
|
#define CONFIG_FILE_H
|
||||||
|
|
||||||
ConfigFile config_file_read(char *path, bool free_path);
|
ConfigFile config_file_read(char *path);
|
||||||
|
|
||||||
char *config_file_get_str(ConfigFile config, char *key, char *default_value);
|
char *config_file_get_str(ConfigFile config, char *key, char *default_value);
|
||||||
|
|
||||||
|
|||||||
+3
-8
@@ -66,7 +66,7 @@ void file_update(File *file) {
|
|||||||
File file_read(char *path) {
|
File file_read(char *path) {
|
||||||
File file;
|
File file;
|
||||||
|
|
||||||
file.path = path;
|
sprintf(file.path, path, STR_LEN);
|
||||||
file.content = NULL;
|
file.content = NULL;
|
||||||
file.error = false;
|
file.error = false;
|
||||||
file.last_write = 0;
|
file.last_write = 0;
|
||||||
@@ -75,7 +75,7 @@ File file_read(char *path) {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_write(char *path, ConstStringArray lines) {
|
void file_write(char *path, StringArray lines) {
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
FILE *file_pointer;
|
FILE *file_pointer;
|
||||||
|
|
||||||
@@ -105,9 +105,4 @@ void file_prepend(File *src, File extra) {
|
|||||||
free(old_src_content);
|
free(old_src_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_free(File *file, bool free_path) {
|
void file_free(File *file) { free(file->content); }
|
||||||
free(file->content);
|
|
||||||
if (free_path) {
|
|
||||||
free(file->path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-2
@@ -11,8 +11,8 @@ void file_update(File *file);
|
|||||||
|
|
||||||
void file_prepend(File *src, File extra);
|
void file_prepend(File *src, File extra);
|
||||||
|
|
||||||
void file_free(File *file, bool free_path);
|
void file_write(char *path, StringArray lines);
|
||||||
|
|
||||||
void file_write(char *path, ConstStringArray lines);
|
void file_free(File *file);
|
||||||
|
|
||||||
#endif /* FILE_H */
|
#endif /* FILE_H */
|
||||||
+5
-7
@@ -101,11 +101,9 @@ static void hot_reload() {
|
|||||||
|
|
||||||
File read_fragment_shader_file(char *frag_path, unsigned int i) {
|
File read_fragment_shader_file(char *frag_path, unsigned int i) {
|
||||||
File fragment_shader;
|
File fragment_shader;
|
||||||
char *file_path;
|
char file_path[STR_LEN];
|
||||||
|
|
||||||
file_path = malloc(sizeof(char) * 1024);
|
snprintf(file_path, STR_LEN, "%s/frag%d.glsl", frag_path, i);
|
||||||
|
|
||||||
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
|
|
||||||
fragment_shader = file_read(file_path);
|
fragment_shader = file_read(file_path);
|
||||||
if (fragment_shader.error) {
|
if (fragment_shader.error) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -134,10 +132,10 @@ static void free_files(unsigned int frag_count) {
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < frag_count; i++) {
|
for (i = 0; i < frag_count; i++) {
|
||||||
file_free(&fragment_shaders.values[i], true);
|
file_free(&fragment_shaders.values[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_free(&common_shader_code, true);
|
file_free(&common_shader_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_inputs(StringArray video_in, unsigned int video_size) {
|
static void init_inputs(StringArray video_in, unsigned int video_size) {
|
||||||
@@ -237,7 +235,7 @@ void forge_run(Parameters params) {
|
|||||||
|
|
||||||
context->stop = false;
|
context->stop = false;
|
||||||
|
|
||||||
config = config_file_read(params.config_path, false);
|
config = config_file_read(params.config_path);
|
||||||
|
|
||||||
frag_count = config_file_get_int(config, "FRAG_COUNT", 1);
|
frag_count = config_file_get_int(config, "FRAG_COUNT", 1);
|
||||||
in_count = config_file_get_int(config, "IN_COUNT", 0);
|
in_count = config_file_get_int(config, "IN_COUNT", 0);
|
||||||
|
|||||||
+2
-1
@@ -1,13 +1,14 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
MidiDevice midi_open(char *name) {
|
MidiDevice midi_open(char *name) {
|
||||||
MidiDevice device;
|
MidiDevice device;
|
||||||
|
|
||||||
device.name = name;
|
strncpy(device.name, name, STR_LEN);
|
||||||
device.input = NULL;
|
device.input = NULL;
|
||||||
device.output = NULL;
|
device.output = NULL;
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -319,13 +319,13 @@ bool state_background_midi_write(SharedContext *context,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void state_load(SharedContext *context, StateConfig state_config,
|
static void state_load(SharedContext *context, StateConfig state_config,
|
||||||
char *state_file) {
|
char *state_file) {
|
||||||
ConfigFile saved_state;
|
ConfigFile saved_state;
|
||||||
char key[100];
|
char key[100];
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
saved_state = config_file_read(state_file, false);
|
saved_state = config_file_read(state_file);
|
||||||
|
|
||||||
tempo_set(&context->tempo,
|
tempo_set(&context->tempo,
|
||||||
config_file_get_int(saved_state, "tempo", context->tempo.tempo));
|
config_file_get_int(saved_state, "tempo", context->tempo.tempo));
|
||||||
@@ -402,7 +402,7 @@ void state_randomize(SharedContext *context, StateConfig state_config) {
|
|||||||
|
|
||||||
void state_save(SharedContext *context, StateConfig state_config,
|
void state_save(SharedContext *context, StateConfig state_config,
|
||||||
char *state_file) {
|
char *state_file) {
|
||||||
ConstStringArray lines;
|
StringArray lines;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
log_info("Saving state to '%s'...", state_file);
|
log_info("Saving state to '%s'...", state_file);
|
||||||
|
|||||||
+12
-12
@@ -21,14 +21,13 @@
|
|||||||
} X
|
} X
|
||||||
|
|
||||||
typedef ARRAY(UintArray, unsigned int);
|
typedef ARRAY(UintArray, unsigned int);
|
||||||
typedef ARRAY(StringArray, char *);
|
|
||||||
typedef ARRAY(Vec3Array, vec3);
|
typedef ARRAY(Vec3Array, vec3);
|
||||||
typedef ARRAY(GLuintArray, GLuint);
|
typedef ARRAY(GLuintArray, GLuint);
|
||||||
|
|
||||||
typedef struct ConstStringArray {
|
typedef struct StringArray {
|
||||||
char values[ARRAY_SIZE][1000];
|
char values[ARRAY_SIZE][STR_LEN];
|
||||||
unsigned int length;
|
unsigned int length;
|
||||||
} ConstStringArray;
|
} StringArray;
|
||||||
|
|
||||||
typedef struct Parameters {
|
typedef struct Parameters {
|
||||||
bool hot_reload;
|
bool hot_reload;
|
||||||
@@ -36,9 +35,9 @@ typedef struct Parameters {
|
|||||||
unsigned int output_screen;
|
unsigned int output_screen;
|
||||||
bool monitor;
|
bool monitor;
|
||||||
unsigned int monitor_screen;
|
unsigned int monitor_screen;
|
||||||
char *frag_path;
|
char frag_path[STR_LEN];
|
||||||
char *config_path;
|
char config_path[STR_LEN];
|
||||||
char *state_file;
|
char state_file[STR_LEN];
|
||||||
bool load_state;
|
bool load_state;
|
||||||
bool save_state;
|
bool save_state;
|
||||||
unsigned int internal_size;
|
unsigned int internal_size;
|
||||||
@@ -54,8 +53,9 @@ typedef struct Vertex {
|
|||||||
} Vertex;
|
} Vertex;
|
||||||
|
|
||||||
typedef struct File {
|
typedef struct File {
|
||||||
char *path;
|
char path[STR_LEN];
|
||||||
char *content;
|
char *content;
|
||||||
|
unsigned int length;
|
||||||
bool error;
|
bool error;
|
||||||
time_t last_write;
|
time_t last_write;
|
||||||
} File;
|
} File;
|
||||||
@@ -117,7 +117,7 @@ typedef struct ShaderProgram {
|
|||||||
} ShaderProgram;
|
} ShaderProgram;
|
||||||
|
|
||||||
typedef struct VideoCapture {
|
typedef struct VideoCapture {
|
||||||
char *name;
|
char name[STR_LEN];
|
||||||
bool error;
|
bool error;
|
||||||
int fd;
|
int fd;
|
||||||
int exp_fd;
|
int exp_fd;
|
||||||
@@ -198,13 +198,13 @@ typedef struct ConfigFile {
|
|||||||
} ConfigFile;
|
} ConfigFile;
|
||||||
|
|
||||||
typedef struct ConfigFileItem {
|
typedef struct ConfigFileItem {
|
||||||
char key[256];
|
char key[STR_LEN];
|
||||||
char value[2048];
|
char value[STR_LEN];
|
||||||
} ConfigFileItem;
|
} ConfigFileItem;
|
||||||
|
|
||||||
typedef struct MidiDevice {
|
typedef struct MidiDevice {
|
||||||
bool error;
|
bool error;
|
||||||
char *name;
|
char name[STR_LEN];
|
||||||
snd_rawmidi_t *input;
|
snd_rawmidi_t *input;
|
||||||
snd_rawmidi_t *output;
|
snd_rawmidi_t *output;
|
||||||
} MidiDevice;
|
} MidiDevice;
|
||||||
|
|||||||
+2
-1
@@ -8,6 +8,7 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "video.h"
|
#include "video.h"
|
||||||
@@ -66,7 +67,7 @@ static void ioctl_error(VideoCapture *video_capture, const char *operation,
|
|||||||
static VideoCapture open_device(char *name) {
|
static VideoCapture open_device(char *name) {
|
||||||
VideoCapture video_capture;
|
VideoCapture video_capture;
|
||||||
|
|
||||||
video_capture.name = name;
|
strncpy(video_capture.name, name, STR_LEN);
|
||||||
video_capture.error = false;
|
video_capture.error = false;
|
||||||
video_capture.fd = -1;
|
video_capture.fd = -1;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user