Mercurial > hg > th-libs
annotate th_printf.c @ 765:c9a6fe116453
Invert some preprocessor conditions for clarity.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 10 Feb 2023 01:03:22 +0200 |
parents | 31bc1ed07cf5 |
children |
rev | line source |
---|---|
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
2 * A simple and incomplete printf() implementation |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
726 | 4 * (C) Copyright 2016-2022 Tecnic Software productions (TNSP) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #ifdef TH_PRINTF_DEBUG |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
11 bool th_printf_debug = false; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 char *th_printf_debug_prefix = NULL; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 static void pflag(char *buf, const char *str, const int sep, const int flags, const int flg) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 strcat(buf, (flags & flg) ? str : " "); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 if (sep) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 strcat(buf, "|"); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 static const char *get_flags(const int flags) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 static char buf[256]; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 buf[0] = 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 pflag(buf, "ALT", 1, flags, TH_PF_ALT); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 pflag(buf, "SGN", 1, flags, TH_PF_SIGN); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 pflag(buf, "SPC", 1, flags, TH_PF_SPACE); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 pflag(buf, "GRP", 1, flags, TH_PF_GROUP); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 pflag(buf, "ZER", 1, flags, TH_PF_ZERO); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 pflag(buf, "LFT", 0, flags, TH_PF_LEFT); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 return buf; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 #define PP_LINE(...) \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 do { \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 if (th_printf_debug) { \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 if (th_printf_debug_prefix != NULL) \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 fputs(th_printf_debug_prefix, stdout); \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 fprintf(stdout, __VA_ARGS__); \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 } \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 } while (0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 #define PP_PRINTF(...) \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 do { \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 if (th_printf_debug) { \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 fprintf(stdout, __VA_ARGS__); \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 } \ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 } while (0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 #else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 #define PP_LINE(...) /* stub */ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 #define PP_PRINTF(...) /* stub */ |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 #endif |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 static int th_vprintf_put_pstr(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *str) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 while (*str) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 int ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 if ((ret = vputch(ctx, *str++)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 static int th_vprintf_put_repch(th_vprintf_ctx *ctx, th_vprintf_putch vputch, int count, const char ch) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 while (count-- > 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 int ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 if ((ret = vputch(ctx, ch)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 static int th_printf_pad_pre(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 int f_width, const int f_flags) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 if (f_width > 0 && (f_flags & TH_PF_LEFT) == 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 return th_vprintf_put_repch(ctx, vputch, f_width, ' '); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 static int th_printf_pad_post(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 int f_width, const int f_flags) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 if (f_width > 0 && (f_flags & TH_PF_LEFT)) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 return th_vprintf_put_repch(ctx, vputch, f_width, ' '); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 |
765
c9a6fe116453
Invert some preprocessor conditions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
105 #define TH_VPRINTF_INTFMT_IMPL 1 |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
106 #define TH_VPRINTF_INTFMT_NAME th_vprintf_buf_int |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
107 #define TH_VPRINTF_INTFMT_TYPE_S int |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
108 #define TH_VPRINTF_INTFMT_TYPE_U unsigned int |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 #include "th_printf1.c" |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
112 #define TH_VPRINTF_INTFMT_NAME th_vprintf_buf_int64 |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
113 #define TH_VPRINTF_INTFMT_TYPE_S int64_t |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
595
diff
changeset
|
114 #define TH_VPRINTF_INTFMT_TYPE_U uint64_t |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 #include "th_printf1.c" |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 |
546
edfe8ac12408
Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents:
545
diff
changeset
|
118 TH_VPRINTF_ALTFMT_FUNC(th_vprintf_altfmt_oct) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 (void) vret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 (void) flags; |
547
c21051972015
Add int *prec and change int flags to int *flags in th_vprintf_altfmt_func
Matti Hamalainen <ccr@tnsp.org>
parents:
546
diff
changeset
|
122 (void) prec; |
541
a5fb0d95a51f
Remove useless debug print.
Matti Hamalainen <ccr@tnsp.org>
parents:
539
diff
changeset
|
123 |
548
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
124 // This is not very nice |
704
b6b8e7249666
Check that the buffer length is >= 1 before trying to access buf[len - 1]
Matti Hamalainen <ccr@tnsp.org>
parents:
663
diff
changeset
|
125 if (len < 1 || buf[len - 1] != '0') |
548
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
126 { |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
127 if (*prec > 0) |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
128 (*prec)--; |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
129 |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
130 *outlen = 1; |
548
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
131 return "0"; |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
132 } |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
133 else |
41efdd004c3a
A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents:
547
diff
changeset
|
134 return NULL; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 |
546
edfe8ac12408
Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents:
545
diff
changeset
|
138 TH_VPRINTF_ALTFMT_FUNC(th_vprintf_altfmt_hex) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 (void) buf; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 (void) len; |
547
c21051972015
Add int *prec and change int flags to int *flags in th_vprintf_altfmt_func
Matti Hamalainen <ccr@tnsp.org>
parents:
546
diff
changeset
|
142 (void) prec; |
536 | 143 |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 if (vret != 0) |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
145 { |
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
146 *outlen = 2; |
547
c21051972015
Add int *prec and change int flags to int *flags in th_vprintf_altfmt_func
Matti Hamalainen <ccr@tnsp.org>
parents:
546
diff
changeset
|
147 return (*flags & TH_PF_UPCASE) ? "0X" : "0x"; |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
148 } |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 else |
542 | 150 return NULL; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 int th_vprintf_put_int_format(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
663 | 155 th_char_t *buf, int f_flags, int f_width, int f_prec, int f_len, int vret, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
156 bool f_neg, bool f_unsig, th_vprintf_altfmt_func f_alt) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 { |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
158 int ret = 0, nspacepad, nzeropad, f_altstr_len = 0; |
663 | 159 th_char_t f_sign, *f_altstr; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 // Special case for value of 0 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 if (vret == 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 { |
545 | 164 // For NULL pointers, use "(nil)" |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 if (f_flags & TH_PF_POINTER) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 strcpy(buf, ")lin("); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 f_len = 5; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 if (f_prec != 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 buf[f_len++] = '0'; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 buf[f_len] = 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 // Get alternative format string, if needed and available |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
179 f_altstr = (f_flags & TH_PF_ALT) && f_alt != NULL ? |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
180 f_alt(buf, f_len, vret, &f_prec, &f_flags, &f_altstr_len) : NULL; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 // Are we using a sign prefix? |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 f_sign = f_unsig ? 0 : ((f_flags & TH_PF_SIGN) ? |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 (f_neg ? '-' : '+') : |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 (f_neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0))); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
187 // Check for left padding, negates zero prefix padding |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
188 if (f_flags & TH_PF_LEFT) |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
189 f_flags &= ~TH_PF_ZERO; |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
190 |
545 | 191 |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 // Calculate necessary padding, etc |
545 | 193 // |
194 // XXX TODO FIXME: The logic here is not very elegant | |
195 // and would benefit from commenting on how it works. | |
196 // Also same goes for the th_vprintf_altfmt_* logic. | |
197 // | |
569
ccff3eb8dbb6
Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents:
568
diff
changeset
|
198 int nlen = (f_sign ? 1 : 0) + f_altstr_len; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 int qlen = (f_prec > f_len ? f_prec : f_len) + nlen; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 if (f_flags & TH_PF_POINTER && vret == 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 PP_LINE("^"); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 qlen = f_len + nlen; |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
205 nspacepad = f_width > qlen ? f_width - qlen : 0; |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
206 nzeropad = 0; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 if ((f_flags & TH_PF_ZERO) && f_prec < 0 && f_width > 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 PP_LINE("#"); |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
212 nzeropad = f_width - qlen; |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
213 nspacepad = 0; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 PP_LINE("$"); |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
218 nzeropad = (f_prec >= 0) ? f_prec - f_len : 0; |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
219 nspacepad = (f_width >= 0) ? f_width - qlen : 0; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
222 PP_PRINTF(": vret=%3d, f_flags=[%s], f_unsig=%d, f_sign='%c', f_len=%3d, f_width=%3d, f_prec=%3d, nspacepad=%3d, nzeropad=%3d, qlen=%3d\n", |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
223 vret, get_flags(f_flags), f_unsig, f_sign ? f_sign : '?', f_len, f_width, f_prec, nspacepad, nzeropad, qlen); |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
226 // Do prefix padding, if any |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
227 if ((ret = th_printf_pad_pre(ctx, vputch, nspacepad, f_flags)) == EOF) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 // Sign prefix |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 if (f_sign && (ret = vputch(ctx, f_sign)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 // Alternative format string |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 if (f_altstr && (ret = th_vprintf_put_pstr(ctx, vputch, f_altstr)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 // Zero padding |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
239 if (nzeropad > 0 && (ret = th_vprintf_put_repch(ctx, vputch, nzeropad, '0')) == EOF) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 // Output the value |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 while (f_len-- > 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 if ((ret = vputch(ctx, buf[f_len])) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
249 // Do postfix padding, if any |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
250 return th_printf_pad_post(ctx, vputch, nspacepad, f_flags); |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 int th_vprintf_put_int(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 va_list ap, const int f_radix, int f_flags, int f_width, int f_prec, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
256 const bool f_unsig, th_vprintf_altfmt_func f_alt) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 { |
663 | 258 th_char_t buf[32]; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 int f_len = 0, vret; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
260 bool f_neg = false; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 if (f_flags & TH_PF_LONGLONG) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 vret = th_vprintf_buf_int64(buf, sizeof(buf), &f_len, va_arg(ap, int64_t), |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 f_radix, f_flags & TH_PF_UPCASE, f_unsig, &f_neg); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 vret = th_vprintf_buf_int(buf, sizeof(buf), &f_len, va_arg(ap, unsigned int), |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 f_radix, f_flags & TH_PF_UPCASE, f_unsig, &f_neg); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 if (vret == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 return th_vprintf_put_int_format(ctx, vputch, buf, f_flags, f_width, f_prec, f_len, vret, f_neg, f_unsig, f_alt); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
280 int th_vprintf_put_str(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
663 | 281 const th_char_t *str, int f_flags, const int f_width, const int f_prec) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 { |
595
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
283 int nspacepad, f_len, ret; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 // Check for null strings |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 if (str == NULL) |
595
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
287 { |
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
288 // If the "(null)" string does not fit precision, return empty |
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
289 if (f_prec >= 0 && f_prec < 6) |
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
290 return 0; |
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
291 |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 str = "(null)"; |
595
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
293 } |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 |
595
308531ac74e7
Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents:
569
diff
changeset
|
295 // Compute padding etc |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 f_len = strlen(str); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 if (f_prec >= 0 && f_len > f_prec) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 f_len = f_prec; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
300 nspacepad = f_width - f_len; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
302 // Do prefix padding, if any |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
303 if ((ret = th_printf_pad_pre(ctx, vputch, nspacepad, f_flags)) == EOF) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 while (*str && f_len--) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 if ((ret = vputch(ctx, *str++)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 return ret; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 |
535
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
312 // Do postfix padding, if any |
ab23d3b3e749
Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents:
533
diff
changeset
|
313 return th_printf_pad_post(ctx, vputch, nspacepad, f_flags); |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 |
533
ef0d732fabe0
Disable floating point printf implementation tests as the support is not
Matti Hamalainen <ccr@tnsp.org>
parents:
531
diff
changeset
|
317 #ifdef TH_WIP_FLOAT_SUPPORT |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 int th_vprintf_put_float(th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
319 va_list ap, int f_flags, int f_width, int f_prec) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 double pval = va_arg(ap, double); // This needs to be double for type promotion to occur |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 int64_t val; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 // This is a hack, trying to avoid dereferencing a type-punned |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 // pointer and all that stuff related to strict aliasing (although |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 // double and int64_t should be same size and have same aliasing rules.) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 memcpy(&val, &pval, sizeof(int64_t)); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 // We have sign, exponent and mantissa |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
330 bool f_sign = (val >> 63) & 0x01; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 int64_t d_exp = (val >> 52) & 0x7ff; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
332 uint64_t d_man = val & 0x0fffffffffffff; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 return 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
336 #endif |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
339 int th_vprintf_do_format( |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
340 th_vprintf_ctx *ctx, th_vprintf_putch vputch, |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
341 int f_width, int f_prec, int f_flags, |
663 | 342 const th_char_t fmt, va_list ap) |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
343 { |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
344 int ret; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
345 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
346 switch (fmt) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
347 { |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
348 case 0: |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
349 // Unexpected end of format string |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
350 return -104; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
351 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
352 case 'c': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
353 if ((ret = th_printf_pad_pre(ctx, vputch, f_width - 1, f_flags)) == EOF) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
354 return ret; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
355 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
356 if ((ret = vputch(ctx, va_arg(ap, int))) == EOF) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
357 return ret; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
358 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
359 return th_printf_pad_post(ctx, vputch, f_width - 1, f_flags); |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
360 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
361 case 'o': |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
362 return th_vprintf_put_int(ctx, vputch, ap, 8, f_flags, f_width, f_prec, true, th_vprintf_altfmt_oct); |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
363 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
364 case 'u': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
365 case 'i': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
366 case 'd': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
367 return th_vprintf_put_int(ctx, vputch, ap, 10, f_flags, f_width, f_prec, fmt == 'u', NULL); |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
368 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
369 case 'x': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
370 case 'X': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
371 if (fmt == 'X') |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
372 f_flags |= TH_PF_UPCASE; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
373 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
374 return th_vprintf_put_int(ctx, vputch, ap, 16, f_flags, f_width, f_prec, true, th_vprintf_altfmt_hex); |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
375 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
376 case 'p': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
377 // Check for invalid flags |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
378 if (f_flags & (TH_PF_LONG | TH_PF_LONGLONG)) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
379 return -120; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
380 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
381 #if (TH_PTRSIZE == 32) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
382 f_flags |= TH_PF_LONG; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
383 #elif (TH_PTRSIZE == 64) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
384 f_flags |= TH_PF_LONGLONG; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
385 #endif |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
386 f_flags |= TH_PF_ALT | TH_PF_POINTER; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
387 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
388 return th_vprintf_put_int(ctx, vputch, ap, 16, f_flags, f_width, f_prec, true, th_vprintf_altfmt_hex); |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
389 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
390 #ifdef TH_WIP_FLOAT_SUPPORT |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
391 case 'f': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
392 case 'F': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
393 return th_vprintf_put_float(ctx, vputch, ap, |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
394 f_flags, f_width, f_prec); |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
395 #endif |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
396 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
397 case 's': |
663 | 398 return th_vprintf_put_str(ctx, vputch, va_arg(ap, th_char_t *), |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
399 f_flags, f_width, f_prec); |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
400 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
401 //case '%': |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
402 default: |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
403 return vputch(ctx, fmt); |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
404 } |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
405 } |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
406 |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
407 |
663 | 408 int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const th_char_t *fmt, va_list ap) |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
409 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
410 int ret = 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 while (*fmt) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
413 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
414 if (*fmt != '%') |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
416 if ((ret = vputch(ctx, *fmt)) == EOF) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
417 goto out; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
420 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 int f_width = -1, f_prec = -1, f_flags = 0; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
422 bool end = false; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
425 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
426 // Check for flags |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
427 while (!end) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
428 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
429 switch (*fmt) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
431 case '#': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 f_flags |= TH_PF_ALT; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
435 case '+': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
436 f_flags |= TH_PF_SIGN; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
437 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
439 case '0': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
440 f_flags |= TH_PF_ZERO; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
441 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
442 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 case '-': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 f_flags |= TH_PF_LEFT; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
445 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
446 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
447 case ' ': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 f_flags |= TH_PF_SPACE; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
449 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
450 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
451 case '\'': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 f_flags |= TH_PF_GROUP; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
453 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
454 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
455 default: |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
456 end = true; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
457 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
458 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
459 if (!end) fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
460 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
461 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
462 // Get field width |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
463 if (*fmt == '*') |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
464 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
465 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
466 f_width = va_arg(ap, int); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 if (f_width < 0) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
468 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
469 f_flags |= TH_PF_LEFT; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
470 f_width = -f_width; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
471 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
472 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
473 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
474 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
475 f_width = 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
476 while (th_isdigit(*fmt)) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
477 f_width = f_width * 10 + (*fmt++ - '0'); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
478 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
479 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
480 // Check for field precision |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
481 if (*fmt == '.') |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
482 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
483 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
484 if (*fmt == '*') |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
485 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
486 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
487 f_prec = va_arg(ap, int); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
488 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
489 else |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
490 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
491 // If no digit after '.', precision is to be 0 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
492 f_prec = 0; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 while (th_isdigit(*fmt)) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
494 f_prec = f_prec * 10 + (*fmt++ - '0'); |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
495 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
496 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
497 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
498 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 // Check for length modifiers (only some are supported currently) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
500 switch (*fmt) |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
501 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
502 case 'l': |
725
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
503 fmt++; |
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
504 if (*fmt == 'l') |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
505 { |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 f_flags |= TH_PF_LONGLONG; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
507 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
508 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
509 else |
725
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
510 #if TH_ARCH == 64 |
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
511 f_flags |= TH_PF_LONGLONG; |
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
512 #else |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
513 f_flags |= TH_PF_LONG; |
725
5c8936e326d6
On 64bit 'l' printf specifier is 64bit, on 32bit 'll' is 64bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
722
diff
changeset
|
514 #endif |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
515 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
516 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
517 case 'L': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
518 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
519 f_flags |= TH_PF_LONGLONG; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
520 break; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
521 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
522 case 'h': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
523 case 'j': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
524 case 'z': |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
525 case 't': |
568
2fbe42d957c4
Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents:
567
diff
changeset
|
526 // Unsupported for now |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
527 return -202; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
528 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 |
568
2fbe42d957c4
Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents:
567
diff
changeset
|
530 ret = th_vprintf_do_format(ctx, vputch, f_width, f_prec, f_flags, *fmt, ap); |
567
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
531 if (ret == EOF) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
532 goto out; |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
533 if (ret < 0) |
b75f42ca08ef
Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
534 return ret; |
531
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
535 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
536 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
537 fmt++; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
538 } |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
539 |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 out: |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
541 return ret == EOF ? ret : ctx->ipos; |
f43c961cc85d
Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
542 } |