changeset 663:0b81e5fdc7b5

Use the new io context API.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Feb 2016 03:03:17 +0200
parents 0c94af18f55e
children 87ef546de419
files main.c
diffstat 1 files changed, 22 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Fri Feb 12 03:02:12 2016 +0200
+++ b/main.c	Fri Feb 12 03:03:17 2016 +0200
@@ -612,7 +612,7 @@
 }
 
 
-void nn_ioctx_errfunc(th_ioctx_t *ctx, int err, const char *msg)
+void nn_ioctx_errfunc(th_ioctx *ctx, const int err, const char *msg)
 {
     (void) err;
     errorMsg("[%s:%d] %s",
@@ -620,7 +620,7 @@
 }
 
 
-void nn_ioctx_msgfunc(th_ioctx_t *ctx, const char *msg)
+void nn_ioctx_msgfunc(th_ioctx *ctx, const int level, const char *msg)
 {
     (void) ctx;
     printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg);
@@ -1266,35 +1266,37 @@
 {
     (void) conn;
     (void) buf;
-    th_ioctx_t ctx;
+    th_ioctx *ctx = NULL;
 #ifndef TH_PLAT_WINDOWS
     int cfgfd = -1;
 #endif
 
-    if (!th_ioctx_init(&ctx, setConfigFile, nn_ioctx_errfunc, nn_ioctx_msgfunc))
+    if ((ctx = th_io_new(&th_stdio_io_ops, setConfigFile)) == NULL)
     {
         printMsgQ(currWin, "Could not initialize I/O context for configuration file writing!\n");
         goto error;
     }
 
-#ifdef __WIN32
-    if ((ctx.fp = fopen(setConfigFile, "w")) == NULL)
+    th_io_set_handlers(ctx, nn_ioctx_errfunc, nn_ioctx_msgfunc);
+
+#ifdef TH_PLAT_WINDOWS
+    if ((ctx->fp = fopen(ctx->filename, "w")) == NULL)
 #else
-    if ((cfgfd = open(setConfigFile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 ||
-        (ctx.fp = fdopen(cfgfd, "w")) == NULL)
+    if ((cfgfd = open(ctx->filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 ||
+        (ctx->data = (void *) fdopen(cfgfd, "w")) == NULL)
 #endif
     {
         int err = th_get_error();
         printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n",
-            setConfigFile, err, th_error_str(err));
+            ctx->filename, err, th_error_str(err));
         goto error;
     }
 
     printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n",
-        setConfigFile, th_cfg_write(&ctx, cfg));
+        ctx->filename, th_cfg_write(ctx, cfg));
 
 error:
-    th_ioctx_close(&ctx);
+    th_io_free(ctx);
     return 0;
 }
 
@@ -2050,15 +2052,17 @@
 
     if (setConfigDir != NULL)
     {
-        th_ioctx_t ctx;
-        setConfigFile = th_strdup_printf("%s%c%s", setConfigDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE);
-
-        THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
+        th_ioctx *ctx;
 
-        if (th_ioctx_open(&ctx, setConfigFile, "r", nn_ioctx_errfunc, nn_ioctx_msgfunc))
+        setConfigFile = th_strdup_printf("%s%c%s", setConfigDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE);
+        if ((ctx = th_io_fopen(&th_stdio_io_ops, setConfigFile, "r")) != NULL)
         {
-            th_cfg_read(&ctx, cfg);
-            th_ioctx_close(&ctx);
+            th_io_set_handlers(ctx, nn_ioctx_errfunc, nn_ioctx_msgfunc);
+
+            THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
+
+            th_cfg_read(ctx, cfg);
+            th_io_close(ctx);
         }
     }