# HG changeset patch # User Matti Hamalainen # Date 1353001553 -7200 # Node ID 1ff9e85a1fcc8e19279fe979409213f74bae01db # Parent 1b2d434a651b59df45ee5e361c3d34bd3dafafe9 Rework the configuration file loading and saving to use the I/O context and new th-config API. diff -r 1b2d434a651b -r 1ff9e85a1fcc Makefile.gen --- 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) diff -r 1b2d434a651b -r 1ff9e85a1fcc main.c --- 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); } }