diff th_ioctx.c @ 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 c2193323736d
children 462b837ea492
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,
 };