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
36 #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
50 #if _WIN32
52 #define LENj LENll
53 #else
54 #define LENj "j"
55 #endif
56 #endif
57 
58 #ifndef LENz
59 #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
72 #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
85 extern "C" {
86 #endif
87 
89 LELY_UTIL_PRINT_INLINE int otoc(int i);
90 
96 LELY_UTIL_PRINT_INLINE int xtoc(int i);
97 
117 size_t print_fmt(char **pbegin, char *end, const char *format, ...)
118  format_printf__(3, 4);
119 
125 size_t vprint_fmt(char **pbegin, char *end, const char *format, va_list ap)
126  format_printf__(3, 0);
127 
143 LELY_UTIL_PRINT_INLINE size_t print_char(char **pbegin, char *end, int c);
144 
164 size_t print_utf8(char **pbegin, char *end, char32_t c32);
165 
186 size_t print_c99_esc(char **pbegin, char *end, char32_t c32);
187 
209 size_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 
230 LELY_UTIL_DEFINE_PRINT(long, long, l)
231 LELY_UTIL_DEFINE_PRINT(unsigned long, ulong, ul)
232 LELY_UTIL_DEFINE_PRINT(long long, llong, ll)
233 LELY_UTIL_DEFINE_PRINT(unsigned long long, ullong, ul)
234 LELY_UTIL_DEFINE_PRINT(float, flt, f)
235 LELY_UTIL_DEFINE_PRINT(double, dbl, d)
236 LELY_UTIL_DEFINE_PRINT(long double, ldbl, ld)
237 
238 LELY_UTIL_DEFINE_PRINT(int_least8_t, i8, i8)
239 LELY_UTIL_DEFINE_PRINT(int_least16_t, i16, i16)
240 LELY_UTIL_DEFINE_PRINT(int_least32_t, i32, i32)
241 LELY_UTIL_DEFINE_PRINT(int_least64_t, i64, i64)
242 LELY_UTIL_DEFINE_PRINT(uint_least8_t, u8, u8)
243 LELY_UTIL_DEFINE_PRINT(uint_least16_t, u16, u16)
244 LELY_UTIL_DEFINE_PRINT(uint_least32_t, u32, u32)
245 LELY_UTIL_DEFINE_PRINT(uint_least64_t, u64, u64)
246 
247 #undef LELY_UTIL_DEFINE_PRINT
248 
268 size_t print_base64(char **pbegin, char *end, const void *ptr, size_t n);
269 
270 LELY_UTIL_PRINT_INLINE int
271 otoc(int i)
272 {
273  return '0' + (i & 7);
274 }
275 
276 LELY_UTIL_PRINT_INLINE int
277 xtoc(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 
297 LELY_UTIL_PRINT_INLINE size_t
298 print_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_
util.h
print_base64
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
vprint_fmt
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
otoc
int otoc(int i)
Returns the character corresponding to the octal digit i.
Definition: print.h:259
xtoc
int xtoc(int i)
Returns the character corresponding to the hexadecimal digit i.
Definition: print.h:265
print_char
size_t print_char(char **pbegin, char *end, int c)
Prints a single character to a memory buffer.
Definition: print.h:286
print_fmt
size_t print_fmt(char **pbegin, char *end, const char *format,...)
Prints a formatted string to a memory buffer.
Definition: print.c:43
print_c99_esc
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
print_utf8
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
uchar.h
print_c99_str
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
stddef.h