Lely core libraries 2.3.4
print.h
Go to the documentation of this file.
1
22#ifndef LELY_UTIL_PRINT_H_
23#define LELY_UTIL_PRINT_H_
24
25#include <lely/libc/uchar.h>
26#include <lely/util/util.h>
27
28#include <stdarg.h>
29#include <stddef.h>
30
31#ifndef LELY_UTIL_PRINT_INLINE
32#define LELY_UTIL_PRINT_INLINE static inline
33#endif
34
35#ifndef LENll
37#if _WIN32
38#define LENll "I64"
39#ifdef __MINGW32__
40// Ignore complaints that "I64" is not a valid ISO C length modifier.
41#pragma GCC diagnostic ignored "-Wformat"
42#pragma GCC diagnostic ignored "-Wformat-extra-args"
43#endif
44#else
45#define LENll "ll"
46#endif
47#endif
48
49#ifndef LENj
51#if _WIN32
52#define LENj LENll
53#else
54#define LENj "j"
55#endif
56#endif
57
58#ifndef LENz
60#if _WIN32
61#if __WORDSIZE == 64
62#define LENz LENll
63#else
64#define LENz LENl
65#endif
66#else
67#define LENz "z"
68#endif
69#endif
70
71#ifndef LENt
73#if _WIN32
74#if __WORDSIZE == 64
75#define LENz LENll
76#else
77#define LENz LENl
78#endif
79#else
80#define LENt "t"
81#endif
82#endif
83
84#ifdef __cplusplus
85extern "C" {
86#endif
87
89LELY_UTIL_PRINT_INLINE int otoc(int i);
90
96LELY_UTIL_PRINT_INLINE int xtoc(int i);
97
117size_t print_fmt(char **pbegin, char *end, const char *format, ...)
118 format_printf__(3, 4);
119
125size_t vprint_fmt(char **pbegin, char *end, const char *format, va_list ap)
126 format_printf__(3, 0);
127
143LELY_UTIL_PRINT_INLINE size_t print_char(char **pbegin, char *end, int c);
144
164size_t print_utf8(char **pbegin, char *end, char32_t c32);
165
186size_t print_c99_esc(char **pbegin, char *end, char32_t c32);
187
209size_t print_c99_str(char **pbegin, char *end, const char *s, size_t n);
210
211// clang-format off
212#define LELY_UTIL_DEFINE_PRINT(type, suffix, name) \
213 \
226 size_t print_c99_##suffix( \
227 char **pbegin, char *end, type name);
228// clang-format on
229
230LELY_UTIL_DEFINE_PRINT(long, long, l)
231LELY_UTIL_DEFINE_PRINT(unsigned long, ulong, ul)
232LELY_UTIL_DEFINE_PRINT(long long, llong, ll)
233LELY_UTIL_DEFINE_PRINT(unsigned long long, ullong, ul)
234LELY_UTIL_DEFINE_PRINT(float, flt, f)
235LELY_UTIL_DEFINE_PRINT(double, dbl, d)
236LELY_UTIL_DEFINE_PRINT(long double, ldbl, ld)
237
238LELY_UTIL_DEFINE_PRINT(int_least8_t, i8, i8)
239LELY_UTIL_DEFINE_PRINT(int_least16_t, i16, i16)
240LELY_UTIL_DEFINE_PRINT(int_least32_t, i32, i32)
241LELY_UTIL_DEFINE_PRINT(int_least64_t, i64, i64)
242LELY_UTIL_DEFINE_PRINT(uint_least8_t, u8, u8)
243LELY_UTIL_DEFINE_PRINT(uint_least16_t, u16, u16)
244LELY_UTIL_DEFINE_PRINT(uint_least32_t, u32, u32)
245LELY_UTIL_DEFINE_PRINT(uint_least64_t, u64, u64)
246
247#undef LELY_UTIL_DEFINE_PRINT
248
268size_t print_base64(char **pbegin, char *end, const void *ptr, size_t n);
269
270LELY_UTIL_PRINT_INLINE int
271otoc(int i)
272{
273 return '0' + (i & 7);
274}
275
276LELY_UTIL_PRINT_INLINE int
277xtoc(int i)
278{
279 i &= 0xf;
280 if (i < 10)
281 return '0' + i;
282#if __STDC_ISO_10646__ && !__STDC_MB_MIGHT_NEQ_WC__
283 return 'a' + i - 10;
284#else
285 switch (i) {
286 case 0xa: return 'a';
287 case 0xb: return 'b';
288 case 0xc: return 'c';
289 case 0xd: return 'd';
290 case 0xe: return 'e';
291 case 0xf: return 'f';
292 default: return -1;
293 }
294#endif
295}
296
297LELY_UTIL_PRINT_INLINE size_t
298print_char(char **pbegin, char *end, int c)
299{
300 if (pbegin && *pbegin && (!end || *pbegin < end))
301 *(*pbegin)++ = (unsigned char)c;
302 return 1;
303}
304
305#ifdef __cplusplus
306}
307#endif
308
309#endif // !LELY_UTIL_PRINT_H_
This is the public header file of the utilities library.
size_t print_c99_str(char **pbegin, char *end, const char *s, size_t n)
Prints a UTF-8 encoded Unicode string to a memory buffer.
Definition: print.c:193
size_t vprint_fmt(char **pbegin, char *end, const char *format, va_list ap)
Prints a formatted string to a memory buffer.
Definition: print.c:53
size_t print_char(char **pbegin, char *end, int c)
Prints a single character to a memory buffer.
Definition: print.h:286
int xtoc(int i)
Returns the character corresponding to the hexadecimal digit i.
Definition: print.h:265
size_t print_c99_esc(char **pbegin, char *end, char32_t c32)
Prints a UTF-8 encoded Unicode character to a memory buffer.
Definition: print.c:109
size_t print_base64(char **pbegin, char *end, const void *ptr, size_t n)
Prints the Base64 representation of binary data to a memory buffer.
Definition: print.c:262
size_t print_utf8(char **pbegin, char *end, char32_t c32)
Prints a UTF-8 encoded Unicode character to a memory buffer.
Definition: print.c:87
int otoc(int i)
Returns the character corresponding to the octal digit i.
Definition: print.h:259
size_t print_fmt(char **pbegin, char *end, const char *format,...)
Prints a formatted string to a memory buffer.
Definition: print.c:43
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
This header file is part of the C11 and POSIX compatibility library; it includes <uchar....