changeset 204:55f429dff750

Add th_io_fopen() helper function.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 11 Feb 2016 23:14:54 +0200
parents 7acdd3ab6900
children c42a9c3f4d55
files th_ioctx.c th_ioctx.h
diffstat 2 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/th_ioctx.c	Thu Feb 11 22:59:30 2016 +0200
+++ b/th_ioctx.c	Thu Feb 11 23:14:54 2016 +0200
@@ -28,9 +28,6 @@
     if (ctx == NULL)
         return THERR_NULLPTR;
 
-    th_free_r(&ctx->filename);
-    th_free_r(&ctx->mode);
-
     ctx->filename = th_strdup(filename);
     ctx->mode = th_strdup(mode);
 
@@ -41,6 +38,22 @@
 }
 
 
+th_ioctx * th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode)
+{
+    th_ioctx *ctx = th_io_new(fops);
+    if (ctx == NULL)
+        return NULL;
+
+    if (th_io_open(ctx, filename, mode) != THERR_OK)
+    {
+        th_io_free(ctx);
+        return NULL;
+    }
+
+    return ctx;
+}
+
+
 void th_io_close(th_ioctx *ctx)
 {
     if (ctx != NULL)
@@ -287,7 +300,7 @@
 {
     ctx->data = (void *) fopen(ctx->filename, ctx->mode);
     ctx->errno = th_get_error();
-    return (CTX_FH != NULL) ? THERR_OK : THERR_FOPEN;
+    return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN;
 }
 
 
--- a/th_ioctx.h	Thu Feb 11 22:59:30 2016 +0200
+++ b/th_ioctx.h	Thu Feb 11 23:14:54 2016 +0200
@@ -74,6 +74,7 @@
 //
 th_ioctx *   th_io_new(const th_ioctx_ops *fops);
 int          th_io_open(th_ioctx *ctx, const char *filename, const char *mode);
+th_ioctx *   th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode);
 void         th_io_close(th_ioctx *ctx);
 void         th_io_free(th_ioctx *ctx);