annotate th_printf1.c @ 361:ad9719373fe3

Simplify th_printf_vbuf*() helpers.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Feb 2016 11:53:08 +0200
parents cda5a2aebbb6
children 4b1b2e9d073f
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
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
9 static int TH_PFUNC_NAME (char *buf, const int len, int *pos,
361
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
10 TH_PFUNC_TYPE_S pval, const int f_radix, const BOOL f_upcase,
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
11 const BOOL f_unsig, BOOL *f_neg)
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 {
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
13 if (f_radix > 16)
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 return EOF;
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 // Check for negative value
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
17 if (!f_unsig && pval < 0)
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 {
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
19 *f_neg = TRUE;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 pval = -pval;
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 }
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
22 else
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
23 *f_neg = FALSE;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 // 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
26 TH_PFUNC_TYPE_U val = pval;
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
27
361
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
28 // Special case for value of 0
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
29 if (val == 0)
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
30 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
31
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
32 *pos = 0;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 do
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
35 TH_PFUNC_TYPE_U digit = val % f_radix;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 if (digit < 10)
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
37 buf[*pos] = '0' + digit;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 else
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
39 buf[*pos] = (f_upcase ? 'A' : 'a') + digit - 10;
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
40 val /= f_radix;
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
41 (*pos)++;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 }
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
43 while (val > 0 && *pos < len - 1);
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
44 buf[*pos] = 0;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
46 return (val > 0) ? EOF : 1;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
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
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 #undef TH_PFUNC_NAME
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 #undef TH_PFUNC_SIGNED
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 #undef TH_PFUNC_TYPE_S
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 #undef TH_PFUNC_TYPE_U