changeset 252:80ffeb316596

More work on printing.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 16 Feb 2016 20:53:10 +0200
parents 7b76108248e9
children d1b4f4ea4715
files th_string.c th_string.h
diffstat 2 files changed, 16 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Tue Feb 16 20:32:02 2016 +0200
+++ b/th_string.c	Tue Feb 16 20:53:10 2016 +0200
@@ -124,7 +124,7 @@
         return EOF;
 
     // Check for negative value
-    if (val < 0)
+    if (val < 0 && !unsig)
     {
         neg = TRUE;
         val = -val;
@@ -236,7 +236,7 @@
 
 int th_vprintf_do(th_printf_ctx *ctx,
     int (*vputch)(th_printf_ctx *ctx, const char ch),
-    const char *fmt, va_list ap, const BOOL eol)
+    const char *fmt, va_list ap)
 {
     int ret = 0;
 
@@ -349,11 +349,8 @@
         fmt++;
     }
 
-    if (eol)
-        ret = vputch(ctx, 0);
-
 out:
-    return ret == EOF ? ret : ctx->pos - 1;
+    return ret == EOF ? ret : ctx->pos;
 }
 
 
@@ -378,12 +375,21 @@
 int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
 {
 #ifdef TH_USE_INTERNAL_SPRINTF
+    int ret;
     th_printf_ctx ctx;
     ctx.buf = buf;
     ctx.size = size;
     ctx.pos = 0;
 
-    return th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap, TRUE);
+    ret = th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap);
+
+    if (ctx.pos < size)
+        buf[ctx.pos] = 0;
+    else
+    if (size > 0)
+        buf[size - 1] = 0;
+
+    return ret;
 #else
     return vsnprintf(buf, size, fmt, ap);
 #endif
@@ -412,7 +418,7 @@
     ctx.data = (void *) fh;
     ctx.pos = 0;
 
-    return th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap, FALSE);
+    return th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
 #else
     return vfprintf(fh, fmt, ap);
 #endif
@@ -430,7 +436,7 @@
 #ifdef TH_USE_INTERNAL_SPRINTF
     ctx.data = (void *) fh;
     ctx.pos = 0;
-    ret = th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap, FALSE);
+    ret = th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap);
 #else
     ret = fprintf(fh, fmt, ap);
 #endif
--- a/th_string.h	Tue Feb 16 20:32:02 2016 +0200
+++ b/th_string.h	Tue Feb 16 20:53:10 2016 +0200
@@ -68,7 +68,7 @@
 char    *th_strrcasecmp(char *haystack, const char *needle);
 void    th_strip_ctrlchars(char *str);
 
-int     th_vprintf_do(th_printf_ctx *ctx, int (*vputch)(th_printf_ctx *ctx, const char ch), const char *fmt, va_list ap, const BOOL eol);
+int     th_vprintf_do(th_printf_ctx *ctx, int (*vputch)(th_printf_ctx *ctx, const char ch), const char *fmt, va_list ap);
 int     th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap);
 int     th_snprintf(char *buf, size_t size, const char *fmt, ...);
 int     th_vfprintf(FILE *fh, const char *fmt, va_list ap);