annotate th_ioctx.c @ 771:c17eadc60c3d

Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Feb 2023 23:33:45 +0200
parents 8eca15bde07d
children 1959ba53e9dc
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
726
29e44a58bc73 Bump copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 722
diff changeset
4 * (C) Copyright 2012-2022 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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
13 static void th_io_update_atime(th_ioctx_t *ctx)
461
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
19 static void th_io_init(th_ioctx_t *ctx)
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 {
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
21 memset(ctx, 0, sizeof(th_ioctx_t));
650
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
26 void th_io_init_stdio(th_ioctx_t *ctx, FILE *fh)
650
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
35 th_ioctx_t * th_io_new(const th_ioctx_t_ops *fops, const char *filename, const char *mode)
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 {
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
37 th_ioctx_t *ctx = th_malloc(sizeof(th_ioctx_t));
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
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
43 ctx->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);
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
47 ctx->fn_allocated = 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
700
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
51 if (mode != NULL && (ctx->mode = th_strdup(mode)) == NULL)
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
52 goto err;
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
53
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
54 ctx->md_allocated = true;
700
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
55
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
56 return ctx;
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
57
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
58 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
59 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
60 return NULL;
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
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
64 int th_io_fopen(th_ioctx_t **pctx, const th_ioctx_t_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
65 {
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
66 th_ioctx_t *ctx;
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
67 int res;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
68
700
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
69 if ((*pctx = ctx = th_io_new(fops, filename, mode)) == NULL)
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
70 return THERR_MALLOC;
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
71
700
ec8fe89576ff Adjust th_ioctx API. Breakage.
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
72 if ((res = thfopen(ctx)) != THERR_OK)
477
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
73 goto err;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
74
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
75 return THERR_OK;
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 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
78 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
79 *pctx = NULL;
96d137a6b392 Improve ioctx API. This breaks backwards compatibility of th_io_fopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
80 return res;
204
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
81 }
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
82
55f429dff750 Add th_io_fopen() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 202
diff changeset
83
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
84 int th_io_reopen(th_ioctx_t *ctx, const char *mode)
695
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
85 {
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
86 if (ctx == NULL)
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
87 return THERR_NULLPTR;
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
88
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
89 thfclose(ctx);
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
90
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
91 if (ctx->md_allocated)
698
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
92 {
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
93 if ((ctx->status = th_pstr_cpy(&ctx->mode, mode)) != THERR_OK)
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
94 return ctx->status;
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
95 }
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
96 else
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
97 {
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
98 ctx->md_allocated = true;
699
3707a823aa02 Fix to the previous commit. strdup the mode string since the argument is const.
Matti Hamalainen <ccr@tnsp.org>
parents: 698
diff changeset
99 if ((ctx->mode = th_strdup(mode)) == NULL)
3707a823aa02 Fix to the previous commit. strdup the mode string since the argument is const.
Matti Hamalainen <ccr@tnsp.org>
parents: 698
diff changeset
100 return THERR_MALLOC;
698
29d9083dff1c Do not reallocate th_ioctx::mode string if it has not been allocated in the first place.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
101 }
695
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
102
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
103 return (ctx->status = thfopen(ctx));
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
104 }
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
105
a04b8fe158b9 Add helper function th_io_reopen().
Matti Hamalainen <ccr@tnsp.org>
parents: 691
diff changeset
106
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
107 void th_io_close(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
108 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
109 if (ctx != NULL)
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
110 {
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
111 thfclose(ctx);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
112
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
113 if (ctx->fn_allocated)
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
114 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
115
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
116 if (ctx->md_allocated)
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
117 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
118
769
8eca15bde07d Rename some th_ioctx members.
Matti Hamalainen <ccr@tnsp.org>
parents: 735
diff changeset
119 if (ctx->ctx_allocated)
650
24cbab6e88c6 Remove th_io_free(), merge the functionality to th_io_close(). Add flags
Matti Hamalainen <ccr@tnsp.org>
parents: 556
diff changeset
120 th_free(ctx);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
121 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
122 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
123
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
124
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
125 bool th_io_set_handlers(th_ioctx_t *ctx,
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
126 void (*error)(th_ioctx_t *, const int, const char *msg),
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
127 void (*msg)(th_ioctx_t *, const int, const char *msg))
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
128 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
129 if (ctx == NULL)
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
130 return false;
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 ctx->error = error;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
133 ctx->msg = msg;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
135 return true;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
139 int th_io_error_v(th_ioctx_t *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
140 {
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
141 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
142
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 if (ctx->error != NULL)
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
144 ctx->error((struct th_ioctx_t *) ctx, err, msg);
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 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
146 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
147
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 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
149 return err;
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
153 int th_io_error(th_ioctx_t *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
154 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
155 va_list ap;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
156 va_start(ap, fmt);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
157 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
158 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
159 return err;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
160 }
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
163 void th_io_msg_v(th_ioctx_t *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
164 {
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
165 if (ctx->msg != NULL)
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 char *msg = th_strdup_vprintf(fmt, ap);
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
168 ctx->msg((struct th_ioctx_t *) ctx, level, msg);
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
169 th_free(msg);
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 else
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
172 THMSG_V(level, fmt, 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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
176 void th_io_msg(th_ioctx_t *ctx, const int level, const char *fmt, ...)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
177 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
178 va_list ap;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
179 va_start(ap, fmt);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
180 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
181 va_end(ap);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
182 }
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
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
185 // thfopen() and thfclose() implementations are allowed to be NULL
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
186 int thfopen(th_ioctx_t *ctx)
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
187 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
188 if (ctx == NULL || ctx->fops == NULL)
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
189 return THERR_NULLPTR;
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
190
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
191 if (ctx->fops->fopen == NULL)
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
192 return THERR_OK;
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
193
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
194 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
195 }
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
196
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
197
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
198 void thfclose(th_ioctx_t *ctx)
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
199 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
200 if (ctx == NULL || ctx->fops == NULL)
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
201 return;
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
202
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
203 if (ctx->fops->fclose != NULL)
686
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
204 ctx->fops->fclose(ctx);
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
205 }
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
206
dfc2c9f0577f Add functions thfopen(ctx) and thfclose(ctx) to th_ioctx.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
207
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
208 int thfreset(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
209 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
210 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
211 return ctx->fops->freset(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
212 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
213
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
214
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
215 int thferror(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
216 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
217 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
218 return ctx->fops->ferror(ctx);
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
221
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
222 int thfseek(th_ioctx_t *ctx, const off_t offset, int whence)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
223 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
224 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
225 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
226 }
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
229 off_t thfsize(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
230 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
231 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
232 return ctx->fops->fsize(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
233 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
234
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
235
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
236 off_t thftell(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
237 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
238 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
239 return ctx->fops->ftell(ctx);
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
242
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
243 bool thfeof(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
244 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
245 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
246 return ctx->fops->feof(ctx);
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
247 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
248
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
249
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
250 int thfgetc(th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
251 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
252 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
253 return ctx->fops->fgetc(ctx);
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
256
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
257 int thfputc(int v, th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
258 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
259 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
260 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
261 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
262
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
263
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
264 size_t thfread(void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
265 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
266 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
267 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
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
271 size_t thfwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
272 {
461
04320ca79407 Actually, use a inline function for ioctx atime updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 460
diff changeset
273 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
274 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
275 }
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
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
278 char *thfgets(char *str, int size, th_ioctx_t *ctx)
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
279 {
206
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
280 char *ptr = str, *end = str + size - 1;
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
281 int c;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
282
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
283 if (size <= 0)
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 return NULL;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
285
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
286 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
287 {
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
288 *ptr++ = c;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
289 if (c == '\n')
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
290 break;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
291 }
206
c2193323736d Fix thfgets().
Matti Hamalainen <ccr@tnsp.org>
parents: 204
diff changeset
292 *ptr = 0;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
293
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
294 return (ptr > str) ? str : NULL;
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
295 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
296
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
297
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
298 int thfputs(const char *ptr, th_ioctx_t *ctx)
208
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 if (ctx->fops->fputs != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
301 return ctx->fops->fputs(ptr, ctx);
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 const char *p = ptr;
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
304 int rval = 0;
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
305 while (*p && (rval = ctx->fops->fputc(*p, ctx)) != EOF) p++;
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
306 return rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
307 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
308
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
309
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
310 int thvfprintf(th_ioctx_t *ctx, const char *fmt, va_list ap)
208
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 if (ctx->fops->vfprintf != NULL)
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
313 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
314 else
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
315 {
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
316 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
317 int rval = thfputs(msg, ctx);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
318 th_free(msg);
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
319 return rval;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
320 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
321 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
322
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
323
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
324 int thfprintf(th_ioctx_t *ctx, const char *fmt, ...)
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
325 {
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
326 int rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
327 va_list ap;
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
328 va_start(ap, fmt);
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
329 rval = thvfprintf(ctx, fmt, ap);
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
330 va_end(ap);
675
fb4093ad1f7b Add MemIO ioctx functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 650
diff changeset
331 return rval;
208
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
332 }
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
333
3635415a2d03 More work on th_ioctx stuff and wrappers.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
334
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
335 bool thfread_str(th_ioctx_t *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
336 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
337 return thfread(ptr, sizeof(uint8_t), len, ctx) == len;
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
338 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
339
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
340
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
341 bool thfread_u8(th_ioctx_t *ctx, uint8_t *val)
202
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 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
344 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
345
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
346
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
347 bool thfwrite_str(th_ioctx_t *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
348 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
349 return thfwrite(ptr, sizeof(uint8_t), len, ctx) == len;
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
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
352
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
353 bool thfwrite_u8(th_ioctx_t *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
354 {
691
49c818acbbbe Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
355 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
356 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
202
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 // 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
361 //
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
362 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
363 bool thfread_ ## xname (th_ioctx_t *ctx, 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; \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
366 if (thfread(&result, sizeof( xtype ), 1, ctx) != 1) \
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
367 return false; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
368 *v = TH_ ## xmacro ## _TO_NATIVE (result); \
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
369 return true; \
202
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 \
771
c17eadc60c3d Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
Matti Hamalainen <ccr@tnsp.org>
parents: 769
diff changeset
372 bool thfwrite_ ## xname (th_ioctx_t *ctx, const xtype v) \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
373 { \
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
374 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
375 if (thfwrite(&result, sizeof( xtype ), 1, ctx) != 1) \
735
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
376 return false; \
31bc1ed07cf5 Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents: 726
diff changeset
377 return true; \
202
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
378 }
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
379
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
380
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
381 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
382 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
383
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
384 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
385 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
386
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
387 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
388 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
389
b392293047da Refactor I/O contexts. Breaks API and all that.
Matti Hamalainen <ccr@tnsp.org>
parents: 166
diff changeset
390 #undef TH_DEFINE_FUNC