Implement gebs_fmt() as a function
This commit is contained in:
25
gebs.h
25
gebs.h
@ -24,6 +24,7 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
|
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
|
||||||
# include <unistd.h>
|
# 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;
|
extern Gebs_Arena *gebs_scratch_arena;
|
||||||
|
|
||||||
#define gebs_fmt(fmt, ...) \
|
char *gebs_fmt(const char *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; \
|
|
||||||
})
|
|
||||||
|
|
||||||
#ifdef GEBS_IMPLEMENTATION
|
#ifdef GEBS_IMPLEMENTATION
|
||||||
|
|
||||||
@ -468,6 +463,22 @@ void gebs_scratch_areana_free(void)
|
|||||||
gebs_arena_destroy(gebs_scratch_arena);
|
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
|
// Filesystem operations
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user