annotate th_printf1.c @ 457:85fa3d333556

Actually, revert the boolean changes .. meh.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Jan 2018 23:09:29 +0200
parents 347bfd3e017e
children e4ce60239d16
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 /*
415
4b1b2e9d073f Update file description.
Matti Hamalainen <ccr@tnsp.org>
parents: 361
diff changeset
2 * A printf() implementation helper function template
310
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
454
347bfd3e017e Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
4 * (C) Copyright 2016-2018 Tecnic Software productions (TNSP)
310
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
451
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
9
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
10 int TH_PFUNC_NAME (char *buf, const int len, int *pos,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
11 TH_PFUNC_TYPE_S pval, const int f_radix, const BOOL f_upcase,
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
12 const BOOL f_unsig, BOOL *f_neg)
451
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
13 #ifdef TH_PFUNC_HEADER
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
14 ;
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
15 #else
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 {
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
17 if (f_radix > 16)
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 return EOF;
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 // Check for negative value
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
21 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
22 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
23 *f_neg = TRUE;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 pval = -pval;
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 }
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
26 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
27 *f_neg = FALSE;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 // 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
30 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
31
361
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
32 // Special case for value of 0
ad9719373fe3 Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents: 336
diff changeset
33 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
34 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
35
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
36 *pos = 0;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 do
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 {
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
39 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
40 if (digit < 10)
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
41 buf[*pos] = '0' + digit;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 else
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
43 buf[*pos] = (f_upcase ? 'A' : 'a') + digit - 10;
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
44 val /= f_radix;
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
45 (*pos)++;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 }
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
47 while (val > 0 && *pos < len - 1);
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
48 buf[*pos] = 0;
310
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
336
cda5a2aebbb6 Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
50 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
51 }
451
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
52 #endif
310
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
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 #undef TH_PFUNC_NAME
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 #undef TH_PFUNC_SIGNED
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 #undef TH_PFUNC_TYPE_S
11cba47777ec Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 #undef TH_PFUNC_TYPE_U
451
db45d6d2e576 Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
59 #undef TH_PFUNC_HEADER