changeset 561:1ff9e85a1fcc

Rework the configuration file loading and saving to use the I/O context and new th-config API.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 15 Nov 2012 19:45:53 +0200
parents 1b2d434a651b
children dbeca46a7c58
files Makefile.gen main.c
diffstat 2 files changed, 18 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Wed Nov 14 18:57:27 2012 +0200
+++ b/Makefile.gen	Thu Nov 15 19:45:53 2012 +0200
@@ -15,7 +15,7 @@
 # Objects
 #
 THLIBS_A=$(OBJPATH)thlibs.a
-THLIBS_OBJ=th_util.o th_string.o th_args.o th_config.o
+THLIBS_OBJ=th_util.o th_string.o th_args.o th_ioctx.o th_config.o
 
 NNCHAT_OBJ=main.o util.o network.o ui.o
 NNCHAT_BIN=$(BINPATH)nnchat$(EXEEXT)
--- a/main.c	Wed Nov 14 18:57:27 2012 +0200
+++ b/main.c	Thu Nov 15 19:45:53 2012 +0200
@@ -70,7 +70,7 @@
 nn_userhash_t *nnUsers = NULL;
 char    *setConfigFile = NULL,
         *setBrowser = NULL;
-cfgitem_t *cfg = NULL;
+th_cfgitem_t *cfg = NULL;
 
 nn_editbuf_t *editHistBuf[SET_MAX_HISTORY+2];
 int      editHistPos = 0,
@@ -997,16 +997,22 @@
 {
     (void) conn;
     (void) buf;
-    FILE *cfgfile = NULL;
+    th_ioctx_t ctx;
 #ifndef __WIN32
     int cfgfd = -1;
 #endif
 
+    if (!th_ioctx_init(&ctx, setConfigFile))
+    {
+        printMsgQ(currWin, "Could not initialize I/O context for configuration file writing!\n");
+        goto error;
+    }
+
 #ifdef __WIN32
-    if ((cfgfile = fopen(setConfigFile, "w")) == NULL)
+    if ((ctx.fp = fopen(setConfigFile, "w")) == NULL)
 #else
     if ((cfgfd = open(setConfigFile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 ||
-        (cfgfile = fdopen(cfgfd, "w")) == NULL)
+        (ctx.fp = fdopen(cfgfd, "w")) == NULL)
 #endif
     {
         printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n",
@@ -1015,17 +1021,10 @@
     }
 
     printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n",
-        setConfigFile,
-        th_cfg_write(cfgfile, setConfigFile, cfg));
+        setConfigFile, th_cfg_write(&ctx, cfg));
 
 error:
-    if (cfgfile != NULL)
-        fclose(cfgfile);
-#ifndef __WIN32
-    else
-    if (cfgfd >= 0)
-        close(cfgfd);
-#endif
+    th_ioctx_close(&ctx);
     return 0;
 }
 
@@ -1738,7 +1737,7 @@
     nn_conn_t *conn = NULL;
     nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE);
     nn_editstate_t editState;
-    cfgitem_t *tmpcfg;
+    th_cfgitem_t *tmpcfg;
     char *setHomeDir = NULL;
 
     memset(editHistBuf, 0, sizeof(editHistBuf));
@@ -1839,15 +1838,15 @@
 
     if (setHomeDir != NULL)
     {
-        FILE *cfgfile;
+        th_ioctx_t ctx;
         setConfigFile = th_strdup_printf("%s%c%s", setHomeDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE);
 
         THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
 
-        if ((cfgfile = fopen(setConfigFile, "r")) != NULL)
+        if (th_ioctx_open(&ctx, setConfigFile, "r"))
         {
-            th_cfg_read(cfgfile, setConfigFile, cfg);
-            fclose(cfgfile);
+            th_cfg_read(&ctx, cfg);
+            th_ioctx_close(&ctx);
         }
     }