Lely core libraries 1.9.2
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#ifdef _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#ifdef _WIN32
52#define LENj LENll
53#else
54#define LENj "j"
55#endif
56#endif
57
58#ifndef LENz
60#ifdef _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#ifdef _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
116size_t print_fmt(char **pbegin, char *end, const char *format, ...)
117 format_printf__(3, 4);
118
124size_t vprint_fmt(char **pbegin, char *end, const char *format, va_list ap)
125 format_printf__(3, 0);
126
141LELY_UTIL_PRINT_INLINE size_t print_char(char **pbegin, char *end, int c);
142
162size_t print_utf8(char **pbegin, char *end, char32_t c32);
163
184size_t print_c99_esc(char **pbegin, char *end, char32_t c32);
185
207size_t print_c99_str(char **pbegin, char *end, const char *s, size_t n);
208
209// clang-format off
210#define LELY_UTIL_DEFINE_PRINT(type, suffix, name) \
211 \
224 size_t print_c99_##suffix( \
225 char **pbegin, char *end, type name);
226// clang-format on
227
228LELY_UTIL_DEFINE_PRINT(long, long, l)
229LELY_UTIL_DEFINE_PRINT(unsigned long, ulong, ul)
230LELY_UTIL_DEFINE_PRINT(long long, llong, ll)
231LELY_UTIL_DEFINE_PRINT(unsigned long long, ullong, ul)
232LELY_UTIL_DEFINE_PRINT(float, flt, f)
233LELY_UTIL_DEFINE_PRINT(double, dbl, d)
234LELY_UTIL_DEFINE_PRINT(long double, ldbl, ld)
235
236LELY_UTIL_DEFINE_PRINT(int_least8_t, i8, i8)
237LELY_UTIL_DEFINE_PRINT(int_least16_t, i16, i16)
238LELY_UTIL_DEFINE_PRINT(int_least32_t, i32, i32)
239LELY_UTIL_DEFINE_PRINT(int_least64_t, i64, i64)
240LELY_UTIL_DEFINE_PRINT(uint_least8_t, u8, u8)
241LELY_UTIL_DEFINE_PRINT(uint_least16_t, u16, u16)
242LELY_UTIL_DEFINE_PRINT(uint_least32_t, u32, u32)
243LELY_UTIL_DEFINE_PRINT(uint_least64_t, u64, u64)
244
245#undef LELY_UTIL_DEFINE_PRINT
246
266size_t print_base64(char **pbegin, char *end, const void *ptr, size_t n);
267
268inline int
269otoc(int i)
270{
271 return '0' + (i & 7);
272}
273
274inline int
275xtoc(int i)
276{
277 i &= 0xf;
278 if (i < 10)
279 return '0' + i;
280#if __STDC_ISO_10646__ && !__STDC_MB_MIGHT_NEQ_WC__
281 return 'a' + i - 10;
282#else
283 switch (i) {
284 case 0xa: return 'a';
285 case 0xb: return 'b';
286 case 0xc: return 'c';
287 case 0xd: return 'd';
288 case 0xe: return 'e';
289 case 0xf: return 'f';
290 default: return -1;
291 }
292#endif
293}
294
295inline size_t
296print_char(char **pbegin, char *end, int c)
297{
298 if (pbegin && *pbegin && (!end || *pbegin < end))
299 *(*pbegin)++ = (unsigned char)c;
300 return 1;
301}
302
303#ifdef __cplusplus
304}
305#endif
306
307#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:190
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:50
size_t print_char(char **pbegin, char *end, int c)
Prints a single character to a memory buffer.
Definition: print.h:284
int xtoc(int i)
Returns the character corresponding to the hexadecimal digit i.
Definition: print.h:263
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:106
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:258
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:84
int otoc(int i)
Returns the character corresponding to the octal digit i.
Definition: print.h:257
size_t print_fmt(char **pbegin, char *end, const char *format,...)
Prints a formatted string to a memory buffer.
Definition: print.c:40
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....