Mercurial > hg > th-libs
annotate th_ioctx.c @ 206:c2193323736d
Fix thfgets().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 12 Feb 2016 00:16:49 +0200 |
parents | 55f429dff750 |
children | 3635415a2d03 |
rev | line source |
---|---|
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Standard I/O context helpers |
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 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
4 * (C) Copyright 2012,2016 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 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
14 th_ioctx *th_io_new(const th_ioctx_ops *fops) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
16 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
|
17 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
18 return NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
19 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
20 ctx->fops = fops; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
21 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
22 return ctx; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
23 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
24 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
26 int th_io_open(th_ioctx *ctx, const char *filename, const char *mode) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
27 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
28 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
29 return THERR_NULLPTR; |
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 ctx->filename = th_strdup(filename); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
32 ctx->mode = th_strdup(mode); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
33 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
34 if (ctx->fops->fopen != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
35 ctx->errno = ctx->fops->fopen(ctx); |
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 return ctx->errno; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
41 th_ioctx * th_io_fopen(const th_ioctx_ops *fops, const char *filename, const char *mode) |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
42 { |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
43 th_ioctx *ctx = th_io_new(fops); |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
44 if (ctx == NULL) |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
45 return NULL; |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
46 |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
47 if (th_io_open(ctx, filename, mode) != THERR_OK) |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
48 { |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
49 th_io_free(ctx); |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
50 return NULL; |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
51 } |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
52 |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
53 return ctx; |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
54 } |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
55 |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
56 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
57 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
|
58 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
59 if (ctx != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
60 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
61 if (ctx->fops->fclose != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
62 ctx->fops->fclose(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
63 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
64 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
65 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
66 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
67 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
|
68 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
69 if (ctx != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
70 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
71 th_io_close(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
72 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
73 th_free_r(&ctx->filename); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
74 th_free_r(&ctx->mode); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
75 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
76 th_free(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
77 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
78 } |
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 BOOL th_io_set_handlers(th_ioctx *ctx, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
82 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
|
83 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
|
84 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
85 if (ctx == NULL) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 return FALSE; |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
88 ctx->error = error; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
89 ctx->msg = msg; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 return TRUE; |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
95 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
|
96 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
97 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
|
98 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 if (ctx->error != NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
100 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
|
101 else |
166
28fd04f43a95
Use TH_PRIu_SIZE_T for size_t printing.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
102 THERR("'%s' #%" TH_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
|
103 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 th_free(msg); |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
108 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
|
109 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
110 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
111 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
112 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
|
113 va_end(ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
114 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
115 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
116 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
117 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
|
118 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
119 if (ctx->msg != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
120 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
121 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
|
122 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
|
123 th_free(msg); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
124 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
125 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
126 THMSG_V(level, fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
127 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
128 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
129 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
130 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
|
131 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
132 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
133 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
134 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
|
135 va_end(ap); |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
138 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
139 int thfreset(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
140 { |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 if (ctx == NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
142 return THERR_NULLPTR; |
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 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
|
145 return THERR_OK; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
146 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
147 return ctx->fops->freset(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
148 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
149 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
150 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
151 int thferror(th_ioctx *ctx) |
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 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
154 return ctx->fops->ferror(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
155 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
156 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
157 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
158 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
|
159 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
160 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
161 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
|
162 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
163 |
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 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
|
166 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
167 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
168 return ctx->fops->fsize(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
169 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
170 |
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 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
|
173 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
174 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
175 return ctx->fops->ftell(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
176 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
177 |
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 BOOL thfeof(th_ioctx *ctx) |
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 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
182 return ctx->fops->feof(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
183 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
184 |
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 int thfgetc(th_ioctx *ctx) |
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 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
189 return ctx->fops->fgetc(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
190 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
191 |
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 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
|
194 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
195 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
196 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
|
197 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
198 |
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 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
|
201 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
202 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
203 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
|
204 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
205 |
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 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
|
208 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
209 ctx->atime = time(NULL); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
210 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
|
211 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
212 |
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 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
|
215 { |
206 | 216 char *ptr = str, *end = str + size - 1; |
217 int c; | |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
218 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
219 if (size <= 0) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 return NULL; |
202
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 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
|
223 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
224 *ptr++ = c; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
225 if (c == '\n') |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
226 break; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
227 } |
206 | 228 *ptr = 0; |
202
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 return (ptr > str) ? str : NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
231 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
232 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
233 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
234 BOOL thfread_str(th_ioctx *ctx, void *ptr, const size_t len) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
235 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
236 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
|
237 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
238 |
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 BOOL thfread_byte(th_ioctx *ctx, uint8_t *val) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
241 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
242 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
|
243 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
244 |
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 BOOL thfwrite_str(th_ioctx *ctx, const void *ptr, const size_t len) |
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 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
|
249 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
250 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
251 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
252 BOOL thfwrite_byte(th_ioctx *ctx, const uint8_t val) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
253 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
254 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
|
255 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
258 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
259 // 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
|
260 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
261 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
262 BOOL thfread_ ## xname (th_ioctx *ctx, xtype *v) \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
263 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
264 xtype result; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
265 if (thfread(&result, sizeof( xtype ), 1, ctx) != 1) \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
266 return FALSE; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
267 *v = TH_ ## xmacro ## _TO_NATIVE (result); \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
268 return TRUE; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
269 } \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
270 \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
271 BOOL thfwrite_ ## xname (th_ioctx *ctx, const xtype v) \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
272 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
273 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
|
274 if (thfwrite(&result, sizeof( xtype ), 1, ctx) != 1) \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
275 return FALSE; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
276 return TRUE; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
277 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
278 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
279 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
280 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
|
281 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
|
282 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
283 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
|
284 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
|
285 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
286 #ifdef TH_HAVE_64BIT |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
287 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
|
288 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
|
289 #endif |
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 #undef TH_DEFINE_FUNC |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
292 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
293 |
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 // 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
|
296 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
297 #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
|
298 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
299 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
300 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
|
301 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
302 ctx->data = (void *) fopen(ctx->filename, ctx->mode); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
303 ctx->errno = th_get_error(); |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
304 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
|
305 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
308 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
|
309 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
310 if (CTX_FH != NULL) |
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 fclose(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
313 ctx->data = NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
314 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
315 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
318 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
|
319 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
320 return ctx->errno; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
321 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
324 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
|
325 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
326 return ftello(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
327 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
328 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
329 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
330 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
|
331 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
332 int ret = fseeko(CTX_FH, pos, whence); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
333 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
334 return ret; |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
337 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
338 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
|
339 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
340 if (CTX_FH != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
341 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
|
342 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
343 return THERR_OK; |
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 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
346 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
347 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
|
348 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
349 off_t savePos, fileSize; |
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 // 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
|
352 if (ctx->size != 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
353 return ctx->size; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
354 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
355 // Get file size |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
356 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
|
357 return -1; |
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 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
|
360 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
361 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
362 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
|
363 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
364 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
365 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
|
366 return -1; |
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 ctx->size = fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
369 return fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
370 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
371 |
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 static BOOL th_stdio_feof(th_ioctx *ctx) |
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 return feof(CTX_FH); |
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 |
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 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
|
380 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
381 int ret = fgetc(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
382 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
383 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
384 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
385 |
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 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
|
388 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
389 int ret = fputc(v, CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
390 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
391 return ret; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
392 } |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
393 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
394 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
395 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
|
396 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
397 size_t ret = fread(ptr, size, nmemb, CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
398 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
399 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
400 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
403 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
|
404 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
405 size_t ret = fwrite(ptr, size, nmemb, CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
406 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
407 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
408 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
411 static int th_stdio_vfprintf(th_ioctx *ctx, const char *format, va_list ap) |
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 int ret = vfprintf(CTX_FH, format, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
414 ctx->errno = th_get_error(); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
415 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
416 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
417 |
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 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
|
420 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
421 "stdio", |
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 th_stdio_fopen, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
424 th_stdio_fclose, |
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 th_stdio_freset, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
427 th_stdio_ferror, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
428 th_stdio_fseek, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
429 th_stdio_fsize, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
430 th_stdio_ftell, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
431 th_stdio_feof, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
432 th_stdio_fgetc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
433 th_stdio_fputc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
434 th_stdio_fread, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
435 th_stdio_fwrite, |
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 th_stdio_vfprintf, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
438 }; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
439 |