changeset 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 2c87abec62db
children 31668615083e
files Makefile.gen th_printf1.c th_string.c
diffstat 3 files changed, 118 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Mon Feb 22 21:53:36 2016 +0200
+++ b/Makefile.gen	Mon Feb 22 21:56:23 2016 +0200
@@ -28,6 +28,9 @@
 #
 all: $(NOBUILD_TARGETS) $(NOINST_TARGETS) $(TARGETS) check
 
+th_string.c: th_printf1.c th_string.h
+
+
 $(OBJPATH):
 	$(MKDIR_P) $@
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/th_printf1.c	Mon Feb 22 21:56:23 2016 +0200
@@ -0,0 +1,81 @@
+/*
+ * printf() helper implementation
+ * Programmed and designed by Matti 'ccr' Hamalainen
+ * (C) Copyright 2016 Tecnic Software productions (TNSP)
+ *
+ * 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 BOOL unsig, const BOOL upcase)
+{
+    char buf[64];
+    size_t pos = 0;
+    int ret = 0;
+#ifdef TH_PFUNC_SIGNED
+    BOOL neg = FALSE;
+#else
+    (void) unsig;
+#endif
+
+    if (radix > 16)
+        return EOF;
+
+#ifdef TH_PFUNC_SIGNED
+    // Check for negative value
+    if (!unsig && pval < 0)
+    {
+        neg = TRUE;
+        pval = -pval;
+    }
+
+    // Render the value to a string in buf (reversed)
+    TH_PFUNC_TYPE_U val = pval;
+#endif
+    do
+    {
+        TH_PFUNC_TYPE_U 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
+#ifdef TH_PFUNC_SIGNED
+    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;
+    }
+#endif
+
+    // Output the data
+    return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_flags);
+    
+#ifdef TH_PFUNC_SIGNED
+out:
+#endif
+    return ret;
+}
+
+
+#undef TH_PFUNC_NAME
+#undef TH_PFUNC_SIGNED
+#undef TH_PFUNC_TYPE_S
+#undef TH_PFUNC_TYPE_U
--- 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)