aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorc2023-11-25 22:52:11 -0500
committerc2023-11-25 22:52:11 -0500
commit418543ed8d974b06b71694da5c71059eb45897b5 (patch)
tree65f0fc3122acf1d5b25b1d912cdea20dcc454e4c /src/util.c
Initial commit.HEADmaster
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..b36558b
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,65 @@
+#include "include/util.h"
+
+void util_die(const char* fmt, ...) {
+ va_list ap;
+ fprintf(stderr, "[\e[31m==\e[0m] ");
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+ exit(1);
+}
+
+void util_log(const char* fmt, ...) {
+ va_list ap;
+ fprintf(stderr, "[\e[36m===\e[0m] ");
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+}
+
+char* util_get_src(char* arg) {
+ if (arg) {
+ FILE* f = fopen(arg, "rb");
+ return util_src_from_fpath(f);
+ } else {
+ return util_src_from_stdin();
+ }
+}
+
+char* util_src_from_stdin() {
+ size_t len = 0;
+ char* src = calloc(len, sizeof(char));
+
+ while (src[len - 1] != '%') {
+ char c = getchar();
+ src = realloc(src, (len + sizeof(char)));
+ memcpy(src + len, &c, sizeof(char));
+ len += sizeof(char);
+ }
+
+ src[len - 1] = '\0'; // drop the last '%' char
+
+ return src;
+}
+
+char* util_src_from_fpath(FILE* f) {
+ if (!f) {
+ util_die("source file not found");
+ }
+
+ fseek(f, 0L, SEEK_END);
+ long fsize = ftell(f);
+ rewind(f);
+
+ char* src = calloc(1, fsize + 1);
+
+ if ((fread(src, fsize, 1, f) != 1) || !src) {
+ fclose(f);
+ free(src);
+ util_die("could not read source file");
+ }
+
+ return src;
+}