annotate th_printf1.c @ 336:cda5a2aebbb6

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