Mercurial > hg > th-libs
annotate th_printf1.c @ 325:40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 23 Feb 2016 12:16:12 +0200 |
parents | 0362ea9872f0 |
children | cda5a2aebbb6 |
rev | line source |
---|---|
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * printf() helper implementation |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2016 Tecnic Software productions (TNSP) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 static int TH_PFUNC_NAME (th_printf_ctx *ctx, th_printf_vputch vputch, |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 TH_PFUNC_TYPE_S pval, |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 #else |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 TH_PFUNC_TYPE_U val, |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 #endif |
314
7bce1e9fa397
Add f_prec arguments to helpers and call points.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
15 const int radix, const int f_flags, const int f_width, const int f_prec, |
318
f8c385739571
No need for unsig argument when TH_PFUNC_SIGNED is not defined.
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
16 #ifdef TH_PFUNC_SIGNED |
f8c385739571
No need for unsig argument when TH_PFUNC_SIGNED is not defined.
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
17 const BOOL unsig, |
f8c385739571
No need for unsig argument when TH_PFUNC_SIGNED is not defined.
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
18 #endif |
f8c385739571
No need for unsig argument when TH_PFUNC_SIGNED is not defined.
Matti Hamalainen <ccr@tnsp.org>
parents:
314
diff
changeset
|
19 const BOOL upcase) |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 char buf[64]; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 size_t pos = 0; |
320
0362ea9872f0
Some work on sign handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
23 char f_sign = 0; |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 BOOL neg = FALSE; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 if (radix > 16) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 return EOF; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 // Check for negative value |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 if (!unsig && pval < 0) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 neg = TRUE; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 pval = -pval; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 // Render the value to a string in buf (reversed) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 TH_PFUNC_TYPE_U val = pval; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 #endif |
325
40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
320
diff
changeset
|
42 |
40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
320
diff
changeset
|
43 // Special case for value of 0 and precision 0 |
40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
320
diff
changeset
|
44 if (val == 0 && f_prec == 0) |
40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
320
diff
changeset
|
45 return 0; |
40ce4106f4ad
Add special case handling for f_prec == 0 && val == 0. Fixes several test cases.
Matti Hamalainen <ccr@tnsp.org>
parents:
320
diff
changeset
|
46 |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 do |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 TH_PFUNC_TYPE_U digit = val % radix; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 if (digit < 10) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 buf[pos] = '0' + digit; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 else |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 buf[pos] = (upcase ? 'A' : 'a') + digit - 10; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 val /= radix; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 pos++; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 while (val > 0 && pos < sizeof(buf) - 1); |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 buf[pos] = 0; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 // Oops, the value did not fit in the buffer! |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 if (val > 0) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 return EOF; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 // Do we want a sign prefix? Not for unsigned values |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 if (!unsig) |
320
0362ea9872f0
Some work on sign handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
67 f_sign = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0)); |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 // Output the data |
320
0362ea9872f0
Some work on sign handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
71 return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags, f_sign); |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 #undef TH_PFUNC_NAME |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 #undef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 #undef TH_PFUNC_TYPE_S |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 #undef TH_PFUNC_TYPE_U |