chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / basic / stdio-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4
5 #if 0 /// elogind is musl-libc compatible and does not directly include printf.h
6 #include <printf.h>
7 #else
8 #include "parse-printf-format.h"
9 #endif // 0
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <sys/types.h>
13
14 #include "macro.h"
15
16 #define snprintf_ok(buf, len, fmt, ...) \
17         ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
18
19 #define xsprintf(buf, fmt, ...) \
20         assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
21
22 #define VA_FORMAT_ADVANCE(format, ap)                                   \
23 do {                                                                    \
24         int _argtypes[128];                                             \
25         size_t _i, _k;                                                  \
26         _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
27         assert(_k < ELEMENTSOF(_argtypes));                             \
28         for (_i = 0; _i < _k; _i++) {                                   \
29                 if (_argtypes[_i] & PA_FLAG_PTR)  {                     \
30                         (void) va_arg(ap, void*);                       \
31                         continue;                                       \
32                 }                                                       \
33                                                                         \
34                 switch (_argtypes[_i]) {                                \
35                 case PA_INT:                                            \
36                 case PA_INT|PA_FLAG_SHORT:                              \
37                 case PA_CHAR:                                           \
38                         (void) va_arg(ap, int);                         \
39                         break;                                          \
40                 case PA_INT|PA_FLAG_LONG:                               \
41                         (void) va_arg(ap, long int);                    \
42                         break;                                          \
43                 case PA_INT|PA_FLAG_LONG_LONG:                          \
44                         (void) va_arg(ap, long long int);               \
45                         break;                                          \
46                 case PA_WCHAR:                                          \
47                         (void) va_arg(ap, wchar_t);                     \
48                         break;                                          \
49                 case PA_WSTRING:                                        \
50                 case PA_STRING:                                         \
51                 case PA_POINTER:                                        \
52                         (void) va_arg(ap, void*);                       \
53                         break;                                          \
54                 case PA_FLOAT:                                          \
55                 case PA_DOUBLE:                                         \
56                         (void) va_arg(ap, double);                      \
57                         break;                                          \
58                 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:                     \
59                         (void) va_arg(ap, long double);                 \
60                         break;                                          \
61                 default:                                                \
62                         assert_not_reached("Unknown format string argument."); \
63                 }                                                       \
64         }                                                               \
65 } while (false)