aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/fmt.cpp
blob: 3a43a6f33eb2c7de1357184a5adf61856c933338 (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
34
#include <string>
#include <cstdio>
#include <cstdarg>

#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;
}