Mercurial > hg > th-libs
annotate th_ioctx.c @ 675:fb4093ad1f7b
Add MemIO ioctx functionality.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 25 Feb 2020 06:15:08 +0200 |
parents | 24cbab6e88c6 |
children | 7e207f1023d9 |
rev | line source |
---|---|
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
211 | 2 * Simple I/O abstraction and context handling layer |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
553 | 4 * (C) Copyright 2012-2020 Tecnic Software productions (TNSP) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_ioctx.h" |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
10 #include "th_endian.h" |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
11 #include <stdio.h> |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
72
43df05a632cb
Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
13 |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
14 static void th_io_update_atime(th_ioctx *ctx) |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
15 { |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
16 ctx->atime = time(NULL); |
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
17 } |
460
0a1a65503e0b
Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
18 |
0a1a65503e0b
Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
19 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
20 static void th_io_init(th_ioctx *ctx) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
21 { |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
22 memset(ctx, 0, sizeof(th_ioctx)); |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
23 ctx->line = 1; |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
24 } |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
25 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
26 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
27 void th_io_init_stdio(th_ioctx *ctx, FILE *fh) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
28 { |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
29 th_io_init(ctx); |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
30 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
31 ctx->fops = &th_stdio_io_ops; |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
32 ctx->data = (void *) fh; |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
33 } |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
34 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
35 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
36 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
|
37 { |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
38 th_ioctx *ctx = th_malloc(sizeof(th_ioctx)); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
39 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
40 return NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
41 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
42 th_io_init(ctx); |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
43 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
44 ctx->allocated = TRUE; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
45 ctx->fops = fops; |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
46 |
209
462b837ea492
Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
47 ctx->filename = th_strdup(filename); |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
48 ctx->fallocated = TRUE; |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
49 if (filename != NULL && ctx->filename == NULL) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
50 goto err; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
51 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
52 return ctx; |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
53 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
54 err: |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
55 th_io_close(ctx); |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
56 return NULL; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
57 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
58 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 |
209
462b837ea492
Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
60 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
|
61 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
62 if (ctx == NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
63 return THERR_NULLPTR; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
64 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
65 if (mode != NULL && (ctx->mode = th_strdup(mode)) == NULL) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
66 return THERR_MALLOC; |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
67 |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
68 ctx->mallocated = TRUE; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
69 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
70 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
|
71 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
|
72 |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
73 return ctx->status; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
77 int th_io_fopen(th_ioctx **pctx, const th_ioctx_ops *fops, const char *filename, const char *mode) |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
78 { |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
79 th_ioctx *ctx; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
80 int res; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
81 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
82 if ((*pctx = ctx = th_io_new(fops, filename)) == NULL) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
83 return THERR_MALLOC; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
84 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
85 if ((res = th_io_open(ctx, mode)) != THERR_OK) |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
86 goto err; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
87 |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
88 return THERR_OK; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
89 |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
90 err: |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
91 th_io_close(ctx); |
477
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
92 *pctx = NULL; |
96d137a6b392
Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents:
461
diff
changeset
|
93 return res; |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
94 } |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
95 |
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
96 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
97 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
|
98 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
99 if (ctx != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
100 { |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
101 if (ctx->fops != NULL && ctx->fops->fclose != NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
102 ctx->fops->fclose(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
103 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
104 if (ctx->fallocated) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
105 th_free_r(&ctx->filename); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
106 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
107 if (ctx->mallocated) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
108 th_free_r(&ctx->mode); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
109 |
650
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
110 if (ctx->allocated) |
24cbab6e88c6
Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents:
556
diff
changeset
|
111 th_free(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
112 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
113 } |
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 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
120 if (ctx == NULL) |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
121 return FALSE; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
123 ctx->error = error; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
124 ctx->msg = msg; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
126 return TRUE; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
519
e1b15fb56ddf
Change return value of th_io_error() and th_io_error_v() to int, and return the error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
130 int 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
|
131 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
132 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
|
133 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 if (ctx->error != NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 th_free(msg); |
519
e1b15fb56ddf
Change return value of th_io_error() and th_io_error_v() to int, and return the error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
140 return err; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 |
519
e1b15fb56ddf
Change return value of th_io_error() and th_io_error_v() to int, and return the error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
144 int th_io_error(th_ioctx *ctx, const int err, const char *fmt, ...) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
145 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
146 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
147 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
148 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
|
149 va_end(ap); |
519
e1b15fb56ddf
Change return value of th_io_error() and th_io_error_v() to int, and return the error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
150 return err; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
151 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
152 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
153 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
154 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
|
155 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
156 if (ctx->msg != NULL) |
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 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
|
159 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
|
160 th_free(msg); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
161 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
162 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
163 THMSG_V(level, fmt, ap); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
164 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
165 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
166 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
167 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
|
168 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
169 va_list ap; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
170 va_start(ap, fmt); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
171 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
|
172 va_end(ap); |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
175 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
176 int thfreset(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
177 { |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 if (ctx == NULL) |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
179 return THERR_NULLPTR; |
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 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
|
182 return THERR_OK; |
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 return ctx->fops->freset(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
185 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
186 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
187 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
188 int thferror(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
189 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
190 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
191 return ctx->fops->ferror(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
192 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
193 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
194 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
195 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
|
196 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
197 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
198 return ctx->fops->fseek(ctx, offset, whence); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
199 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
200 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
201 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
202 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
|
203 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
204 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
205 return ctx->fops->fsize(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
206 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
207 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
208 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
209 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
|
210 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
211 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
212 return ctx->fops->ftell(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
213 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
214 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
215 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
216 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
|
217 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
218 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
219 return ctx->fops->feof(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
220 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
221 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
222 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
223 int thfgetc(th_ioctx *ctx) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
224 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
225 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
226 return ctx->fops->fgetc(ctx); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
227 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
228 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
229 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
230 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
|
231 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
232 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
233 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
|
234 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
237 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
|
238 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
239 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
240 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
|
241 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
242 |
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 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
|
245 { |
461
04320ca79407
Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents:
460
diff
changeset
|
246 th_io_update_atime(ctx); |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
247 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
|
248 } |
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 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
|
252 { |
206 | 253 char *ptr = str, *end = str + size - 1; |
254 int c; | |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
255 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
256 if (size <= 0) |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 return NULL; |
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 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
|
260 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
261 *ptr++ = c; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
262 if (c == '\n') |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
263 break; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
264 } |
206 | 265 *ptr = 0; |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
266 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
267 return (ptr > str) ? str : NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
268 } |
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 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
271 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
|
272 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
273 if (ctx->fops->fputs != NULL) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
274 return ctx->fops->fputs(ptr, ctx); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
275 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
276 const char *p = ptr; |
675
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
277 int rval = 0; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
278 while (*p && (rval = ctx->fops->fputc(*p, ctx)) != EOF) p++; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
279 return rval; |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
280 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
281 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
282 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
283 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
|
284 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
285 if (ctx->fops->vfprintf != NULL) |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
286 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
|
287 else |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
288 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
289 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
|
290 int rval = thfputs(msg, ctx); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
291 th_free(msg); |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
292 return rval; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
293 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
294 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
295 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
296 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
297 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
|
298 { |
675
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
299 int rval; |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
300 va_list ap; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
301 va_start(ap, fmt); |
675
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
302 rval = thvfprintf(ctx, fmt, ap); |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
303 va_end(ap); |
675
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
304 return rval; |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
305 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
306 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
307 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
308 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
|
309 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
310 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
|
311 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
312 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
313 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
314 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
|
315 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
316 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
|
317 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
318 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
319 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
320 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
|
321 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
322 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
|
323 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
324 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
325 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
326 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
|
327 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
328 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
|
329 } |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 |
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
332 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
333 // 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
|
334 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
335 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
336 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
|
337 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
338 xtype result; \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
339 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
|
340 return FALSE; \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
341 *v = TH_ ## xmacro ## _TO_NATIVE (result); \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
342 return TRUE; \ |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
343 } \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
344 \ |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
345 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
|
346 { \ |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
347 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
|
348 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
|
349 return FALSE; \ |
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
350 return TRUE; \ |
202
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
354 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
|
355 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
|
356 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
357 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
|
358 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
|
359 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
360 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
|
361 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
|
362 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
363 #undef TH_DEFINE_FUNC |
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 |
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 // 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
|
368 // |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
369 #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
|
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 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
|
373 { |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
374 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
|
375 ctx->status = th_get_error(); |
204
55f429dff750
Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
376 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
|
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
380 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
|
381 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
382 if (CTX_FH != NULL) |
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 fclose(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
385 ctx->data = NULL; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
386 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
387 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
388 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
389 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
390 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
|
391 { |
219
faeeac291a6c
Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents:
218
diff
changeset
|
392 return ctx->status; |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
396 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
|
397 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
398 return ftello(CTX_FH); |
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 |
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 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
|
403 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
404 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
|
405 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
|
406 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
407 } |
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 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
|
411 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
412 if (CTX_FH != NULL) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
413 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
|
414 else |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
415 return THERR_OK; |
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 |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
419 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
|
420 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
421 off_t savePos, fileSize; |
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 // 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
|
424 if (ctx->size != 0) |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
425 return ctx->size; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
426 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
427 // Get file size |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
428 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
|
429 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
430 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
431 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
|
432 return -1; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
433 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
434 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
|
435 return -1; |
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 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
|
438 return -1; |
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 ctx->size = fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
441 return fileSize; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
442 } |
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 |
457
85fa3d333556
Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents:
454
diff
changeset
|
445 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
|
446 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
447 return feof(CTX_FH); |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
448 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
449 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
450 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
451 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
|
452 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
453 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
|
454 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
|
455 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
456 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
457 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
458 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
459 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
|
460 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
461 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
|
462 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
|
463 return ret; |
67
d94af48cef7b
Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
464 } |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
465 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
466 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
467 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
|
468 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
469 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
|
470 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
|
471 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
472 } |
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 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
475 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
|
476 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
477 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
|
478 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
|
479 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
480 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
481 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
482 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
483 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
|
484 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
485 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
|
486 ctx->status = th_get_error(); |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
487 return ret; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
488 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
489 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
490 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
491 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
|
492 { |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
493 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
|
494 ctx->status = th_get_error(); |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
495 return ret; |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
496 } |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
497 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
498 |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
499 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
|
500 { |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
501 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
|
502 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
|
503 return ret; |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
504 } |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
505 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
506 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
507 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
|
508 { |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
509 "stdio", |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
510 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
511 th_stdio_fopen, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
512 th_stdio_fclose, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
513 |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
514 th_stdio_freset, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
515 th_stdio_ferror, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
516 th_stdio_fseek, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
517 th_stdio_fsize, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
518 th_stdio_ftell, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
519 th_stdio_feof, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
520 th_stdio_fgetc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
521 th_stdio_fputc, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
522 th_stdio_fread, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
523 th_stdio_fwrite, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
524 |
208
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
525 th_stdio_fgets, |
3635415a2d03
More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
526 th_stdio_fputs, |
202
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
527 th_stdio_vfprintf, |
b392293047da
Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
528 }; |
675
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
529 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
530 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
531 static BOOL th_mem_realloc(th_ioctx *ctx, const size_t newSize) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
532 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
533 size_t grow; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
534 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
535 if (ctx->maxSize > 0 && newSize > ctx->maxSize) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
536 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
537 ctx->status = THERR_BOUNDS; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
538 return FALSE; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
539 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
540 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
541 if (newSize < ctx->memAlloc) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
542 return TRUE; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
543 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
544 grow = (ctx->minAlloc > 0) ? ctx->minAlloc : 8 * 1024; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
545 if (newSize - ctx->memAlloc > grow) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
546 grow += newSize - ctx->memAlloc; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
547 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
548 if (ctx->maxSize > 0 && ctx->memAlloc + grow >= ctx->maxSize) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
549 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
550 ctx->status = THERR_BOUNDS; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
551 return FALSE; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
552 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
553 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
554 ctx->memAlloc += grow; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
555 if ((ctx->memData = th_realloc(ctx->memData, ctx->memAlloc)) == NULL) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
556 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
557 ctx->status = THERR_MALLOC; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
558 return FALSE; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
559 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
560 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
561 ctx->memSize = newSize; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
562 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
563 return TRUE; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
564 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
565 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
566 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
567 static int th_mem_freset(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
568 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
569 ctx->memOffset = 0; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
570 return THERR_OK; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
571 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
572 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
573 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
574 static int th_mem_ferror(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
575 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
576 return ctx->status; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
577 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
578 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
579 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
580 static int th_mem_fseek(th_ioctx *ctx, const off_t offset, const int whence) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
581 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
582 off_t newPos; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
583 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
584 // Calculate the new position |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
585 switch (whence) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
586 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
587 case SEEK_SET: |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
588 newPos = offset; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
589 break; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
590 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
591 case SEEK_CUR: |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
592 newPos = ctx->memOffset + offset; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
593 break; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
594 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
595 case SEEK_END: |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
596 newPos = ctx->memSize + offset; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
597 break; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
598 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
599 default: |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
600 return -1; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
601 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
602 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
603 // Set the new position |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
604 ctx->memOffset = newPos; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
605 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
606 // Check the new position |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
607 if (newPos < 0) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
608 return -1; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
609 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
610 //if (!th_mem_realloc(ctx, newPos)) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
611 // return -1; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
612 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
613 return 0; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
614 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
615 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
616 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
617 static off_t th_mem_fsize(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
618 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
619 return ctx->memSize; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
620 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
621 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
622 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
623 static off_t th_mem_ftell(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
624 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
625 return ctx->memOffset; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
626 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
627 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
628 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
629 static BOOL th_mem_feof(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
630 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
631 return ((size_t) ctx->memOffset) >= ctx->memSize; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
632 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
633 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
634 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
635 static int th_mem_fgetc(th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
636 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
637 // Check for EOF |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
638 if ((size_t) ctx->memOffset < ctx->memSize) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
639 return ctx->memData[ctx->memOffset++]; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
640 else |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
641 return EOF; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
642 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
643 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
644 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
645 static size_t th_mem_fread(void *buf, size_t size, size_t nmemb, th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
646 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
647 size_t length = size * nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
648 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
649 // Check if we can read the whole chunk |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
650 if (((size_t) ctx->memOffset + length) >= ctx->memSize) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
651 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
652 nmemb = (ctx->memSize - ctx->memOffset) / size; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
653 length = size * nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
654 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
655 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
656 memcpy(buf, ctx->memData + ctx->memOffset, length); |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
657 ctx->memOffset += length; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
658 return nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
659 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
660 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
661 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
662 static int th_mem_fputc(int ch, th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
663 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
664 // Check for EOF |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
665 if (!th_mem_realloc(ctx, ctx->memOffset + 1)) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
666 return EOF; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
667 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
668 ctx->memData[ctx->memOffset++] = ch; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
669 return ch; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
670 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
671 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
672 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
673 static size_t th_mem_fwrite(const void *buf, size_t size, size_t nmemb, th_ioctx *ctx) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
674 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
675 size_t length = size * nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
676 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
677 // Check if we can write the whole chunk |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
678 if (!th_mem_realloc(ctx, ctx->memOffset + length)) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
679 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
680 nmemb = (ctx->memSize - ctx->memOffset) / size; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
681 length = size * nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
682 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
683 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
684 if (length > 0) |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
685 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
686 memcpy(ctx->memData + ctx->memOffset, buf, length); |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
687 ctx->memOffset += length; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
688 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
689 return nmemb; |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
690 } |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
691 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
692 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
693 const th_ioctx_ops th_mem_io_ops = |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
694 { |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
695 "MemIO", |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
696 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
697 NULL, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
698 NULL, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
699 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
700 th_mem_freset, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
701 th_mem_ferror, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
702 th_mem_fseek, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
703 th_mem_fsize, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
704 th_mem_ftell, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
705 th_mem_feof, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
706 th_mem_fgetc, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
707 th_mem_fputc, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
708 th_mem_fread, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
709 th_mem_fwrite, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
710 |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
711 NULL, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
712 NULL, |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
713 NULL |
fb4093ad1f7b
Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
650
diff
changeset
|
714 }; |