diff th_string.c @ 310:11cba47777ec

Split some things to a template file th_printf1.c
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 22 Feb 2016 21:56:23 +0200
parents 0311f139fcf6
children d6a9db84d702
line wrap: on
line diff
--- a/th_string.c	Mon Feb 22 21:53:36 2016 +0200
+++ b/th_string.c	Mon Feb 22 21:56:23 2016 +0200
@@ -158,73 +158,45 @@
 }
 
 
-static int th_printf_vput_int(th_printf_ctx *ctx, th_printf_vputch vputch,
-    int pval, const int radix, const int f_flags,
-    const int f_width, const BOOL unsig, const BOOL upcase)
+int th_printf_vput_intstr(th_printf_ctx *ctx, th_printf_vputch vputch,
+    const char *buf, const int f_width, int pos, const int f_flags)
 {
-    char buf[64];
-    size_t pos = 0;
-    BOOL neg = FALSE;
-    int ret = 0;
-
-    if (radix > 16)
-        return EOF;
-
-    // Check for negative value
-    if (!unsig && pval < 0)
-    {
-        neg = TRUE;
-        pval = -pval;
-    }
-
-    // Render the value to a string in buf (reversed)
-    unsigned int val = pval;
-    do
-    {
-        unsigned int digit = val % radix;
-        if (digit < 10)
-            buf[pos] = '0' + digit;
-        else
-            buf[pos] = (upcase ? 'A' : 'a') + digit - 10;
-        val /= radix;
-        pos++;
-    }
-    while (val > 0 && pos < sizeof(buf) - 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
-    if (!unsig)
-    {
-        char ch = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0));
-        if (ch && (ret = vputch(ctx, ch)) == EOF)
-            goto out;
-    }
-
     // Calculate necessary padding, if any
-    int nwidth = f_width - pos;
+    int ret, nwidth = f_width - pos;
 
     // Prefix padding?
     if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
-        goto out;
+        return ret;
 
     // Output the value
     while (pos--)
     {
         if ((ret = vputch(ctx, buf[pos])) == EOF)
-            goto out;
+            return ret;
     }
 
     // Postfix padding?
-    if ((ret = th_printf_pad_post(ctx, vputch, nwidth, f_flags)) == EOF)
-        goto out;
+    return th_printf_pad_post(ctx, vputch, nwidth, f_flags);
+}
+
+
+#define TH_PFUNC_NAME th_printf_vput_int
+#define TH_PFUNC_SIGNED
+#define TH_PFUNC_TYPE_S int
+#define TH_PFUNC_TYPE_U unsigned int
+#include "th_printf1.c"
+
 
-out:
-    return ret;
-}
+#define TH_PFUNC_NAME th_printf_vput_int64
+#define TH_PFUNC_SIGNED
+#define TH_PFUNC_TYPE_S int64_t
+#define TH_PFUNC_TYPE_U uint64_t
+#include "th_printf1.c"
+
+
+#define TH_PFUNC_NAME th_printf_vput_size_t
+#define TH_PFUNC_TYPE_U size_t
+#include "th_printf1.c"
 
 
 static int th_printf_vput_str(th_printf_ctx *ctx, th_printf_vputch vputch,
@@ -375,9 +347,17 @@
                     break;
 
                 case 'p':
-                    if ((ret = th_printf_vput_int(ctx, vputch, va_arg(ap, void *),
+#if (TH_PTRSIZE == 32)
+                    if ((ret = th_printf_vput_int(ctx, vputch, (int32_t) va_arg(ap, void *),
                         16, f_flags, f_width, TRUE, FALSE)) == EOF)
                         goto out;
+#elif (TH_PTRSIZE == 64)
+                    if ((ret = th_printf_vput_int64(ctx, vputch, (int64_t) va_arg(ap, void *),
+                        16, f_flags, f_width, TRUE, FALSE)) == EOF)
+                        goto out;
+#else
+#    error TH_PTRSIZE not defined
+#endif
                     break;
 
                 case 'f':
@@ -522,11 +502,7 @@
         int n;
         va_list ap;
         va_copy(ap, args);
-#ifdef TH_USE_INTERNAL_SPRINTF
-        n = th_vsnprintf(buf, size, fmt, ap);
-#else
         n = vsnprintf(buf, size, fmt, ap);
-#endif
         va_end(ap);
 
         if (n > -1 && n < size)