annotate th_printf1.c @ 320:0362ea9872f0

Some work on sign handling.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Feb 2016 01:42:34 +0200
parents f8c385739571
children 40ce4106f4ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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)
320
0362ea9872f0 Some work on sign handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 318
diff changeset
62 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
63 #endif
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 // Output the data
320
0362ea9872f0 Some work on sign handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 318
diff changeset
66 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
67 }
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
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 #undef TH_PFUNC_NAME
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 #undef TH_PFUNC_SIGNED
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 #undef TH_PFUNC_TYPE_S
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 #undef TH_PFUNC_TYPE_U