Implement gebs_fmt() as a function

This commit is contained in:
kamkow1
2025-05-20 00:06:05 +02:00
parent c1e0a5ec35
commit c015a4d861

25
gebs.h
View File

@ -24,6 +24,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdarg.h>
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
# include <unistd.h>
@ -262,13 +263,7 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
extern Gebs_Arena *gebs_scratch_arena;
#define gebs_fmt(fmt, ...) \
({ \
size_t __size = snprintf(NULL, 0, (fmt), ##__VA_ARGS__); \
char *__buf = gebs_malloc(gebs_scratch_arena, __size+1); \
sprintf(__buf, (fmt), ##__VA_ARGS__); \
__buf; \
})
char *gebs_fmt(const char *fmt, ...);
#ifdef GEBS_IMPLEMENTATION
@ -468,6 +463,22 @@ void gebs_scratch_areana_free(void)
gebs_arena_destroy(gebs_scratch_arena);
}
char *gebs_fmt(const char *fmt, ...)
{
va_list list;
va_start(list, fmt);
va_list list_copy;
va_copy(list_copy, list);
size_t size = vsnprintf(NULL, 0, fmt, list_copy);
va_end(list_copy);
char *buf = gebs_malloc(gebs_scratch_arena, size+1);
vsprintf(buf, fmt, list);
va_end(list);
return buf;
}
// ----------------------------------------------------------------------------
// Filesystem operations
// ----------------------------------------------------------------------------