summaryrefslogtreecommitdiff
path: root/util.h
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-02-23 13:52:44 -0500
committerpommicket <pommicket@gmail.com>2025-02-25 15:16:08 -0500
commit27b5aa8289330bc7b9f3499bf98a84f0127f4899 (patch)
tree7bffb0ce28a7924d425fde6fc2f7112f1b6ea7da /util.h
parent78f28b310251cd3e35d588c9f1476e3d0ef6d983 (diff)
separate video stuff into its own file
Diffstat (limited to 'util.h')
-rw-r--r--util.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..4db637a
--- /dev/null
+++ b/util.h
@@ -0,0 +1,37 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <time.h>
+#include <stdio.h>
+#include "ds.h"
+
+static char *va_sprintf(const char *fmt, va_list args) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+ char fakebuf[2] = {0};
+ int ret = vsnprintf(fakebuf, 1, fmt, args_copy);
+ va_end(args_copy);
+
+ if (ret < 0) return NULL; // bad format or something
+ size_t n = (size_t)ret;
+ char *str = calloc(1, n + 1);
+ vsnprintf(str, n + 1, fmt, args);
+ return str;
+}
+
+static char *a_sprintf(PRINTF_FORMAT_STRING const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
+static char *a_sprintf(const char *fmt, ...) {
+ // idk if you can always just pass NULL to vsnprintf
+ va_list args;
+ va_start(args, fmt);
+ char *str = va_sprintf(fmt, args);
+ va_end(args);
+ return str;
+}
+
+
+static double get_time_double(void) {
+ struct timespec ts = {0};
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
+}