aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/fmt.cpp
blob: 8ef11647d96015c0774c61683909e8f9bf3f6446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdarg>
#include <cstdio>
#include <string>

#include "fmt.h"

using namespace std;

string crepe::util::va_stringf(va_list args, const char * fmt) {
	va_list args_copy;
	va_copy(args_copy, args);

	size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1;
	char * msg = (char *) malloc(sz);
	va_end(args_copy);

	vsnprintf(msg, sz, fmt, args);

	string out = msg;
	free(msg);

	va_end(args);

	return out;
}

string crepe::util::stringf(const char * fmt, ...) {
	va_list args;
	va_start(args, fmt);
	string out = va_stringf(args, fmt);
	va_end(args);
	return out;
}