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