-
Notifications
You must be signed in to change notification settings - Fork 2
/
strprintf.cc
47 lines (41 loc) · 997 Bytes
/
strprintf.cc
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
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) 2010-2013 David Caldwell <david@porkrind.org>
// Licenced under the GPL 3.0 or any later version. See LICENSE file for details.
#include <stdexcept>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include "strprintf.h"
using std::string;
static string vstrprintf(const char *format, va_list ap)
{
char *buf;
vasprintf(&buf, format, ap);
if (!buf)
return string("");
string s(buf);
free(buf);
return s;
}
string strprintf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
return vstrprintf(format, ap);
}
bool throw_str(const char *format, ...)
{
va_list ap;
va_start(ap, format);
throw std::runtime_error(vstrprintf(format, ap));
return false;
}
bool throw_strerr(const char *format, ...)
{
va_list ap;
va_start(ap, format);
throw std::runtime_error(vstrprintf(format, ap) + ": " + strerror(errno));
return false;
}