changeset 650:24cbab6e88c6

Remove th_io_free(), merge the functionality to th_io_close(). Add flags allocation for certain elements and/of the th_ioctx structure itself. Add helper function th_io_init_stdio(th_ioctx *ctx, FILE *fh) for initializing th_ioctx with stdio fops and given file handle.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 13:05:11 +0200
parents 2c9260f5cf44
children 18fe45e61b2b
files tests.c th_ioctx.c th_ioctx.h
diffstat 3 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Sat Jan 25 11:35:59 2020 +0200
+++ b/tests.c	Sat Jan 25 13:05:11 2020 +0200
@@ -474,7 +474,7 @@
     }
 
 out:
-    th_io_free(fh);
+    th_io_close(fh);
 }
 
 
@@ -544,7 +544,7 @@
     th_io_set_handlers(fh, test_ioctx_error, test_ioctx_msg);
 
     th_cfg_write(fh, cfg);
-    th_io_free(fh);
+    th_io_close(fh);
     fh = NULL;
 
     // Test against written configuration file
@@ -555,7 +555,7 @@
 
 out:
     // Free the data for v_str_list
-    th_io_free(fh);
+    th_io_close(fh);
     th_llist_free_func_node(v_str_list, test_config_free);
     v_str_list = NULL;
 }
--- a/th_ioctx.c	Sat Jan 25 11:35:59 2020 +0200
+++ b/th_ioctx.c	Sat Jan 25 13:05:11 2020 +0200
@@ -17,21 +17,42 @@
 }
 
 
+static void th_io_init(th_ioctx *ctx)
+{
+    memset(ctx, 0, sizeof(th_ioctx));
+    ctx->line = 1;
+}
+
+
+void th_io_init_stdio(th_ioctx *ctx, FILE *fh)
+{
+    th_io_init(ctx);
+
+    ctx->fops = &th_stdio_io_ops;
+    ctx->data = (void *) fh;
+}
+
+
 th_ioctx * th_io_new(const th_ioctx_ops *fops, const char *filename)
 {
-    th_ioctx *ctx = th_malloc0(sizeof(th_ioctx));
+    th_ioctx *ctx = th_malloc(sizeof(th_ioctx));
     if (ctx == NULL)
         return NULL;
 
+    th_io_init(ctx);
+
+    ctx->allocated = TRUE;
     ctx->fops = fops;
+
     ctx->filename = th_strdup(filename);
+    ctx->fallocated = TRUE;
     if (filename != NULL && ctx->filename == NULL)
         goto err;
 
     return ctx;
 
 err:
-    th_io_free(ctx);
+    th_io_close(ctx);
     return NULL;
 }
 
@@ -41,8 +62,10 @@
     if (ctx == NULL)
         return THERR_NULLPTR;
 
-    ctx->mode = th_strdup(mode);
-    ctx->line = 1;
+    if (mode != NULL && (ctx->mode = th_strdup(mode)) == NULL)
+        return THERR_MALLOC;
+
+    ctx->mallocated = TRUE;
 
     if (ctx->fops->fopen != NULL)
         ctx->status = ctx->fops->fopen(ctx);
@@ -65,7 +88,7 @@
     return THERR_OK;
 
 err:
-    th_io_free(ctx);
+    th_io_close(ctx);
     *pctx = NULL;
     return res;
 }
@@ -75,22 +98,17 @@
 {
     if (ctx != NULL)
     {
-        if (ctx->fops->fclose != NULL)
+        if (ctx->fops != NULL && ctx->fops->fclose != NULL)
             ctx->fops->fclose(ctx);
-    }
-}
 
+        if (ctx->fallocated)
+            th_free_r(&ctx->filename);
 
-void th_io_free(th_ioctx *ctx)
-{
-    if (ctx != NULL)
-    {
-        th_io_close(ctx);
+        if (ctx->mallocated)
+            th_free_r(&ctx->mode);
 
-        th_free_r(&ctx->filename);
-        th_free_r(&ctx->mode);
-
-        th_free(ctx);
+        if (ctx->allocated)
+            th_free(ctx);
     }
 }
 
--- a/th_ioctx.h	Sat Jan 25 11:35:59 2020 +0200
+++ b/th_ioctx.h	Sat Jan 25 13:05:11 2020 +0200
@@ -29,7 +29,11 @@
 typedef struct th_ioctx
 {
     char *filename;     ///< Context filename, if any. May be NULL.
+    BOOL fallocated;    ///< TRUE if filename is allocated, FALSE if "const"
     char *mode;         ///< Context's stdio open "mode", may also be NULL.
+    BOOL mallocated;    ///< TRUE if mode string is allocated, FALSE if "const"
+    BOOL allocated;     ///< TRUE if this structure has been allocated
+
     void *data;         ///< Internal data
     time_t atime;       ///< Last access time
     int64_t size;       ///< Size in bytes
@@ -82,7 +86,8 @@
 int          th_io_open(th_ioctx *ctx, const char *mode);
 int          th_io_fopen(th_ioctx **pctx, 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);
+
+void         th_io_init_stdio(th_ioctx *ctx, FILE *fh);
 
 BOOL         th_io_set_handlers(th_ioctx *ctx,
              void (*error)(th_ioctx *, const int, const char *msg),