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