Mercurial > hg > th-libs
annotate th_printf1.c @ 310:11cba47777ec
Split some things to a template file th_printf1.c
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 22 Feb 2016 21:56:23 +0200 |
parents | |
children | 7bce1e9fa397 |
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 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 const int radix, const int f_flags, |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 const int f_width, const BOOL unsig, const BOOL upcase) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 char buf[64]; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 size_t pos = 0; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 int ret = 0; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 BOOL neg = FALSE; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 #else |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 (void) unsig; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 if (radix > 16) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 return EOF; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 // Check for negative value |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 if (!unsig && pval < 0) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 neg = TRUE; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 pval = -pval; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 } |
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 // 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
|
39 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
|
40 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 do |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 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
|
44 if (digit < 10) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 buf[pos] = '0' + digit; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 else |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 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
|
48 val /= radix; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 pos++; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 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
|
52 buf[pos] = 0; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 // 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
|
55 if (val > 0) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 return EOF; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 // 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
|
59 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 if (!unsig) |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 { |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 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
|
63 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
|
64 goto out; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 } |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 // Output the data |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_flags); |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 #ifdef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 out: |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 #endif |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 return ret; |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 } |
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 #undef TH_PFUNC_NAME |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 #undef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 #undef TH_PFUNC_TYPE_S |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 #undef TH_PFUNC_TYPE_U |