]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/stdio-util.h
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.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 #include <printf.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12
13 #include "macro.h"
14
15 #define snprintf_ok(buf, len, fmt, ...) \
16 ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
17
18 #define xsprintf(buf, fmt, ...) \
19 assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
20
21 #define VA_FORMAT_ADVANCE(format, ap) \
22 do { \
23 int _argtypes[128]; \
24 size_t _i, _k; \
25 _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
26 assert(_k < ELEMENTSOF(_argtypes)); \
27 for (_i = 0; _i < _k; _i++) { \
28 if (_argtypes[_i] & PA_FLAG_PTR) { \
29 (void) va_arg(ap, void*); \
30 continue; \
31 } \
32 \
33 switch (_argtypes[_i]) { \
34 case PA_INT: \
35 case PA_INT|PA_FLAG_SHORT: \
36 case PA_CHAR: \
37 (void) va_arg(ap, int); \
38 break; \
39 case PA_INT|PA_FLAG_LONG: \
40 (void) va_arg(ap, long int); \
41 break; \
42 case PA_INT|PA_FLAG_LONG_LONG: \
43 (void) va_arg(ap, long long int); \
44 break; \
45 case PA_WCHAR: \
46 (void) va_arg(ap, wchar_t); \
47 break; \
48 case PA_WSTRING: \
49 case PA_STRING: \
50 case PA_POINTER: \
51 (void) va_arg(ap, void*); \
52 break; \
53 case PA_FLOAT: \
54 case PA_DOUBLE: \
55 (void) va_arg(ap, double); \
56 break; \
57 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
58 (void) va_arg(ap, long double); \
59 break; \
60 default: \
61 assert_not_reached("Unknown format string argument."); \
62 } \
63 } \
64 } while (false)