libwreport 3.40
compat.h
1#ifndef WREPORT_COMPAT_H
2#define WREPORT_COMPAT_H
3
4#include "config.h"
5#include <cmath>
6#include <cstdarg>
7#include <cstdlib>
8#include <cstring>
9
10#ifdef USE_OWN_VASPRINTF
11static int vasprintf(char** result, const char* format, va_list args)
12{
13 const char* p = format;
14 /* Add one to make sure that it is never zero, which might cause malloc
15 to return NULL. */
16 int total_width = strlen(format) + 1;
17 va_list ap;
18
19 memcpy((void*)&ap, (void*)&args, sizeof(va_list));
20
21 while (*p != '\0')
22 {
23 if (*p++ == '%')
24 {
25 while (strchr("-+ #0", *p))
26 ++p;
27 if (*p == '*')
28 {
29 ++p;
30 total_width += abs(va_arg(ap, int));
31 }
32 else
33 total_width += strtoul(p, (char**)&p, 10);
34 if (*p == '.')
35 {
36 ++p;
37 if (*p == '*')
38 {
39 ++p;
40 total_width += abs(va_arg(ap, int));
41 }
42 else
43 total_width += strtoul(p, (char**)&p, 10);
44 }
45 while (strchr("hlL", *p))
46 ++p;
47 /* Should be big enough for any format specifier except %s and
48 * floats. */
49 total_width += 30;
50 switch (*p)
51 {
52 case 'd':
53 case 'i':
54 case 'o':
55 case 'u':
56 case 'x':
57 case 'X':
58 case 'c': (void)va_arg(ap, int); break;
59 case 'f':
60 case 'e':
61 case 'E':
62 case 'g':
63 case 'G':
64 (void)va_arg(ap, double);
65 /* Since an ieee double can have an exponent of 307, we'll
66 make the buffer wide enough to cover the gross case. */
67 total_width += 307;
68 break;
69 case 's': total_width += strlen(va_arg(ap, char*)); break;
70 case 'p':
71 case 'n': (void)va_arg(ap, char*); break;
72 }
73 p++;
74 }
75 }
76 *result = (char*)malloc(total_width);
77 if (*result != NULL)
78 {
79 return vsprintf(*result, format, args);
80 }
81 else
82 {
83 return 0;
84 }
85}
86static int asprintf(char** result, const char* format, ...)
87{
88 va_list ap;
89 va_start(ap, format);
90 int res = vasprintf(result, format, ap);
91 va_end(ap);
92 return res;
93}
94#endif
95
96#endif