chiark / gitweb /
Prep v233: Unmask now needed functions in src/basic
[elogind.git] / src / basic / stdio-util.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #if 0 /// elogind is musl-libc compatible and does not directly include printf.h
23 #include <printf.h>
24 #else
25 #include "parse-printf-format.h"
26 #endif // 0
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30
31 #include "macro.h"
32
33 #define xsprintf(buf, fmt, ...) \
34         assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), "xsprintf: " #buf "[] must be big enough")
35
36
37 #define VA_FORMAT_ADVANCE(format, ap)                                   \
38 do {                                                                    \
39         int _argtypes[128];                                             \
40         size_t _i, _k;                                                  \
41         _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
42         assert(_k < ELEMENTSOF(_argtypes));                             \
43         for (_i = 0; _i < _k; _i++) {                                   \
44                 if (_argtypes[_i] & PA_FLAG_PTR)  {                     \
45                         (void) va_arg(ap, void*);                       \
46                         continue;                                       \
47                 }                                                       \
48                                                                         \
49                 switch (_argtypes[_i]) {                                \
50                 case PA_INT:                                            \
51                 case PA_INT|PA_FLAG_SHORT:                              \
52                 case PA_CHAR:                                           \
53                         (void) va_arg(ap, int);                         \
54                         break;                                          \
55                 case PA_INT|PA_FLAG_LONG:                               \
56                         (void) va_arg(ap, long int);                    \
57                         break;                                          \
58                 case PA_INT|PA_FLAG_LONG_LONG:                          \
59                         (void) va_arg(ap, long long int);               \
60                         break;                                          \
61                 case PA_WCHAR:                                          \
62                         (void) va_arg(ap, wchar_t);                     \
63                         break;                                          \
64                 case PA_WSTRING:                                        \
65                 case PA_STRING:                                         \
66                 case PA_POINTER:                                        \
67                         (void) va_arg(ap, void*);                       \
68                         break;                                          \
69                 case PA_FLOAT:                                          \
70                 case PA_DOUBLE:                                         \
71                         (void) va_arg(ap, double);                      \
72                         break;                                          \
73                 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:                     \
74                         (void) va_arg(ap, long double);                 \
75                         break;                                          \
76                 default:                                                \
77                         assert_not_reached("Unknown format string argument."); \
78                 }                                                       \
79         }                                                               \
80 } while (false)