Mercurial > hg > nnchat
comparison main.c @ 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 | c3e4e8f3c658 |
comparison
equal
deleted
inserted
replaced
560:1b2d434a651b | 561:1ff9e85a1fcc |
---|---|
68 *setFriendList = NULL, | 68 *setFriendList = NULL, |
69 *setIdleMessages = NULL; | 69 *setIdleMessages = NULL; |
70 nn_userhash_t *nnUsers = NULL; | 70 nn_userhash_t *nnUsers = NULL; |
71 char *setConfigFile = NULL, | 71 char *setConfigFile = NULL, |
72 *setBrowser = NULL; | 72 *setBrowser = NULL; |
73 cfgitem_t *cfg = NULL; | 73 th_cfgitem_t *cfg = NULL; |
74 | 74 |
75 nn_editbuf_t *editHistBuf[SET_MAX_HISTORY+2]; | 75 nn_editbuf_t *editHistBuf[SET_MAX_HISTORY+2]; |
76 int editHistPos = 0, | 76 int editHistPos = 0, |
77 editHistMax = 0; | 77 editHistMax = 0; |
78 | 78 |
995 | 995 |
996 int nncmd_save_config(nn_conn_t *conn, char *buf) | 996 int nncmd_save_config(nn_conn_t *conn, char *buf) |
997 { | 997 { |
998 (void) conn; | 998 (void) conn; |
999 (void) buf; | 999 (void) buf; |
1000 FILE *cfgfile = NULL; | 1000 th_ioctx_t ctx; |
1001 #ifndef __WIN32 | 1001 #ifndef __WIN32 |
1002 int cfgfd = -1; | 1002 int cfgfd = -1; |
1003 #endif | 1003 #endif |
1004 | 1004 |
1005 if (!th_ioctx_init(&ctx, setConfigFile)) | |
1006 { | |
1007 printMsgQ(currWin, "Could not initialize I/O context for configuration file writing!\n"); | |
1008 goto error; | |
1009 } | |
1010 | |
1005 #ifdef __WIN32 | 1011 #ifdef __WIN32 |
1006 if ((cfgfile = fopen(setConfigFile, "w")) == NULL) | 1012 if ((ctx.fp = fopen(setConfigFile, "w")) == NULL) |
1007 #else | 1013 #else |
1008 if ((cfgfd = open(setConfigFile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || | 1014 if ((cfgfd = open(setConfigFile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || |
1009 (cfgfile = fdopen(cfgfd, "w")) == NULL) | 1015 (ctx.fp = fdopen(cfgfd, "w")) == NULL) |
1010 #endif | 1016 #endif |
1011 { | 1017 { |
1012 printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", | 1018 printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", |
1013 setConfigFile, errno, strerror(errno)); | 1019 setConfigFile, errno, strerror(errno)); |
1014 goto error; | 1020 goto error; |
1015 } | 1021 } |
1016 | 1022 |
1017 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", | 1023 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", |
1018 setConfigFile, | 1024 setConfigFile, th_cfg_write(&ctx, cfg)); |
1019 th_cfg_write(cfgfile, setConfigFile, cfg)); | |
1020 | 1025 |
1021 error: | 1026 error: |
1022 if (cfgfile != NULL) | 1027 th_ioctx_close(&ctx); |
1023 fclose(cfgfile); | |
1024 #ifndef __WIN32 | |
1025 else | |
1026 if (cfgfd >= 0) | |
1027 close(cfgfd); | |
1028 #endif | |
1029 return 0; | 1028 return 0; |
1030 } | 1029 } |
1031 | 1030 |
1032 | 1031 |
1033 enum | 1032 enum |
1736 int index, updateCount = 0; | 1735 int index, updateCount = 0; |
1737 BOOL argsOK, colorSet = FALSE; | 1736 BOOL argsOK, colorSet = FALSE; |
1738 nn_conn_t *conn = NULL; | 1737 nn_conn_t *conn = NULL; |
1739 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); | 1738 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); |
1740 nn_editstate_t editState; | 1739 nn_editstate_t editState; |
1741 cfgitem_t *tmpcfg; | 1740 th_cfgitem_t *tmpcfg; |
1742 char *setHomeDir = NULL; | 1741 char *setHomeDir = NULL; |
1743 | 1742 |
1744 memset(editHistBuf, 0, sizeof(editHistBuf)); | 1743 memset(editHistBuf, 0, sizeof(editHistBuf)); |
1745 clearEditState(&editState); | 1744 clearEditState(&editState); |
1746 | 1745 |
1837 } | 1836 } |
1838 #endif | 1837 #endif |
1839 | 1838 |
1840 if (setHomeDir != NULL) | 1839 if (setHomeDir != NULL) |
1841 { | 1840 { |
1842 FILE *cfgfile; | 1841 th_ioctx_t ctx; |
1843 setConfigFile = th_strdup_printf("%s%c%s", setHomeDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE); | 1842 setConfigFile = th_strdup_printf("%s%c%s", setHomeDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE); |
1844 | 1843 |
1845 THMSG(0, "Reading configuration from '%s'.\n", setConfigFile); | 1844 THMSG(0, "Reading configuration from '%s'.\n", setConfigFile); |
1846 | 1845 |
1847 if ((cfgfile = fopen(setConfigFile, "r")) != NULL) | 1846 if (th_ioctx_open(&ctx, setConfigFile, "r")) |
1848 { | 1847 { |
1849 th_cfg_read(cfgfile, setConfigFile, cfg); | 1848 th_cfg_read(&ctx, cfg); |
1850 fclose(cfgfile); | 1849 th_ioctx_close(&ctx); |
1851 } | 1850 } |
1852 } | 1851 } |
1853 | 1852 |
1854 optNickSep = optNickSepStr ? optNickSepStr[0] : SET_NICK_SEPARATOR; | 1853 optNickSep = optNickSepStr ? optNickSepStr[0] : SET_NICK_SEPARATOR; |
1855 | 1854 |