Mercurial > hg > th-libs
annotate th_printf1.c @ 540:a3e3b26accd5
Add comment about minimum buffer size requirement.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 30 Dec 2019 15:18:55 +0200 |
parents | 5c9e65b74f8d |
children | 3738d07e20e7 |
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 |
481 | 4 * (C) Copyright 2016-2019 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 |
537
7fd1c51f3b18
Make private function static.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
10 static 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 |
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 { |
336
cda5a2aebbb6
Refactor things to be simpler.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
42 TH_PFUNC_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 |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 #undef TH_PFUNC_NAME |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 #undef TH_PFUNC_SIGNED |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 #undef TH_PFUNC_TYPE_S |
11cba47777ec
Split some things to a template file th_printf1.c
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 #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
|
64 #undef TH_PFUNC_HEADER |