X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/002eaee36ed12caf6cf7f2a7188111240a427d80..3bc429127d05ea3c84e3c151d53ad3546bea5e9b:/dputf.c diff --git a/dputf.c b/dputf.c index 9c827bc..7ca969a 100644 --- a/dputf.c +++ b/dputf.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dputf.c,v 1.1 1999/10/04 21:44:47 mdw Exp $ + * $Id$ * * `printf'-style formatting for dynamic strings * @@ -27,24 +27,19 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: dputf.c,v $ - * Revision 1.1 1999/10/04 21:44:47 mdw - * Move `dstr_putf' and `dstr_vputf' into a separate source file. - * - */ - /*----- Header files ------------------------------------------------------*/ #include -#include #include #include #include #include #include +#ifdef HAVE_FLOAT_H +# include +#endif + #include "dstr.h" /*----- Tunable constants -------------------------------------------------*/ @@ -62,7 +57,7 @@ * * Arguments: @dstr *d@ = pointer to a dynamic string block * @const char *p@ = pointer to @printf@-style format string - * @va_list ap@ = argument handle + * @va_list *ap@ = argument handle * * Returns: The number of characters written to the string. * @@ -70,7 +65,7 @@ * supplied functions with @printf@-style interfaces. */ -int dstr_vputf(dstr *d, const char *p, va_list ap) +int dstr_vputf(dstr *d, const char *p, va_list *ap) { const char *q = p; size_t n = d->len; @@ -81,13 +76,11 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) unsigned f; int wd, prec; - enum { - f_short = 1, - f_long = 2, - f_Long = 4, - f_wd = 8, - f_prec = 16 - }; +#define f_short 1u +#define f_long 2u +#define f_Long 4u +#define f_wd 8u +#define f_prec 16u /* --- Most stuff gets passed on through --- */ @@ -138,6 +131,7 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) DPUTC(&dd, '.'); ip = ≺ f |= f_prec; + p++; goto getnum; case '*': ip = &wd; @@ -154,7 +148,7 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) getnum: *ip = 0; if (*p == '*') { - *ip = va_arg(ap, int); + *ip = va_arg(*ap, int); DENSURE(&dd, DSTR_PUTFSTEP); dd.len += sprintf(dd.buf + dd.len, "%i", *ip); } else { @@ -181,13 +175,14 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) DENSURE(d, sz); if (f & f_long) d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, unsigned long)); + va_arg(*ap, unsigned long)); else d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, unsigned int)); + va_arg(*ap, unsigned int)); goto formatted; case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': +#ifdef HAVE_FLOAT_H DPUTC(&dd, *p); DPUTZ(&dd); if (*p == 'f') { @@ -195,20 +190,23 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) if (mx > sz) sz = mx; } - if ((f & f_prec) == 0) + if (!(f & f_prec)) prec = 6; - if ((f & f_prec)) + else sz += prec + 16; if ((f & f_wd) && wd + 1 > sz) sz = wd + 1; DENSURE(d, sz); if (f & f_Long) d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, long double)); + va_arg(*ap, long double)); else d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, double)); + va_arg(*ap, double)); goto formatted; +#else + DPUTS(d, ""); +#endif case 'c': DPUTC(&dd, *p); @@ -217,11 +215,11 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) sz = wd + 1; DENSURE(d, sz); d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, unsigned char)); + va_arg(*ap, unsigned)); goto formatted; case 's': { - const char *s = va_arg(ap, const char *); + const char *s = va_arg(*ap, const char *); sz = strlen(s); DPUTC(&dd, *p); DPUTZ(&dd); @@ -243,16 +241,16 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) sz = wd + 1; DENSURE(d, sz); d->len += sprintf(d->buf + d->len, dd.buf, - va_arg(ap, const void *)); + va_arg(*ap, const void *)); goto formatted; case 'n': if (f & f_long) - *va_arg(ap, long *) = (long)(d->len - n); + *va_arg(*ap, long *) = (long)(d->len - n); else if (f & f_short) - *va_arg(ap, short *) = (short)(d->len - n); + *va_arg(*ap, short *) = (short)(d->len - n); else - *va_arg(ap, int *) = (int)(d->len - n); + *va_arg(*ap, int *) = (int)(d->len - n); goto formatted; /* --- Other random stuff --- */ @@ -267,6 +265,12 @@ int dstr_vputf(dstr *d, const char *p, va_list ap) formatted: DRESET(&dd); q = ++p; + +#undef f_short +#undef f_long +#undef f_Long +#undef f_wd +#undef f_prec } DPUTM(d, q, p - q); @@ -295,7 +299,7 @@ int dstr_putf(dstr *d, const char *p, ...) int n; va_list ap; va_start(ap, p); - n = dstr_vputf(d, p, ap); + n = dstr_vputf(d, p, &ap); va_end(ap); return (n); }