Mercurial > hg > nnchat
comparison main.c @ 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 |
comparison
equal
deleted
inserted
replaced
662:0c94af18f55e | 663:0b81e5fdc7b5 |
---|---|
610 (void) loglevel; | 610 (void) loglevel; |
611 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); | 611 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); |
612 } | 612 } |
613 | 613 |
614 | 614 |
615 void nn_ioctx_errfunc(th_ioctx_t *ctx, int err, const char *msg) | 615 void nn_ioctx_errfunc(th_ioctx *ctx, const int err, const char *msg) |
616 { | 616 { |
617 (void) err; | 617 (void) err; |
618 errorMsg("[%s:%d] %s", | 618 errorMsg("[%s:%d] %s", |
619 ctx->filename, ctx->line, msg); | 619 ctx->filename, ctx->line, msg); |
620 } | 620 } |
621 | 621 |
622 | 622 |
623 void nn_ioctx_msgfunc(th_ioctx_t *ctx, const char *msg) | 623 void nn_ioctx_msgfunc(th_ioctx *ctx, const int level, const char *msg) |
624 { | 624 { |
625 (void) ctx; | 625 (void) ctx; |
626 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); | 626 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); |
627 } | 627 } |
628 | 628 |
1264 | 1264 |
1265 int nncmd_save_config(th_conn_t *conn, char *buf) | 1265 int nncmd_save_config(th_conn_t *conn, char *buf) |
1266 { | 1266 { |
1267 (void) conn; | 1267 (void) conn; |
1268 (void) buf; | 1268 (void) buf; |
1269 th_ioctx_t ctx; | 1269 th_ioctx *ctx = NULL; |
1270 #ifndef TH_PLAT_WINDOWS | 1270 #ifndef TH_PLAT_WINDOWS |
1271 int cfgfd = -1; | 1271 int cfgfd = -1; |
1272 #endif | 1272 #endif |
1273 | 1273 |
1274 if (!th_ioctx_init(&ctx, setConfigFile, nn_ioctx_errfunc, nn_ioctx_msgfunc)) | 1274 if ((ctx = th_io_new(&th_stdio_io_ops, setConfigFile)) == NULL) |
1275 { | 1275 { |
1276 printMsgQ(currWin, "Could not initialize I/O context for configuration file writing!\n"); | 1276 printMsgQ(currWin, "Could not initialize I/O context for configuration file writing!\n"); |
1277 goto error; | 1277 goto error; |
1278 } | 1278 } |
1279 | 1279 |
1280 #ifdef __WIN32 | 1280 th_io_set_handlers(ctx, nn_ioctx_errfunc, nn_ioctx_msgfunc); |
1281 if ((ctx.fp = fopen(setConfigFile, "w")) == NULL) | 1281 |
1282 #ifdef TH_PLAT_WINDOWS | |
1283 if ((ctx->fp = fopen(ctx->filename, "w")) == NULL) | |
1282 #else | 1284 #else |
1283 if ((cfgfd = open(setConfigFile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || | 1285 if ((cfgfd = open(ctx->filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || |
1284 (ctx.fp = fdopen(cfgfd, "w")) == NULL) | 1286 (ctx->data = (void *) fdopen(cfgfd, "w")) == NULL) |
1285 #endif | 1287 #endif |
1286 { | 1288 { |
1287 int err = th_get_error(); | 1289 int err = th_get_error(); |
1288 printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", | 1290 printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", |
1289 setConfigFile, err, th_error_str(err)); | 1291 ctx->filename, err, th_error_str(err)); |
1290 goto error; | 1292 goto error; |
1291 } | 1293 } |
1292 | 1294 |
1293 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", | 1295 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", |
1294 setConfigFile, th_cfg_write(&ctx, cfg)); | 1296 ctx->filename, th_cfg_write(ctx, cfg)); |
1295 | 1297 |
1296 error: | 1298 error: |
1297 th_ioctx_close(&ctx); | 1299 th_io_free(ctx); |
1298 return 0; | 1300 return 0; |
1299 } | 1301 } |
1300 | 1302 |
1301 | 1303 |
1302 int nncmd_quit(th_conn_t *conn, char *buf) | 1304 int nncmd_quit(th_conn_t *conn, char *buf) |
2048 #endif | 2050 #endif |
2049 } | 2051 } |
2050 | 2052 |
2051 if (setConfigDir != NULL) | 2053 if (setConfigDir != NULL) |
2052 { | 2054 { |
2053 th_ioctx_t ctx; | 2055 th_ioctx *ctx; |
2056 | |
2054 setConfigFile = th_strdup_printf("%s%c%s", setConfigDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE); | 2057 setConfigFile = th_strdup_printf("%s%c%s", setConfigDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE); |
2055 | 2058 if ((ctx = th_io_fopen(&th_stdio_io_ops, setConfigFile, "r")) != NULL) |
2056 THMSG(0, "Reading configuration from '%s'.\n", setConfigFile); | 2059 { |
2057 | 2060 th_io_set_handlers(ctx, nn_ioctx_errfunc, nn_ioctx_msgfunc); |
2058 if (th_ioctx_open(&ctx, setConfigFile, "r", nn_ioctx_errfunc, nn_ioctx_msgfunc)) | 2061 |
2059 { | 2062 THMSG(0, "Reading configuration from '%s'.\n", setConfigFile); |
2060 th_cfg_read(&ctx, cfg); | 2063 |
2061 th_ioctx_close(&ctx); | 2064 th_cfg_read(ctx, cfg); |
2065 th_io_close(ctx); | |
2062 } | 2066 } |
2063 } | 2067 } |
2064 | 2068 |
2065 if (setProxyURI && !argHandleProxyURI(setProxyURI)) | 2069 if (setProxyURI && !argHandleProxyURI(setProxyURI)) |
2066 goto err_exit; | 2070 goto err_exit; |