annotate th_printf.c @ 569:ccff3eb8dbb6

Add outlen parameter to th_vprintf_altfmt_funcs.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 08 Jan 2020 02:36:42 +0200
parents 2fbe42d957c4
children 308531ac74e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 /*
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * A printf() implementation
f43c961cc85d Move 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
553
3a852e9f70a6 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 548
diff changeset
4 * (C) Copyright 2016-2020 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
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 BOOL th_printf_debug = FALSE;
f43c961cc85d Move 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
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 #define TH_PFUNC_NAME th_vprintf_buf_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
106 #define TH_PFUNC_TYPE_S 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
107 #define TH_PFUNC_TYPE_U 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
108 #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
109
f43c961cc85d Move 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 #define TH_PFUNC_NAME th_vprintf_buf_int64
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 #define TH_PFUNC_TYPE_S 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
113 #define TH_PFUNC_TYPE_U uint64_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
114 #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
115
f43c961cc85d Move 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
546
edfe8ac12408 Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents: 545
diff changeset
117 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
118 {
f43c961cc85d Move 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 (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
120 (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
121 (void) prec;
541
a5fb0d95a51f Remove useless debug print.
Matti Hamalainen <ccr@tnsp.org>
parents: 539
diff changeset
122
548
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
123 // This is not very nice
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
124 if (buf[len - 1] != '0')
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
125 {
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
126 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
127 (*prec)--;
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
128
569
ccff3eb8dbb6 Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents: 568
diff changeset
129 *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
130 return "0";
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
131 }
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
132 else
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
133 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
134 }
f43c961cc85d Move 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
546
edfe8ac12408 Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents: 545
diff changeset
137 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
138 {
f43c961cc85d Move 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 (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
140 (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
141 (void) prec;
536
173609d71821 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 535
diff changeset
142
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
143 if (vret != 0)
569
ccff3eb8dbb6 Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents: 568
diff changeset
144 {
ccff3eb8dbb6 Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents: 568
diff changeset
145 *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
146 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
147 }
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
148 else
542
bd532780ede8 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 541
diff changeset
149 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
150 }
f43c961cc85d Move 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 int th_vprintf_put_int_format(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
154 char *buf, int f_flags, int f_width, int f_prec, int f_len, int vret,
546
edfe8ac12408 Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents: 545
diff changeset
155 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
156 {
569
ccff3eb8dbb6 Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents: 568
diff changeset
157 int ret = 0, nspacepad, nzeropad, f_altstr_len = 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
158 char f_sign, *f_altstr;
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
f43c961cc85d Move 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 // 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
161 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
162 {
545
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
163 // 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
164 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
165 {
f43c961cc85d Move 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 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
167 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
168 }
f43c961cc85d Move 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 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
170 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
171 {
f43c961cc85d Move 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 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
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 }
f43c961cc85d Move 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 // 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
178 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
179 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
180
f43c961cc85d Move 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 // 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
182 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
183 (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
184 (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
185
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
186 // 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
187 if (f_flags & TH_PF_LEFT)
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
188 f_flags &= ~TH_PF_ZERO;
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
189
545
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
190
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
191 // Calculate necessary padding, etc
545
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
192 //
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
193 // XXX TODO FIXME: The logic here is not very elegant
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
194 // and would benefit from commenting on how it works.
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
195 // Also same goes for the th_vprintf_altfmt_* logic.
4d2e9c806b98 Add few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
196 //
569
ccff3eb8dbb6 Add outlen parameter to th_vprintf_altfmt_funcs.
Matti Hamalainen <ccr@tnsp.org>
parents: 568
diff changeset
197 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
198 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
199
f43c961cc85d Move 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 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
201 {
f43c961cc85d Move 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 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
203 qlen = f_len + nlen;
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
204 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
205 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
206 }
f43c961cc85d Move 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 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
208 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
209 {
f43c961cc85d Move 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 PP_LINE("#");
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
211 nzeropad = f_width - qlen;
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
212 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
213 }
f43c961cc85d Move 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 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
215 {
f43c961cc85d Move 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 PP_LINE("$");
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
217 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
218 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
219 }
f43c961cc85d Move 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
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
221 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
222 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
223
f43c961cc85d Move 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
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
225 // Do prefix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
226 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
227 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
228
f43c961cc85d Move 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 // 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
230 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
231 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
232
f43c961cc85d Move 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 // 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
234 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
235 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
236
f43c961cc85d Move 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 // Zero padding
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
238 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
239 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
240
f43c961cc85d Move 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 // 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
242 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
243 {
f43c961cc85d Move 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 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
245 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
246 }
f43c961cc85d Move 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
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
248 // Do postfix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
249 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
250 }
f43c961cc85d Move 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 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
254 va_list ap, const int f_radix, int f_flags, int f_width, int f_prec,
546
edfe8ac12408 Add function typedef th_vprintf_altfmt_func and #define
Matti Hamalainen <ccr@tnsp.org>
parents: 545
diff changeset
255 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
256 {
539
7615dbdc4d34 The buffer used for th_vprintf_put_int() was about twice as large as required. Make it smaller.
Matti Hamalainen <ccr@tnsp.org>
parents: 536
diff changeset
257 char 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
258 int f_len = 0, 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
259 BOOL f_neg = FALSE;
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
f43c961cc85d Move 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 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
262 {
f43c961cc85d Move 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 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
264 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
265 }
f43c961cc85d Move 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 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
267 {
f43c961cc85d Move 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 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
269 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
270 }
f43c961cc85d Move 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 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
273 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
274
f43c961cc85d Move 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 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
276 }
f43c961cc85d Move 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 int th_vprintf_put_str(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
280 const char *str, int f_flags, const int f_width, const 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
281 {
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
282 int nspacepad, f_len, ret = 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
283
f43c961cc85d Move 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 // 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
285 if (str == 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
286 str = "(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
287
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 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
289 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
290 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
291
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
292 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
293
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
294 // Do prefix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
295 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
296 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
297
f43c961cc85d Move 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 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
299 {
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 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
301 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
302 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
304 // Do postfix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
305 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
306 }
f43c961cc85d Move 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
533
ef0d732fabe0 Disable floating point printf implementation tests as the support is not
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
309 #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
310 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
311 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
312 {
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 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
314 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
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 // 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
317 // 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
318 // 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
319 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
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 // We have sign, exponent and mantissa
f43c961cc85d Move 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 BOOL f_sign = (val >> 63) & 0x01;
f43c961cc85d Move 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 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
324 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
325
f43c961cc85d Move 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 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
327 }
f43c961cc85d Move 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 #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
329
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330
567
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
331 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
332 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
333 int f_width, int f_prec, int 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
334 const char fmt, va_list ap)
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
335 {
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
336 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
337
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
338 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
339 {
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
340 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
341 // 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
342 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
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 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
345 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
346 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
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 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
349 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
350
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
351 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
352
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
353 case 'o':
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 th_vprintf_put_int(ctx, vputch, ap, 8, f_flags, f_width, f_prec, TRUE, th_vprintf_altfmt_oct);
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 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
357 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
358 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
359 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
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 'x':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
362 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
363 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
364 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
365
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
366 return th_vprintf_put_int(ctx, vputch, ap, 16, f_flags, f_width, f_prec, TRUE, th_vprintf_altfmt_hex);
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
367
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
368 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
369 // 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
370 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
371 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
372
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
373 #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
374 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
375 #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
376 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
377 #endif
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
378 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
379
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
380 return th_vprintf_put_int(ctx, vputch, ap, 16, f_flags, f_width, f_prec, TRUE, th_vprintf_altfmt_hex);
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
381
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
382 #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
383 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
384 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
385 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
386 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
387 #endif
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
388
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
389 case 's':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
390 return th_vprintf_put_str(ctx, vputch, va_arg(ap, char *),
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
391 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
392
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
393 //case '%':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
394 default:
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
395 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
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 }
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
398
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
399
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
400 int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *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
401 {
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 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
403
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 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
405 {
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 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
407 {
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 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
409 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
410 }
f43c961cc85d Move 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 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
412 {
f43c961cc85d Move 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 int f_width = -1, f_prec = -1, f_flags = 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
414 BOOL end = FALSE;
f43c961cc85d Move 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 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
417
f43c961cc85d Move 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 // 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
419 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
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 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
422 {
f43c961cc85d Move 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 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
424 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
425 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
426
f43c961cc85d Move 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 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
428 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
429 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
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 '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
432 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
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_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
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 ' ':
f43c961cc85d Move 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_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
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_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
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 default:
f43c961cc85d Move 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 end = TRUE;
f43c961cc85d Move 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 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
452 }
f43c961cc85d Move 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
f43c961cc85d Move 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 // 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
455 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
456 {
f43c961cc85d Move 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 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
458 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
459 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
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 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
462 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
463 }
f43c961cc85d Move 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 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
466 {
f43c961cc85d Move 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 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 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
469 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
470 }
f43c961cc85d Move 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 // 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
473 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
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 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
476 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
477 {
f43c961cc85d Move 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 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
479 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
480 }
f43c961cc85d Move 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 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
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 // 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
484 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
485 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
486 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
487 }
f43c961cc85d Move 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
f43c961cc85d Move 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 // 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
492 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
493 {
f43c961cc85d Move 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 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
495 if (*++fmt == '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
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 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
498 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
499 }
f43c961cc85d Move 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 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
501 f_flags |= TH_PF_LONG;
f43c961cc85d Move 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 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
503
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 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
505 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
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 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
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 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
510 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
511 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
512 case 't':
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
513 // 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
514 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
515 }
f43c961cc85d Move 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
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
517 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
518 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
519 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
520 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
521 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
522
f43c961cc85d Move 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 }
f43c961cc85d Move 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 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
525 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526
f43c961cc85d Move 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 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
528 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
529 }