Mercurial > hg > th-libs
comparison th_ioctx.c @ 477:96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Jul 2018 08:06:45 +0300 |
parents | 04320ca79407 |
children | e4ce60239d16 |
comparison
equal
deleted
inserted
replaced
476:61fed1286780 | 477:96d137a6b392 |
---|---|
15 { | 15 { |
16 ctx->atime = time(NULL); | 16 ctx->atime = time(NULL); |
17 } | 17 } |
18 | 18 |
19 | 19 |
20 th_ioctx *th_io_new(const th_ioctx_ops *fops, const char *filename) | 20 th_ioctx * th_io_new(const th_ioctx_ops *fops, const char *filename) |
21 { | 21 { |
22 th_ioctx *ctx = th_malloc0(sizeof(th_ioctx)); | 22 th_ioctx *ctx = th_malloc0(sizeof(th_ioctx)); |
23 if (ctx == NULL) | 23 if (ctx == NULL) |
24 return NULL; | 24 return NULL; |
25 | 25 |
26 ctx->fops = fops; | 26 ctx->fops = fops; |
27 ctx->filename = th_strdup(filename); | 27 ctx->filename = th_strdup(filename); |
28 if (filename != NULL && ctx->filename == NULL) | |
29 goto err; | |
28 | 30 |
29 return ctx; | 31 return ctx; |
32 | |
33 err: | |
34 th_io_free(ctx); | |
35 return NULL; | |
30 } | 36 } |
31 | 37 |
32 | 38 |
33 int th_io_open(th_ioctx *ctx, const char *mode) | 39 int th_io_open(th_ioctx *ctx, const char *mode) |
34 { | 40 { |
42 | 48 |
43 return ctx->status; | 49 return ctx->status; |
44 } | 50 } |
45 | 51 |
46 | 52 |
47 th_ioctx * th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode) | 53 int th_io_fopen(th_ioctx **pctx, const th_ioctx_ops *fops, const char *filename, const char *mode) |
48 { | 54 { |
49 th_ioctx *ctx = th_io_new(fops, filename); | 55 th_ioctx *ctx; |
50 if (ctx == NULL) | 56 int res; |
51 return NULL; | 57 |
52 | 58 if ((*pctx = ctx = th_io_new(fops, filename)) == NULL) |
53 if (th_io_open(ctx, mode) != THERR_OK) | 59 return THERR_MALLOC; |
54 { | 60 |
55 th_io_free(ctx); | 61 if ((res = th_io_open(ctx, mode)) != THERR_OK) |
56 return NULL; | 62 goto err; |
57 } | 63 |
58 | 64 return THERR_OK; |
59 return ctx; | 65 |
66 err: | |
67 th_io_free(ctx); | |
68 *pctx = NULL; | |
69 return res; | |
60 } | 70 } |
61 | 71 |
62 | 72 |
63 void th_io_close(th_ioctx *ctx) | 73 void th_io_close(th_ioctx *ctx) |
64 { | 74 { |
493 | 503 |
494 th_stdio_fgets, | 504 th_stdio_fgets, |
495 th_stdio_fputs, | 505 th_stdio_fputs, |
496 th_stdio_vfprintf, | 506 th_stdio_vfprintf, |
497 }; | 507 }; |
498 |