diff options
author | zlago | 2024-09-02 11:20:02 +0200 |
---|---|---|
committer | zlago | 2024-09-02 11:20:02 +0200 |
commit | 6d30e75c7684817b9637a370f494040a1135b876 (patch) | |
tree | 2148b98728f760a726ac4e3a8b4f9c35425fc49a | |
parent | e1dbcc7c80e6930f223e4617385931aad1fff2c7 (diff) |
un-copy paste some stuff
-rw-r--r-- | GNUmakefile | 8 | ||||
-rw-r--r-- | src/SDL2.c | 52 | ||||
-rw-r--r-- | src/common/common.c | 54 | ||||
-rw-r--r-- | src/common/common.h | 7 | ||||
-rw-r--r-- | src/portaudio.c | 56 |
5 files changed, 70 insertions, 107 deletions
diff --git a/GNUmakefile b/GNUmakefile index f7eb64a..8ef4aec 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -7,7 +7,7 @@ libs ::= openmpt fluidsynth cflags ::= -I . -g -Og ${CFLAGS} ldflags ::= -Wl,--rpath,'$$ORIGIN' $(addprefix -l,${libs}) ${LDFLAGS} -srcs ::= $(wildcard src/modules/*.c) +srcs ::= $(wildcard src/modules/*.c src/common/*.c) objs ::= $(addprefix out/,$(notdir ${srcs:.c=.o})) deps ::= $(addprefix out/,$(notdir ${srcs:.c=.d})) @@ -30,6 +30,12 @@ out/%.o: src/modules/%.c out/%.d | out/ out/%.d: src/modules/%.c | out/ ${CC} ${cflags} ${CPPFLAGS} -MM -MG -MF $@ -MT "${@:.d=.o} $@" $< +out/%.o: src/common/%.c out/%.d | out/ + ${CC} -c -o $@ $< ${cflags} + +out/%.d: src/common/%.c | out/ + ${CC} ${cflags} ${CPPFLAGS} -MM -MG -MF $@ -MT "${@:.d=.o} $@" $< + out/%.o: src/%.c out/%.d | out/ ${CC} -c -o $@ $< ${cflags} diff --git a/src/SDL2.c b/src/SDL2.c index 4e51269..79b4e4a 100644 --- a/src/SDL2.c +++ b/src/SDL2.c @@ -6,9 +6,7 @@ #include <stdio.h> #include "include.h" - -int module_openmpt(struct blob *file, struct userdata *userdata); -int module_fluidsynth(struct blob *file, struct userdata *userdata); +#include "common/common.h" #define eprintf(...) fprintf(stderr, __VA_ARGS__) @@ -171,51 +169,3 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } - -int (*file_ext(char *file))(struct blob *, struct userdata *) { - size_t len = strlen(file); - #define ext(extension) memcmp(file + len - sizeof (extension) + 1, extension, sizeof (extension)) - if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) { - return module_openmpt; - } else if ((ext(".mid") && ext(".midi")) == 0) { - return module_fluidsynth; - } - #undef ext - return NULL; -} - -struct blob load_file(char const *const name) { - const size_t START_SIZE = 1; - FILE *file = fopen(name, "rb"); - if (file == NULL) { - return (struct blob) {.data = NULL}; - } - void *data = malloc(START_SIZE); - size_t allocated = START_SIZE; - size_t used = 0; - while (1) { - size_t read = fread(data + used, 1, allocated - used, file); - if (read != allocated - used) { - used += read; - break; - } - used += read; - allocated *= 2; - void *const newdata = realloc(data, allocated); - if (newdata == NULL) { - goto realloc_error; - } - data = newdata; - } - void *const newdata = realloc(data, used); - if (newdata == NULL && used != 0) { - goto realloc_error; - } - fclose(file); - return (struct blob) {.data = newdata, .size = used}; - - realloc_error: - free(data); - fclose(file); - return (struct blob) {.data = NULL}; -} diff --git a/src/common/common.c b/src/common/common.c new file mode 100644 index 0000000..508140d --- /dev/null +++ b/src/common/common.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../include.h" +#include "common.h" + +int (*file_ext(char *file))(struct blob *, struct userdata *) { + size_t len = strlen(file); + #define ext(extension) memcmp(file + len - sizeof (extension) + 1, extension, sizeof (extension)) + if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) { + return module_openmpt; + } else if ((ext(".mid") && ext(".midi")) == 0) { + return module_fluidsynth; + } + #undef ext + return NULL; +} + +struct blob load_file(char const *const name) { + const size_t START_SIZE = 1; + FILE *file = fopen(name, "rb"); + if (file == NULL) { + return (struct blob) {.data = NULL}; + } + void *data = malloc(START_SIZE); + size_t allocated = START_SIZE; + size_t used = 0; + while (1) { + size_t read = fread(data + used, 1, allocated - used, file); + if (read != allocated - used) { + used += read; + break; + } + used += read; + allocated *= 2; + void *const newdata = realloc(data, allocated); + if (newdata == NULL) { + goto realloc_error; + } + data = newdata; + } + void *const newdata = realloc(data, used); + if (newdata == NULL && used != 0) { + goto realloc_error; + } + fclose(file); + return (struct blob) {.data = newdata, .size = used}; + + realloc_error: + free(data); + fclose(file); + return (struct blob) {.data = NULL}; +} diff --git a/src/common/common.h b/src/common/common.h new file mode 100644 index 0000000..806212a --- /dev/null +++ b/src/common/common.h @@ -0,0 +1,7 @@ +#include "../include.h" + +int (*file_ext(char *file))(struct blob *, struct userdata *); +struct blob load_file(char const *const name); + +int module_openmpt(struct blob *file, struct userdata *userdata); +int module_fluidsynth(struct blob *file, struct userdata *userdata); diff --git a/src/portaudio.c b/src/portaudio.c index f4ca4b7..e8e9839 100644 --- a/src/portaudio.c +++ b/src/portaudio.c @@ -1,21 +1,15 @@ #define SDL_MAIN_HANDLED #include <portaudio.h> -#include <stdbool.h> #include <stdlib.h> #include <stdio.h> -#include <string.h> #include <signal.h> #include "include.h" - -int module_openmpt(struct blob *file, struct userdata *userdata); -int module_fluidsynth(struct blob *file, struct userdata *userdata); +#include "common/common.h" #define eprintf(...) fprintf(stderr, __VA_ARGS__) -static const int WINDOW_WIDTH = 160, WINDOW_HEIGHT = 90; - volatile sig_atomic_t keep_going = 1; void sigint_handler(int sig_num) { @@ -112,51 +106,3 @@ int main(int argc, char **argv) { error: return EXIT_FAILURE; } - -int (*file_ext(char *file))(struct blob *, struct userdata *) { - size_t len = strlen(file); - #define ext(extension) memcmp(file + len - sizeof (extension) + 1, extension, sizeof (extension)) - if ((ext(".mptm") && ext(".mod") && ext(".xm") && ext(".s3m") && ext(".it")) == 0) { - return module_openmpt; - } else if ((ext(".mid") && ext(".midi")) == 0) { - return module_fluidsynth; - } - #undef ext - return NULL; -} - -struct blob load_file(char const *const name) { - const size_t START_SIZE = 1; - FILE *file = fopen(name, "rb"); - if (file == NULL) { - return (struct blob) {.data = NULL}; - } - void *data = malloc(START_SIZE); - size_t allocated = START_SIZE; - size_t used = 0; - while (1) { - size_t read = fread(data + used, 1, allocated - used, file); - if (read != allocated - used) { - used += read; - break; - } - used += read; - allocated *= 2; - void *const newdata = realloc(data, allocated); - if (newdata == NULL) { - goto realloc_error; - } - data = newdata; - } - void *const newdata = realloc(data, used); - if (newdata == NULL && used != 0) { - goto realloc_error; - } - fclose(file); - return (struct blob) {.data = newdata, .size = used}; - - realloc_error: - free(data); - fclose(file); - return (struct blob) {.data = NULL}; -} |