changeset 72:43df05a632cb

Break I/O context API, add function pointers to init/open/new functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 15 Nov 2012 19:52:09 +0200
parents ce49160d2599
children 8ff993207415
files th_ioctx.c th_ioctx.h
diffstat 2 files changed, 28 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/th_ioctx.c	Thu Nov 15 19:44:59 2012 +0200
+++ b/th_ioctx.c	Thu Nov 15 19:52:09 2012 +0200
@@ -10,12 +10,17 @@
 
 /* Simple STD I/O contexts
  */
-BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename)
+BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg))
+
 {
     if (ctx == NULL || filename == NULL)
         return FALSE;
 
     th_memset(ctx, 0, sizeof(ctx));
+    ctx->error = error;
+    ctx->msg   = msg;
 
     if ((ctx->filename = th_strdup(filename)) == NULL)
         return FALSE;
@@ -24,9 +29,11 @@
 }
 
 
-BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode)
+BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg))
 {
-    if (!th_ioctx_init(ctx, filename) || mode == NULL)
+    if (!th_ioctx_init(ctx, filename, error, msg) || mode == NULL)
         return FALSE;
 
     if ((ctx->fp = fopen(filename, mode)) == NULL)
@@ -68,13 +75,15 @@
 }
 
 
-th_ioctx_t *th_ioctx_new(const char *filename)
+th_ioctx_t *th_ioctx_new(const char *filename,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg))
 {
     th_ioctx_t *ctx = th_malloc0(sizeof(th_ioctx_t));
     if (ctx == NULL)
         return NULL;
     
-    th_ioctx_init(ctx, filename);
+    th_ioctx_init(ctx, filename, error, msg);
     ctx->allocated = TRUE;
     return ctx;
 }
--- a/th_ioctx.h	Thu Nov 15 19:44:59 2012 +0200
+++ b/th_ioctx.h	Thu Nov 15 19:52:09 2012 +0200
@@ -30,13 +30,21 @@
 } th_ioctx_t;
 
 
-BOOL         th_ioctx_init(th_ioctx_t *ctx, const char *filename);
-BOOL         th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode);
-void         th_ioctx_close(th_ioctx_t *ctx);
-void         th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...);
+BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg));
+
+BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg));
 
-th_ioctx_t * th_ioctx_new(const char *filename);
-void         th_ioctx_free(th_ioctx_t *ctx);
+void th_ioctx_close(th_ioctx_t *ctx);
+void th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...);
+
+th_ioctx_t * th_ioctx_new(const char *filename,
+    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
+    void (*msg)(struct _th_ioctx_t *, const char *msg));
+void th_ioctx_free(th_ioctx_t *ctx);
 
 
 #ifdef __cplusplus