Mercurial > hg > th-libs
annotate th_printf1.c @ 318:f8c385739571
No need for unsig argument when TH_PFUNC_SIGNED is not defined.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 23 Feb 2016 00:14:13 +0200 |
parents | 7bce1e9fa397 |
children | 0362ea9872f0 |
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; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 int ret = 0; |
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 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 do |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 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
|
45 if (digit < 10) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 buf[pos] = '0' + digit; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 else |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 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
|
49 val /= radix; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 pos++; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 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
|
53 buf[pos] = 0; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 // 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
|
56 if (val > 0) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 return EOF; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 // 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
|
60 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 if (!unsig) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 char ch = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0)); |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 if (ch && (ret = vputch(ctx, ch)) == EOF) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 goto out; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 // Output the data |
314
7bce1e9fa397
Add f_prec arguments to helpers and call points.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
70 return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags); |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 out: |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 return ret; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 #undef TH_PFUNC_NAME |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 #undef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 #undef TH_PFUNC_TYPE_S |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 #undef TH_PFUNC_TYPE_U |