Mercurial > hg > th-libs
diff 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 |
line wrap: on
line diff
--- a/th_printf1.c Tue Feb 23 14:59:07 2016 +0200 +++ b/th_printf1.c Tue Feb 23 15:26:29 2016 +0200 @@ -6,69 +6,46 @@ * Please read file 'COPYING' for information on license and distribution. */ -static int TH_PFUNC_NAME (th_printf_ctx *ctx, th_printf_vputch vputch, -#ifdef TH_PFUNC_SIGNED - TH_PFUNC_TYPE_S pval, -#else - TH_PFUNC_TYPE_U val, -#endif - const int radix, const int f_flags, const int f_width, const int f_prec, -#ifdef TH_PFUNC_SIGNED - const BOOL unsig, -#endif - const BOOL upcase) +static int TH_PFUNC_NAME (char *buf, const int len, int *pos, + TH_PFUNC_TYPE_S pval, const int f_radix, const int f_flags, + const int f_prec, const BOOL f_unsig, BOOL *f_neg) { - char buf[64]; - size_t pos = 0; - char f_sign = 0; -#ifdef TH_PFUNC_SIGNED - BOOL neg = FALSE; -#endif + const BOOL f_upcase = f_flags & TH_PF_UPCASE; - if (radix > 16) + if (f_radix > 16) return EOF; -#ifdef TH_PFUNC_SIGNED // Check for negative value - if (!unsig && pval < 0) + if (!f_unsig && pval < 0) { - neg = TRUE; + *f_neg = TRUE; pval = -pval; } + else + *f_neg = FALSE; // Render the value to a string in buf (reversed) TH_PFUNC_TYPE_U val = pval; -#endif // Special case for value of 0 and precision 0 if (val == 0 && f_prec == 0) return 0; + *pos = 0; do { - TH_PFUNC_TYPE_U digit = val % radix; + TH_PFUNC_TYPE_U digit = val % f_radix; if (digit < 10) - buf[pos] = '0' + digit; + buf[*pos] = '0' + digit; else - buf[pos] = (upcase ? 'A' : 'a') + digit - 10; - val /= radix; - pos++; + buf[*pos] = (f_upcase ? 'A' : 'a') + digit - 10; + val /= f_radix; + (*pos)++; } - while (val > 0 && pos < sizeof(buf) - 1); - buf[pos] = 0; + while (val > 0 && *pos < len - 1); + buf[*pos] = 0; - // Oops, the value did not fit in the buffer! - if (val > 0) - return EOF; - - // Do we want a sign prefix? Not for unsigned values -#ifdef TH_PFUNC_SIGNED - if (!unsig) - f_sign = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0)); -#endif - - // Output the data - return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags, f_sign); + return (val > 0) ? EOF : 1; }