annotate th_ioctx.c @ 686:dfc2c9f0577f

Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Mar 2020 00:16:30 +0200
parents 7e207f1023d9
children 49c818acbbbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
211
a2b2106943a8 Update header comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 209
diff changeset
2 * Simple I/O abstraction and context handling layer
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
553
3a852e9f70a6 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 519
diff changeset
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"
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
12
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
13 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
14 {
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
15 ctx->atime = time(NULL);
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
16 }
460
0a1a65503e0b Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
17
0a1a65503e0b Use a macro for updating atime.
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
18
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
19 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
20 {
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
21 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
22 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
23 }
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 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
27 {
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
28 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
29
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
30 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
31 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
32 }
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
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
35 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
36 {
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
37 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
38 if (ctx == NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
39 return NULL;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
40
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
41 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
42
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
43 ctx->allocated = TRUE;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
44 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
45
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
46 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
47 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
48 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
49 goto err;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
50
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
51 return ctx;
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
52
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
53 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
54 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
55 return NULL;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
56 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
57
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
209
462b837ea492 Change io context API. Again.
Matti Hamalainen <ccr@tnsp.org>
parents: 208
diff changeset
59 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
60 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
61 if (ctx == NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
62 return THERR_NULLPTR;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
63
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
64 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
65 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
66
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
67 ctx->mallocated = TRUE;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
68
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
69 ctx->status = thfopen(ctx);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
70
219
faeeac291a6c Oops, using "errno" as io_ctx struct member causes problems on some
Matti Hamalainen <ccr@tnsp.org>
parents: 218
diff changeset
71 return ctx->status;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
75 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
76 {
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
77 th_ioctx *ctx;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
78 int res;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
79
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
80 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
81 return THERR_MALLOC;
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
82
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
83 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
84 goto err;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
85
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
86 return THERR_OK;
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
87
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
88 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
89 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
90 *pctx = NULL;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
91 return res;
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
92 }
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
93
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
94
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
95 void th_io_close(th_ioctx *ctx)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
96 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
97 if (ctx != NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
98 {
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
99 thfclose(ctx);
202
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->fallocated)
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
102 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
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->mallocated)
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->mode);
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->allocated)
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(ctx);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
109 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
110 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
111
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
112
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
113 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
114 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
115 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
116 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
117 if (ctx == NULL)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
118 return FALSE;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
120 ctx->error = error;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
121 ctx->msg = msg;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
123 return TRUE;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
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
127 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
128 {
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
129 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
130
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 if (ctx->error != NULL)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
132 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
133 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
134 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
135
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 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
137 return err;
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
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
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
141 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
142 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
143 va_list ap;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
144 va_start(ap, fmt);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
145 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
146 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
147 return err;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
148 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
149
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
150
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
151 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
152 {
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
153 if (ctx->msg != NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
154 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
155 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
156 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
157 th_free(msg);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
158 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
159 else
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
160 THMSG_V(level, fmt, ap);
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
163
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
164 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
165 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
166 va_list ap;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
167 va_start(ap, fmt);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
168 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
169 va_end(ap);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
170 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
171
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
172
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
173 int thfopen(th_ioctx *ctx)
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
174 {
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
175 if (ctx == NULL)
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
176 return THERR_NULLPTR;
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
177
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
178 if (ctx->fops == NULL || ctx->fops->fopen == NULL)
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
179 return THERR_OK;
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
180
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
181 return ctx->fops->fopen(ctx);
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
182 }
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
183
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
184
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
185 void thfclose(th_ioctx *ctx)
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
186 {
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
187 if (ctx != NULL &&
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
188 ctx->fops == NULL &&
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
189 ctx->fops->fclose == NULL)
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
190 ctx->fops->fclose(ctx);
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
191 }
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
192
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
193
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
194 int thfreset(th_ioctx *ctx)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
195 {
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 if (ctx == NULL)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
197 return THERR_NULLPTR;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
198
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
199 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
200 return THERR_OK;
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 return ctx->fops->freset(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
203 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
204
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
205
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
206 int thferror(th_ioctx *ctx)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
207 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
208 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
209 return ctx->fops->ferror(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
210 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
211
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
212
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
213 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
214 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
215 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
216 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
217 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
218
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
219
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
220 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
221 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
222 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
223 return ctx->fops->fsize(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
224 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
225
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
226
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
227 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
228 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
229 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
230 return ctx->fops->ftell(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
231 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
232
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
233
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
234 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
235 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
236 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
237 return ctx->fops->feof(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
238 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
239
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
240
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
241 int thfgetc(th_ioctx *ctx)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
242 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
243 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
244 return ctx->fops->fgetc(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
245 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
246
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
247
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
248 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
249 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
250 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
251 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
252 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
253
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
254
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
255 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
256 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
257 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
258 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
259 }
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
262 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
263 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
264 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
265 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
266 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
267
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 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
270 {
206
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
271 char *ptr = str, *end = str + size - 1;
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
272 int c;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
273
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
274 if (size <= 0)
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 return NULL;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
276
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
277 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
278 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
279 *ptr++ = c;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
280 if (c == '\n')
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
281 break;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
282 }
206
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
283 *ptr = 0;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
284
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
285 return (ptr > str) ? str : NULL;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
286 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
287
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
288
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
289 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
290 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
291 if (ctx->fops->fputs != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
292 return ctx->fops->fputs(ptr, ctx);
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 const char *p = ptr;
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
295 int rval = 0;
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
296 while (*p && (rval = ctx->fops->fputc(*p, ctx)) != EOF) p++;
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
297 return rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
298 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
299
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
300
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
301 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
302 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
303 if (ctx->fops->vfprintf != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
304 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
305 else
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 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
308 int rval = thfputs(msg, ctx);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
309 th_free(msg);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
310 return rval;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
311 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
312 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
313
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
314
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
315 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
316 {
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
317 int rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
318 va_list ap;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
319 va_start(ap, fmt);
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
320 rval = thvfprintf(ctx, fmt, ap);
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
321 va_end(ap);
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
322 return rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
323 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
324
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
325
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
326 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
327 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
328 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
329 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
330
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
331
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
332 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
333 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
334 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
335 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
336
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
337
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
338 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
339 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
340 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
341 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
342
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
343
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
344 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
345 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
346 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
347 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
350 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
351 // 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
352 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
353 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
354 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
355 { \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
356 xtype result; \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
357 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
358 return FALSE; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
359 *v = TH_ ## xmacro ## _TO_NATIVE (result); \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
360 return TRUE; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
361 } \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
362 \
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
363 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
364 { \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
365 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
366 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
367 return FALSE; \
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
368 return TRUE; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
369 }
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 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
373 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
374
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
375 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
376 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
377
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
378 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
379 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
380
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
381 #undef TH_DEFINE_FUNC