aboutsummaryrefslogtreecommitdiff
path: root/lib.h
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-12-24 23:08:52 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-12-24 23:08:52 +0000
commit3c39df6c9534d5c45427339e028cee38968adbff (patch)
treec39bf8956ff0c0109502cd9f48eef26c7c40583e /lib.h
parent70cfeac9959310944619bcf45f039d48f10f8c20 (diff)
downloadobf-3c39df6c9534d5c45427339e028cee38968adbff.tar.gz
obf-3c39df6c9534d5c45427339e028cee38968adbff.tar.bz2
obf-3c39df6c9534d5c45427339e028cee38968adbff.zip
Implement SIZE_FMT and vec_append_fmt
SIZE_FMT gets the size of a cstr with the given sprintf formatted string and arguments using `snprintf`. vec_append_fmt formats a cstr with the given sprintf arguments then appends it to the given vector. This simplifies a little bit of the wordy implementation of the assembler.
Diffstat (limited to 'lib.h')
-rw-r--r--lib.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib.h b/lib.h
index c58d425..9d964f7 100644
--- a/lib.h
+++ b/lib.h
@@ -26,6 +26,8 @@ char *fread_all(FILE *fp);
void print_error(const char *handle, size_t row, size_t column,
const char *reason);
+#define SIZE_FMT(FMT, ...) (snprintf(NULL, 0, (FMT), __VA_ARGS__))
+
typedef struct Buffer
{
const char *name;
@@ -46,4 +48,12 @@ void vec_ensure(vec_t *vec, u64 abs_size);
void vec_ensure_free(vec_t *vec, u64 rel_size);
void vec_free(vec_t *vec);
+#define vec_append_fmt(VEC, FMT, ...) \
+ do \
+ { \
+ char f[SIZE_FMT(FMT, __VA_ARGS__) + 1]; \
+ snprintf(f, sizeof(f), (FMT), __VA_ARGS__); \
+ vec_append((VEC), f, sizeof(f) - 1); \
+ } while (0)
+
#endif