annotate th_ioctx.c @ 457:85fa3d333556

Actually, revert the boolean changes .. meh.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Jan 2018 23:09:29 +0200
parents 347bfd3e017e
children 0a1a65503e0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
211
a2b2106943a8 Update header comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 209
diff changeset
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
347bfd3e017e Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
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
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
14 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
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;
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
21 ctx->filename = th_strdup(filename);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
22
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
23 return ctx;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
24 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
25
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
27 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
28 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
29 if (ctx == NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
30 return THERR_NULLPTR;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
31
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)
219
faeeac291a6c Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents: 218
diff changeset
35 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
36
219
faeeac291a6c Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents: 218
diff changeset
37 return ctx->status;
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 {
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
43 th_ioctx *ctx = th_io_new(fops, filename);
204
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
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
47 if (th_io_open(ctx, mode) != THERR_OK)
204
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
81 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
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)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
86 return FALSE;
67
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
91 return TRUE;
67
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
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
102 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
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
179 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
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
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
216 char *ptr = str, *end = str + size - 1;
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
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
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
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
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
234 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
235 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
236 if (ctx->fops->fputs != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
237 return ctx->fops->fputs(ptr, ctx);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
238
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
239 const char *p = ptr;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
240 int retv = 0;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
241 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
242 return retv;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
243 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
244
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
245
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
246 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
247 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
248 if (ctx->fops->vfprintf != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
249 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
250 else
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 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
253 int rval = thfputs(msg, ctx);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
254 th_free(msg);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
255 return rval;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
256 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
257 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
258
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 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
261 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
262 int retv;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
263 va_list ap;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
264 va_start(ap, fmt);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
265 retv = thvfprintf(ctx, fmt, ap);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
266 va_end(ap);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
267 return retv;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
268 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
269
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
270
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
271 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
272 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
273 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
274 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
275
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
276
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
277 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
278 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
279 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
280 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
281
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
282
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
283 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
284 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
285 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
286 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
287
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
288
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
289 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
290 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
291 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
292 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
295 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
296 // 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
297 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
298 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
299 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
300 { \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
301 xtype result; \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
302 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
303 return FALSE; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
304 *v = TH_ ## xmacro ## _TO_NATIVE (result); \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
305 return TRUE; \
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 \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
308 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
309 { \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
310 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
311 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
312 return FALSE; \
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
313 return TRUE; \
202
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 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
318 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
319
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
320 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
321 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
322
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
323 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
324 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
325
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
326 #undef TH_DEFINE_FUNC
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 // 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
331 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
332 #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
333
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
334
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
335 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
336 {
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
337 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
338 ctx->status = th_get_error();
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
339 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
340 }
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
343 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
344 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
345 if (CTX_FH != NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
346 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
347 fclose(CTX_FH);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
348 ctx->data = NULL;
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
352
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
353 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
354 {
219
faeeac291a6c Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents: 218
diff changeset
355 return ctx->status;
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 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
360 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
361 return ftello(CTX_FH);
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
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 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
366 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
367 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
368 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
369 return ret;
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 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
374 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
375 if (CTX_FH != NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
376 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
377 else
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
378 return THERR_OK;
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
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
382 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
383 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
384 off_t savePos, fileSize;
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 // 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
387 if (ctx->size != 0)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
388 return ctx->size;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
389
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
390 // Get file size
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
391 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
392 return -1;
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 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
395 return -1;
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 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
398 return -1;
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 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
401 return -1;
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 ctx->size = fileSize;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
404 return fileSize;
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
407
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
408 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
409 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
410 return feof(CTX_FH);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
411 }
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
414 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
415 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
416 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
417 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
418 return ret;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
419 }
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
422 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
423 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
424 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
425 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
426 return ret;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 }
202
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 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
431 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
432 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
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 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
439 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
440 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
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;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
443 }
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
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
446 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
447 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
448 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
449 ctx->status = th_get_error();
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
450 return ret;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
451 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
452
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
453
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
454 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
455 {
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
456 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
457 ctx->status = th_get_error();
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
458 return ret;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
459 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
460
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
461
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
462 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
463 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
464 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
465 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
466 return ret;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
467 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
468
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
469
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
470 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
471 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
472 "stdio",
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
473
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
474 th_stdio_fopen,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
475 th_stdio_fclose,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
476
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
477 th_stdio_freset,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
478 th_stdio_ferror,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
479 th_stdio_fseek,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
480 th_stdio_fsize,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
481 th_stdio_ftell,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
482 th_stdio_feof,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
483 th_stdio_fgetc,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
484 th_stdio_fputc,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
485 th_stdio_fread,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
486 th_stdio_fwrite,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
487
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
488 th_stdio_fgets,
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
489 th_stdio_fputs,
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
490 th_stdio_vfprintf,
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
491 };
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
492