Mercurial > hg > th-libs
diff th_ioctx.c @ 204:55f429dff750
Add th_io_fopen() helper function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 11 Feb 2016 23:14:54 +0200 |
parents | b392293047da |
children | c2193323736d |
line wrap: on
line diff
--- a/th_ioctx.c Thu Feb 11 22:59:30 2016 +0200 +++ b/th_ioctx.c Thu Feb 11 23:14:54 2016 +0200 @@ -28,9 +28,6 @@ if (ctx == NULL) return THERR_NULLPTR; - th_free_r(&ctx->filename); - th_free_r(&ctx->mode); - ctx->filename = th_strdup(filename); ctx->mode = th_strdup(mode); @@ -41,6 +38,22 @@ } +th_ioctx * th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode) +{ + th_ioctx *ctx = th_io_new(fops); + if (ctx == NULL) + return NULL; + + if (th_io_open(ctx, filename, mode) != THERR_OK) + { + th_io_free(ctx); + return NULL; + } + + return ctx; +} + + void th_io_close(th_ioctx *ctx) { if (ctx != NULL) @@ -287,7 +300,7 @@ { ctx->data = (void *) fopen(ctx->filename, ctx->mode); ctx->errno = th_get_error(); - return (CTX_FH != NULL) ? THERR_OK : THERR_FOPEN; + return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN; }