Mercurial > hg > th-libs
annotate th_printf1.c @ 735:31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 12:14:39 +0200 |
parents | 29e44a58bc73 |
children | c9a6fe116453 |
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 | 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 |
726 | 4 * (C) Copyright 2016-2022 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 |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
10 int TH_VPRINTF_INTFMT_NAME (char *buf, const int len, int *pos, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
11 TH_VPRINTF_INTFMT_TYPE_S pval, const int f_radix, const bool f_upcase, |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
12 const bool f_unsig, bool *f_neg) |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
13 #ifdef TH_VPRINTF_INTFMT_HEADER |
451
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 { |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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) |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
30 TH_VPRINTF_INTFMT_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 |
538
5c9e65b74f8d
Add more explanation for the reasoning behind the special casing of 0.
Matti Hamalainen <ccr@tnsp.org>
parents:
537
diff
changeset
|
32 // Special case for value of 0. This would seem like a |
5c9e65b74f8d
Add more explanation for the reasoning behind the special casing of 0.
Matti Hamalainen <ccr@tnsp.org>
parents:
537
diff
changeset
|
33 // dirty kludge, but allows us to handle NULL ptr (nul) |
5c9e65b74f8d
Add more explanation for the reasoning behind the special casing of 0.
Matti Hamalainen <ccr@tnsp.org>
parents:
537
diff
changeset
|
34 // situation a bit better elsewhere. |
361
ad9719373fe3
Simplify th_printf_vbuf*() helpers.
Matti Hamalainen <ccr@tnsp.org>
parents:
336
diff
changeset
|
35 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
|
36 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
|
37 |
540
a3e3b26accd5
Add comment about minimum buffer size requirement.
Matti Hamalainen <ccr@tnsp.org>
parents:
538
diff
changeset
|
38 // We assume that the output buffer is at least 2 bytes long |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
39 *pos = 0; |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 do |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 { |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
42 TH_VPRINTF_INTFMT_TYPE_U digit = val % f_radix; |
540
a3e3b26accd5
Add comment about minimum buffer size requirement.
Matti Hamalainen <ccr@tnsp.org>
parents:
538
diff
changeset
|
43 |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 if (digit < 10) |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
45 buf[*pos] = '0' + digit; |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 else |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
47 buf[*pos] = (f_upcase ? 'A' : 'a') + digit - 10; |
540
a3e3b26accd5
Add comment about minimum buffer size requirement.
Matti Hamalainen <ccr@tnsp.org>
parents:
538
diff
changeset
|
48 |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
49 val /= f_radix; |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
50 (*pos)++; |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 } |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
52 while (val > 0 && *pos < len - 1); |
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
53 buf[*pos] = 0; |
310
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
55 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
|
56 } |
451
db45d6d2e576
Expose some of the internal vprintf() implementation helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
57 #endif |
310
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 |
661
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
60 #undef TH_VPRINTF_INTFMT_NAME |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
61 #undef TH_VPRINTF_INTFMT_SIGNED |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
62 #undef TH_VPRINTF_INTFMT_TYPE_S |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
63 #undef TH_VPRINTF_INTFMT_TYPE_U |
7f1efa37288b
Rename TH_PFUNC_* macros to TH_VPRINTF_INTFMT_*
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
64 #undef TH_VPRINTF_INTFMT_HEADER |