changeset 208:3635415a2d03

More work on th_ioctx stuff and wrappers.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Feb 2016 02:49:21 +0200
parents 75dbac8f2f59
children 462b837ea492
files th_ioctx.c th_ioctx.h
diffstat 2 files changed, 62 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/th_ioctx.c	Fri Feb 12 02:47:11 2016 +0200
+++ b/th_ioctx.c	Fri Feb 12 02:49:21 2016 +0200
@@ -231,6 +231,43 @@
 }
 
 
+int thfputs(const char *ptr, th_ioctx *ctx)
+{
+    if (ctx->fops->fputs != NULL)
+        return ctx->fops->fputs(ptr, ctx);
+
+    const char *p = ptr;
+    int retv = 0;
+    while (*p && (retv = ctx->fops->fputc(*p, ctx)) != EOF) p++;
+    return retv;
+}
+
+
+int thvfprintf(th_ioctx *ctx, const char *fmt, va_list ap)
+{
+    if (ctx->fops->vfprintf != NULL)
+        return ctx->fops->vfprintf(ctx, fmt, ap);
+    else
+    {
+        char *msg = th_strdup_printf(fmt, ap);
+        int rval = thfputs(msg, ctx);
+        th_free(msg);
+        return rval;
+    }
+}
+
+
+int thfprintf(th_ioctx *ctx, const char *fmt, ...)
+{
+    int retv;
+    va_list ap;
+    va_start(ap, fmt);
+    retv = thvfprintf(ctx, fmt, ap);
+    va_end(ap);
+    return retv;
+}
+
+
 BOOL thfread_str(th_ioctx *ctx, void *ptr, const size_t len)
 {
     return (thfread(ptr, sizeof(uint8_t), len, ctx) == len);
@@ -408,9 +445,25 @@
 }
 
 
-static int th_stdio_vfprintf(th_ioctx *ctx, const char *format, va_list ap)
+static char * th_stdio_fgets(char *str, int size, th_ioctx *ctx)
+{
+    char *ret = fgets(str, size, CTX_FH);
+    ctx->errno = th_get_error();
+    return ret;
+}
+
+
+static int th_stdio_fputs(const char *str, th_ioctx *ctx)
 {
-    int ret = vfprintf(CTX_FH, format, ap);
+    int ret = fputs(str, CTX_FH);
+    ctx->errno = th_get_error();
+    return ret;
+}
+
+
+static int th_stdio_vfprintf(th_ioctx *ctx, const char *fmt, va_list ap)
+{
+    int ret = vfprintf(CTX_FH, fmt, ap);
     ctx->errno = th_get_error();
     return ret;
 }
@@ -434,6 +487,8 @@
     th_stdio_fread,
     th_stdio_fwrite,
 
+    th_stdio_fgets,
+    th_stdio_fputs,
     th_stdio_vfprintf,
 };
 
--- a/th_ioctx.h	Fri Feb 12 02:47:11 2016 +0200
+++ b/th_ioctx.h	Fri Feb 12 02:49:21 2016 +0200
@@ -57,7 +57,10 @@
     int     (*fputc)(int, th_ioctx *ctx);
     size_t  (*fread)(void *ptr, const size_t, const size_t, th_ioctx *ctx);
     size_t  (*fwrite)(const void *ptr, const size_t, const size_t, th_ioctx *ctx);
-    int     (*vfprintf)(th_ioctx *ctx, const char *format, va_list ap);
+
+    char *  (*fgets)(char *str, int size, th_ioctx *ctx);
+    int     (*fputs)(const char *str, th_ioctx *ctx);
+    int     (*vfprintf)(th_ioctx *ctx, const char *fmt, va_list ap);
 
 } th_ioctx_ops;
 
@@ -102,7 +105,7 @@
 size_t       thfread(void *ptr, const size_t, const size_t, th_ioctx *ctx);
 size_t       thfwrite(const void *, const size_t, const size_t, th_ioctx *ctx);
 char *       thfgets(char *ptr, int size, th_ioctx *ctx);
-
+int          thfputs(const char *ptr, th_ioctx *ctx);
 int          thvfprintf(th_ioctx *ctx, const char *fmt, va_list ap);
 int          thfprintf(th_ioctx *ctx, const char *fmt, ...);