chiark / gitweb /
Various manual fixes.
[mLib] / dputf.c
diff --git a/dputf.c b/dputf.c
index 9c827bc20d9d95e8d29bac713569032daec73551..7ca969a2949acbb851f08332db4140d58bbe2bee 100644 (file)
--- a/dputf.c
+++ b/dputf.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: dputf.c,v 1.1 1999/10/04 21:44:47 mdw Exp $
+ * $Id$
  *
  * `printf'-style formatting for dynamic strings
  *
  *
  * `printf'-style formatting for dynamic strings
  *
  * MA 02111-1307, USA.
  */
 
  * 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 <ctype.h>
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
-#include <float.h>
 #include <math.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <math.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef HAVE_FLOAT_H
+#  include <float.h>
+#endif
+
 #include "dstr.h"
 
 /*----- Tunable constants -------------------------------------------------*/
 #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
  *
  * 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.
  *
  *
  * Returns:    The number of characters written to the string.
  *
@@ -70,7 +65,7 @@
  *             supplied functions with @printf@-style interfaces.
  */
 
  *             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;
 {
   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;
 
     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 --- */
 
 
     /* --- Most stuff gets passed on through --- */
 
@@ -138,6 +131,7 @@ int dstr_vputf(dstr *d, const char *p, va_list ap)
          DPUTC(&dd, '.');
          ip = &prec;
          f |= f_prec;
          DPUTC(&dd, '.');
          ip = &prec;
          f |= f_prec;
+         p++;
          goto getnum;
        case '*':
          ip = &wd;
          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 == '*') {
         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 {
            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,
          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,
          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':
          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') {
          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 (mx > sz)
              sz = mx;
          }
-         if ((f & f_prec) == 0)
+         if (!(f & f_prec))
            prec = 6;
            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,
            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,
          else
            d->len += sprintf(d->buf + d->len, dd.buf,
-                             va_arg(ap, double));
+                             va_arg(*ap, double));
          goto formatted;
          goto formatted;
+#else
+         DPUTS(d, "<no float support>");
+#endif
 
        case 'c':
          DPUTC(&dd, *p);
 
        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,
            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': {
          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);
          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,
            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)
          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)
          else if (f & f_short)
-           *va_arg(ap, short *) = (short)(d->len - n);
+           *va_arg(*ap, short *) = (short)(d->len - n);
          else
          else
-           *va_arg(ap, int *) = (int)(d->len - n);
+           *va_arg(*ap, int *) = (int)(d->len - n);
          goto formatted;
 
        /* --- Other random stuff --- */
          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;
   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);
   }
 
   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);
   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);
 }
   va_end(ap);
   return (n);
 }