Mercurial > hg > th-libs
comparison 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 |
comparison
equal
deleted
inserted
replaced
203:7acdd3ab6900 | 204:55f429dff750 |
---|---|
26 int th_io_open(th_ioctx *ctx, const char *filename, const char *mode) | 26 int th_io_open(th_ioctx *ctx, const char *filename, const char *mode) |
27 { | 27 { |
28 if (ctx == NULL) | 28 if (ctx == NULL) |
29 return THERR_NULLPTR; | 29 return THERR_NULLPTR; |
30 | 30 |
31 th_free_r(&ctx->filename); | |
32 th_free_r(&ctx->mode); | |
33 | |
34 ctx->filename = th_strdup(filename); | 31 ctx->filename = th_strdup(filename); |
35 ctx->mode = th_strdup(mode); | 32 ctx->mode = th_strdup(mode); |
36 | 33 |
37 if (ctx->fops->fopen != NULL) | 34 if (ctx->fops->fopen != NULL) |
38 ctx->errno = ctx->fops->fopen(ctx); | 35 ctx->errno = ctx->fops->fopen(ctx); |
39 | 36 |
40 return ctx->errno; | 37 return ctx->errno; |
38 } | |
39 | |
40 | |
41 th_ioctx * th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode) | |
42 { | |
43 th_ioctx *ctx = th_io_new(fops); | |
44 if (ctx == NULL) | |
45 return NULL; | |
46 | |
47 if (th_io_open(ctx, filename, mode) != THERR_OK) | |
48 { | |
49 th_io_free(ctx); | |
50 return NULL; | |
51 } | |
52 | |
53 return ctx; | |
41 } | 54 } |
42 | 55 |
43 | 56 |
44 void th_io_close(th_ioctx *ctx) | 57 void th_io_close(th_ioctx *ctx) |
45 { | 58 { |
285 | 298 |
286 static int th_stdio_fopen(th_ioctx *ctx) | 299 static int th_stdio_fopen(th_ioctx *ctx) |
287 { | 300 { |
288 ctx->data = (void *) fopen(ctx->filename, ctx->mode); | 301 ctx->data = (void *) fopen(ctx->filename, ctx->mode); |
289 ctx->errno = th_get_error(); | 302 ctx->errno = th_get_error(); |
290 return (CTX_FH != NULL) ? THERR_OK : THERR_FOPEN; | 303 return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN; |
291 } | 304 } |
292 | 305 |
293 | 306 |
294 static void th_stdio_fclose(th_ioctx *ctx) | 307 static void th_stdio_fclose(th_ioctx *ctx) |
295 { | 308 { |