Mercurial > hg > th-libs
annotate 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 |
rev | line source |
---|---|
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
211 | 2 * Simple I/O abstraction and context handling layer |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
454 | 4 * (C) Copyright 2012-2018 Tecnic Software productions (TNSP) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_ioctx.h" |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
10 #include "th_endian.h" |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
11 #include <stdio.h> |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
72
43df05a632cb
Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
13 |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
14 static void th_io_update_atime(th_ioctx *ctx) |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
15 { |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
16 ctx->atime = time(NULL); |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
17 } |
460
0a1a65503e0b
Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
18 |
0a1a65503e0b
Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
19 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
20 th_ioctx * th_io_new(const th_ioctx_ops *fops, const char *filename) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
22 th_ioctx *ctx = th_malloc0(sizeof(th_ioctx)); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
23 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
24 return NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
25 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
26 ctx->fops = fops; |
209
462b837ea492
Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
27 ctx->filename = th_strdup(filename); |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
28 if (filename != NULL && ctx->filename == NULL) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
29 goto err; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
30 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
31 return ctx; |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
32 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
33 err: |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
34 th_io_free(ctx); |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
35 return NULL; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
36 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
37 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
209
462b837ea492
Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
39 int th_io_open(th_ioctx *ctx, const char *mode) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
40 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
41 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
42 return THERR_NULLPTR; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
43 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
44 ctx->mode = th_strdup(mode); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
45 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
46 if (ctx->fops->fopen != NULL) |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
47 ctx->status = ctx->fops->fopen(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
48 |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
49 return ctx->status; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
53 int th_io_fopen(th_ioctx **pctx, const th_ioctx_ops *fops, const char *filename, const char *mode) |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
54 { |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
55 th_ioctx *ctx; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
56 int res; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
57 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
58 if ((*pctx = ctx = th_io_new(fops, filename)) == NULL) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
59 return THERR_MALLOC; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
60 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
61 if ((res = th_io_open(ctx, mode)) != THERR_OK) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
62 goto err; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
63 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
64 return THERR_OK; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
65 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
66 err: |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
67 th_io_free(ctx); |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
68 *pctx = NULL; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
69 return res; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
70 } |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
71 |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
72 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
73 void th_io_close(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
74 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
75 if (ctx != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
76 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
77 if (ctx->fops->fclose != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
78 ctx->fops->fclose(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
79 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
80 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
81 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
82 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
83 void th_io_free(th_ioctx *ctx) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
85 if (ctx != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
86 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
87 th_io_close(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
88 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
89 th_free_r(&ctx->filename); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
90 th_free_r(&ctx->mode); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
91 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
92 th_free(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
93 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
94 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
95 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
96 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
97 BOOL th_io_set_handlers(th_ioctx *ctx, |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
98 void (*error)(th_ioctx *, const int, const char *msg), |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
99 void (*msg)(th_ioctx *, const int, const char *msg)) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
100 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
101 if (ctx == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
102 return FALSE; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
104 ctx->error = error; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
105 ctx->msg = msg; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
107 return TRUE; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
111 void th_io_error_v(th_ioctx *ctx, const int err, const char *fmt, va_list ap) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
113 char *msg = th_strdup_vprintf(fmt, ap); |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 if (ctx->error != NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
116 ctx->error((struct th_ioctx *) ctx, err, msg); |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 else |
395
bffd3caf2d2c
Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents:
219
diff
changeset
|
118 THERR("'%s' #%" PRIu_SIZE_T ": %s\n", ctx->filename, ctx->line, msg); |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 th_free(msg); |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
124 void th_io_error(th_ioctx *ctx, const int err, const char *fmt, ...) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
125 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
126 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
127 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
128 th_io_error_v(ctx, err, fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
129 va_end(ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
130 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
131 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
132 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
133 void th_io_msg_v(th_ioctx *ctx, const int level, const char *fmt, va_list ap) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
135 if (ctx->msg != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
136 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
137 char *msg = th_strdup_vprintf(fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
138 ctx->msg((struct th_ioctx *) ctx, level, msg); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
139 th_free(msg); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
140 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
141 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
142 THMSG_V(level, fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
143 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
144 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
145 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
146 void th_io_msg(th_ioctx *ctx, const int level, const char *fmt, ...) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
147 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
148 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
149 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
150 th_io_msg_v(ctx, level, fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
151 va_end(ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
152 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
153 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
154 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
155 int thfreset(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
156 { |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 if (ctx == NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
158 return THERR_NULLPTR; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
159 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
160 if (ctx->fops == NULL || ctx->fops->freset == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
161 return THERR_OK; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
162 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
163 return ctx->fops->freset(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
164 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
165 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
166 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
167 int thferror(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
168 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
169 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
170 return ctx->fops->ferror(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
171 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
172 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
173 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
174 int thfseek(th_ioctx *ctx, const off_t offset, int whence) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
175 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
176 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
177 return ctx->fops->fseek(ctx, offset, whence); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
178 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
179 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
180 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
181 off_t thfsize(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
182 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
183 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
184 return ctx->fops->fsize(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
185 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
186 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
187 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
188 off_t thftell(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
189 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
190 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
191 return ctx->fops->ftell(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
192 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
193 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
194 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
195 BOOL thfeof(th_ioctx *ctx) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
196 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
197 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
198 return ctx->fops->feof(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
199 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
200 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
201 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
202 int thfgetc(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
203 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
204 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
205 return ctx->fops->fgetc(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
206 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
207 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
208 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
209 int thfputc(int v, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
210 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
211 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
212 return ctx->fops->fputc(v, ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
213 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
214 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
215 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
216 size_t thfread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
217 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
218 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
219 return ctx->fops->fread(ptr, size, nmemb, ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
220 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
221 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
222 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
223 size_t thfwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
224 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
225 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
226 return ctx->fops->fwrite(ptr, size, nmemb, ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
227 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
228 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
229 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
230 char *thfgets(char *str, int size, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
231 { |
206 | 232 char *ptr = str, *end = str + size - 1; |
233 int c; | |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
234 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
235 if (size <= 0) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 return NULL; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
237 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
238 while (ptr < end && (c = ctx->fops->fgetc(ctx)) != EOF) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
239 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
240 *ptr++ = c; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
241 if (c == '\n') |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
242 break; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
243 } |
206 | 244 *ptr = 0; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
245 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
246 return (ptr > str) ? str : NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
247 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
248 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
249 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
250 int thfputs(const char *ptr, th_ioctx *ctx) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
251 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
252 if (ctx->fops->fputs != NULL) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
253 return ctx->fops->fputs(ptr, ctx); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
254 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
255 const char *p = ptr; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
256 int retv = 0; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
257 while (*p && (retv = ctx->fops->fputc(*p, ctx)) != EOF) p++; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
258 return retv; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
259 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
260 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
261 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
262 int thvfprintf(th_ioctx *ctx, const char *fmt, va_list ap) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
263 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
264 if (ctx->fops->vfprintf != NULL) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
265 return ctx->fops->vfprintf(ctx, fmt, ap); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
266 else |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
267 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
268 char *msg = th_strdup_printf(fmt, ap); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
269 int rval = thfputs(msg, ctx); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
270 th_free(msg); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
271 return rval; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
272 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
273 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
274 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
275 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
276 int thfprintf(th_ioctx *ctx, const char *fmt, ...) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
277 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
278 int retv; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
279 va_list ap; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
280 va_start(ap, fmt); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
281 retv = thvfprintf(ctx, fmt, ap); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
282 va_end(ap); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
283 return retv; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
284 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
285 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
286 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
287 BOOL thfread_str(th_ioctx *ctx, void *ptr, const size_t len) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
288 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
289 return (thfread(ptr, sizeof(uint8_t), len, ctx) == len); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
290 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
291 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
292 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
293 BOOL thfread_u8(th_ioctx *ctx, uint8_t *val) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
294 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
295 return (thfread(val, sizeof(uint8_t), 1, ctx) == 1); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
296 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
297 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
298 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
299 BOOL thfwrite_str(th_ioctx *ctx, const void *ptr, const size_t len) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
300 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
301 return (thfwrite(ptr, sizeof(uint8_t), len, ctx) == len); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
302 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
303 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
304 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
305 BOOL thfwrite_u8(th_ioctx *ctx, const uint8_t val) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
306 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
307 return (thfwrite(&val, sizeof(uint8_t), 1, ctx) == 1); |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
311 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
312 // File routines for endian-dependant data |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
313 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
314 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
315 BOOL thfread_ ## xname (th_ioctx *ctx, xtype *v) \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
316 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
317 xtype result; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
318 if (thfread(&result, sizeof( xtype ), 1, ctx) != 1) \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
319 return FALSE; \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
320 *v = TH_ ## xmacro ## _TO_NATIVE (result); \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
321 return TRUE; \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
322 } \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
323 \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
324 BOOL thfwrite_ ## xname (th_ioctx *ctx, const xtype v) \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
325 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
326 xtype result = TH_NATIVE_TO_ ## xmacro (v); \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
327 if (thfwrite(&result, sizeof( xtype ), 1, ctx) != 1) \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
328 return FALSE; \ |
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
329 return TRUE; \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
330 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
331 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
332 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
333 TH_DEFINE_FUNC(le16, uint16_t, LE16) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
334 TH_DEFINE_FUNC(le32, uint32_t, LE32) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
335 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
336 TH_DEFINE_FUNC(be16, uint16_t, BE16) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
337 TH_DEFINE_FUNC(be32, uint32_t, BE32) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
338 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
339 TH_DEFINE_FUNC(le64, uint64_t, LE64) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
340 TH_DEFINE_FUNC(be64, uint64_t, BE64) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
341 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
342 #undef TH_DEFINE_FUNC |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
343 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
344 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
345 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
346 // stdio wrappers for I/O contexts |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
347 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
348 #define CTX_FH ((FILE *) ctx->data) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
349 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
350 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
351 static int th_stdio_fopen(th_ioctx *ctx) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
352 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
353 ctx->data = (void *) fopen(ctx->filename, ctx->mode); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
354 ctx->status = th_get_error(); |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
355 return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
356 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
357 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
358 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
359 static void th_stdio_fclose(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
360 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
361 if (CTX_FH != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
362 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
363 fclose(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
364 ctx->data = NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
365 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
366 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
367 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
368 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
369 static int th_stdio_ferror(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
370 { |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
371 return ctx->status; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
372 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
373 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
374 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
375 static off_t th_stdio_ftell(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
376 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
377 return ftello(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
378 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
379 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
380 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
381 static int th_stdio_fseek(th_ioctx *ctx, const off_t pos, const int whence) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
382 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
383 int ret = fseeko(CTX_FH, pos, whence); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
384 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
385 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
386 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
387 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
388 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
389 static int th_stdio_freset(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
390 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
391 if (CTX_FH != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
392 return th_stdio_fseek(ctx, 0, SEEK_SET); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
393 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
394 return THERR_OK; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
395 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
396 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
398 static off_t th_stdio_fsize(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
399 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
400 off_t savePos, fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
401 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
402 // Check if the size is cached |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
403 if (ctx->size != 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
404 return ctx->size; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
405 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
406 // Get file size |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
407 if ((savePos = th_stdio_ftell(ctx)) < 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
408 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
409 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
410 if (th_stdio_fseek(ctx, 0, SEEK_END) != 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
411 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
412 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
413 if ((fileSize = th_stdio_ftell(ctx)) < 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
414 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
415 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
416 if (th_stdio_fseek(ctx, savePos, SEEK_SET) != 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
417 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
418 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
419 ctx->size = fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
420 return fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
421 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
422 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
423 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
424 static BOOL th_stdio_feof(th_ioctx *ctx) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
425 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
426 return feof(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
427 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
428 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
429 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
430 static int th_stdio_fgetc(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
431 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
432 int ret = fgetc(CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
433 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
434 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
435 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
436 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
437 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
438 static int th_stdio_fputc(int v, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
439 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
440 int ret = fputc(v, CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
441 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
442 return ret; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 } |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
444 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
445 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
446 static size_t th_stdio_fread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
447 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
448 size_t ret = fread(ptr, size, nmemb, CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
449 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
450 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
451 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
452 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
453 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
454 static size_t th_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
455 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
456 size_t ret = fwrite(ptr, size, nmemb, CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
457 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
458 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
459 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
460 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
461 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
462 static char * th_stdio_fgets(char *str, int size, th_ioctx *ctx) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
463 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
464 char *ret = fgets(str, size, CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
465 ctx->status = th_get_error(); |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
466 return ret; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
467 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
468 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
469 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
470 static int th_stdio_fputs(const char *str, th_ioctx *ctx) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
471 { |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
472 int ret = fputs(str, CTX_FH); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
473 ctx->status = th_get_error(); |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
474 return ret; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
475 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
476 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
477 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
478 static int th_stdio_vfprintf(th_ioctx *ctx, const char *fmt, va_list ap) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
479 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
480 int ret = vfprintf(CTX_FH, fmt, ap); |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
481 ctx->status = th_get_error(); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
482 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
483 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
484 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
485 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
486 const th_ioctx_ops th_stdio_io_ops = |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
487 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
488 "stdio", |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
489 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
490 th_stdio_fopen, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
491 th_stdio_fclose, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
492 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
493 th_stdio_freset, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
494 th_stdio_ferror, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
495 th_stdio_fseek, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
496 th_stdio_fsize, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
497 th_stdio_ftell, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
498 th_stdio_feof, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
499 th_stdio_fgetc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
500 th_stdio_fputc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
501 th_stdio_fread, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
502 th_stdio_fwrite, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
503 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
504 th_stdio_fgets, |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
505 th_stdio_fputs, |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
506 th_stdio_vfprintf, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
507 }; |