annotate th_printf.c @ 704:b6b8e7249666

Check that the buffer length is >= 1 before trying to access buf[len - 1]
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 26 Apr 2020 23:34:21 +0300
parents 284d5b789b7c
children 4ca6a3b30fe8
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 /*
661
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
2 * A simple and incomplete printf() implementation
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
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
661
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
105 #define TH_VPRINTF_INTFMT_NAME th_vprintf_buf_int
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
106 #define TH_VPRINTF_INTFMT_TYPE_S int
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
107 #define TH_VPRINTF_INTFMT_TYPE_U unsigned int
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
661
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
111 #define TH_VPRINTF_INTFMT_NAME th_vprintf_buf_int64
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
112 #define TH_VPRINTF_INTFMT_TYPE_S int64_t
7f1efa37288b Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents: 595
diff changeset
113 #define TH_VPRINTF_INTFMT_TYPE_U uint64_t
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
704
b6b8e7249666 Check that the buffer length is >= 1 before trying to access buf[len - 1]
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
124 if (len < 1 || buf[len - 1] != '0')
548
41efdd004c3a A somewhat kludgy fix for the octal format issues, now all current
Matti Hamalainen <ccr@tnsp.org>
parents: 547
diff changeset
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,
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
154 th_char_t *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;
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
158 th_char_t f_sign, *f_altstr;
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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 {
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
257 th_char_t buf[32];
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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,
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
280 const th_char_t *str, int f_flags, const int f_width, const int f_prec)
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 {
595
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
282 int nspacepad, f_len, ret;
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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)
595
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
286 {
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
287 // If the "(null)" string does not fit precision, return empty
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
288 if (f_prec >= 0 && f_prec < 6)
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
289 return 0;
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
290
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
291 str = "(null)";
595
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
292 }
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
595
308531ac74e7 Fix internal printf() for case of NULL string pointer, but with format
Matti Hamalainen <ccr@tnsp.org>
parents: 569
diff changeset
294 // Compute padding etc
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 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
296 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
297 f_len = f_prec;
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
299 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
300
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
301 // Do prefix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
302 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
303 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
304
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 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
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 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
308 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
309 }
f43c961cc85d Move 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
535
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
311 // Do postfix padding, if any
ab23d3b3e749 Change few variable names to be more descriptive.
Matti Hamalainen <ccr@tnsp.org>
parents: 533
diff changeset
312 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
313 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315
533
ef0d732fabe0 Disable floating point printf implementation tests as the support is not
Matti Hamalainen <ccr@tnsp.org>
parents: 531
diff changeset
316 #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
317 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
318 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
319 {
f43c961cc85d Move 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 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
321 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
322
f43c961cc85d Move 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 // 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
324 // 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
325 // 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
326 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
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 // 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
329 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
330 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
331 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
332
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 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
334 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 #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
336
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337
567
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
338 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
339 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
340 int f_width, int f_prec, int f_flags,
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
341 const th_char_t fmt, va_list ap)
567
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
342 {
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
343 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
344
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
345 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
346 {
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
347 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
348 // 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
349 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
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 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
352 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
353 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
354
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
355 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
356 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
357
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
358 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
359
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
360 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
361 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
362
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
363 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
364 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
365 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
366 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
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 'x':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
369 case 'X':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
370 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
371 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
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 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
374
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
375 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
376 // 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
377 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
378 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
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 #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
381 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
382 #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
383 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
384 #endif
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
385 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
386
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
387 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
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 #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
390 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
391 case 'F':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
392 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
393 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
394 #endif
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
395
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
396 case 's':
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
397 return th_vprintf_put_str(ctx, vputch, va_arg(ap, th_char_t *),
567
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
398 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
399
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
400 //case '%':
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
401 default:
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
402 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
403 }
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
404 }
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
405
b75f42ca08ef Massage th_vprintf_do() a bit, and factor out the format specifier handling
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
406
663
284d5b789b7c Add th_char_t type.
Matti Hamalainen <ccr@tnsp.org>
parents: 661
diff changeset
407 int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const th_char_t *fmt, va_list ap)
531
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 {
f43c961cc85d Move 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 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
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 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
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 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
414 {
f43c961cc85d Move 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 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
416 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
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 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
419 {
f43c961cc85d Move 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 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
421 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
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 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
424
f43c961cc85d Move 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 // 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
426 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
427 {
f43c961cc85d Move 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 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
429 {
f43c961cc85d Move 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 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
431 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
432 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
433
f43c961cc85d Move 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 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
435 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
436 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
437
f43c961cc85d Move 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 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
439 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
440 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
441
f43c961cc85d Move 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 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
443 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
444 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
445
f43c961cc85d Move 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 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
447 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
448 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
449
f43c961cc85d Move 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 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
451 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
452 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
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 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
455 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
456 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
457 }
f43c961cc85d Move 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 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
459 }
f43c961cc85d Move 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 // 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
462 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
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 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
465 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
466 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
467 {
f43c961cc85d Move 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 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
469 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
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 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
473 {
f43c961cc85d Move 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 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
475 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
476 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
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
f43c961cc85d Move 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 // 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
480 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
481 {
f43c961cc85d Move 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 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
483 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
484 {
f43c961cc85d Move 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 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 = 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
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 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
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 // 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
491 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
492 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
493 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
494 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 // 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
499 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
500 {
f43c961cc85d Move 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 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
502 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
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 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
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 }
f43c961cc85d Move 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 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
508 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
509 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
510
f43c961cc85d Move 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 '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
512 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
513 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
514 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
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 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
517 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
518 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
519 case 't':
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
520 // 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
521 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
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
568
2fbe42d957c4 Actually, partially revert the previous commit and unbreak the API. Leave
Matti Hamalainen <ccr@tnsp.org>
parents: 567
diff changeset
524 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
525 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
526 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
527 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
528 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
529
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 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
532 }
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533
f43c961cc85d Move bulk of the printf() implementation into a separate file th_printf.c from th_string.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 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
535 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
536 }