comparison 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
comparison
equal deleted inserted replaced
770:79f888e4616f 771:c17eadc60c3d
8 #include "th_ioctx.h" 8 #include "th_ioctx.h"
9 #include "th_string.h" 9 #include "th_string.h"
10 #include "th_endian.h" 10 #include "th_endian.h"
11 11
12 12
13 static void th_io_update_atime(th_ioctx *ctx) 13 static void th_io_update_atime(th_ioctx_t *ctx)
14 { 14 {
15 ctx->atime = time(NULL); 15 ctx->atime = time(NULL);
16 } 16 }
17 17
18 18
19 static void th_io_init(th_ioctx *ctx) 19 static void th_io_init(th_ioctx_t *ctx)
20 { 20 {
21 memset(ctx, 0, sizeof(th_ioctx)); 21 memset(ctx, 0, sizeof(th_ioctx_t));
22 ctx->line = 1; 22 ctx->line = 1;
23 } 23 }
24 24
25 25
26 void th_io_init_stdio(th_ioctx *ctx, FILE *fh) 26 void th_io_init_stdio(th_ioctx_t *ctx, FILE *fh)
27 { 27 {
28 th_io_init(ctx); 28 th_io_init(ctx);
29 29
30 ctx->fops = &th_stdio_io_ops; 30 ctx->fops = &th_stdio_io_ops;
31 ctx->data = (void *) fh; 31 ctx->data = (void *) fh;
32 } 32 }
33 33
34 34
35 th_ioctx * th_io_new(const th_ioctx_ops *fops, const char *filename, const char *mode) 35 th_ioctx_t * th_io_new(const th_ioctx_t_ops *fops, const char *filename, const char *mode)
36 { 36 {
37 th_ioctx *ctx = th_malloc(sizeof(th_ioctx)); 37 th_ioctx_t *ctx = th_malloc(sizeof(th_ioctx_t));
38 if (ctx == NULL) 38 if (ctx == NULL)
39 return NULL; 39 return NULL;
40 40
41 th_io_init(ctx); 41 th_io_init(ctx);
42 42
59 th_io_close(ctx); 59 th_io_close(ctx);
60 return NULL; 60 return NULL;
61 } 61 }
62 62
63 63
64 int th_io_fopen(th_ioctx **pctx, const th_ioctx_ops *fops, const char *filename, const char *mode) 64 int th_io_fopen(th_ioctx_t **pctx, const th_ioctx_t_ops *fops, const char *filename, const char *mode)
65 { 65 {
66 th_ioctx *ctx; 66 th_ioctx_t *ctx;
67 int res; 67 int res;
68 68
69 if ((*pctx = ctx = th_io_new(fops, filename, mode)) == NULL) 69 if ((*pctx = ctx = th_io_new(fops, filename, mode)) == NULL)
70 return THERR_MALLOC; 70 return THERR_MALLOC;
71 71
79 *pctx = NULL; 79 *pctx = NULL;
80 return res; 80 return res;
81 } 81 }
82 82
83 83
84 int th_io_reopen(th_ioctx *ctx, const char *mode) 84 int th_io_reopen(th_ioctx_t *ctx, const char *mode)
85 { 85 {
86 if (ctx == NULL) 86 if (ctx == NULL)
87 return THERR_NULLPTR; 87 return THERR_NULLPTR;
88 88
89 thfclose(ctx); 89 thfclose(ctx);
102 102
103 return (ctx->status = thfopen(ctx)); 103 return (ctx->status = thfopen(ctx));
104 } 104 }
105 105
106 106
107 void th_io_close(th_ioctx *ctx) 107 void th_io_close(th_ioctx_t *ctx)
108 { 108 {
109 if (ctx != NULL) 109 if (ctx != NULL)
110 { 110 {
111 thfclose(ctx); 111 thfclose(ctx);
112 112
120 th_free(ctx); 120 th_free(ctx);
121 } 121 }
122 } 122 }
123 123
124 124
125 bool th_io_set_handlers(th_ioctx *ctx, 125 bool th_io_set_handlers(th_ioctx_t *ctx,
126 void (*error)(th_ioctx *, const int, const char *msg), 126 void (*error)(th_ioctx_t *, const int, const char *msg),
127 void (*msg)(th_ioctx *, const int, const char *msg)) 127 void (*msg)(th_ioctx_t *, const int, const char *msg))
128 { 128 {
129 if (ctx == NULL) 129 if (ctx == NULL)
130 return false; 130 return false;
131 131
132 ctx->error = error; 132 ctx->error = error;
134 134
135 return true; 135 return true;
136 } 136 }
137 137
138 138
139 int th_io_error_v(th_ioctx *ctx, const int err, const char *fmt, va_list ap) 139 int th_io_error_v(th_ioctx_t *ctx, const int err, const char *fmt, va_list ap)
140 { 140 {
141 char *msg = th_strdup_vprintf(fmt, ap); 141 char *msg = th_strdup_vprintf(fmt, ap);
142 142
143 if (ctx->error != NULL) 143 if (ctx->error != NULL)
144 ctx->error((struct th_ioctx *) ctx, err, msg); 144 ctx->error((struct th_ioctx_t *) ctx, err, msg);
145 else 145 else
146 THERR("'%s' #%" PRIu_SIZE_T ": %s\n", ctx->filename, ctx->line, msg); 146 THERR("'%s' #%" PRIu_SIZE_T ": %s\n", ctx->filename, ctx->line, msg);
147 147
148 th_free(msg); 148 th_free(msg);
149 return err; 149 return err;
150 } 150 }
151 151
152 152
153 int th_io_error(th_ioctx *ctx, const int err, const char *fmt, ...) 153 int th_io_error(th_ioctx_t *ctx, const int err, const char *fmt, ...)
154 { 154 {
155 va_list ap; 155 va_list ap;
156 va_start(ap, fmt); 156 va_start(ap, fmt);
157 th_io_error_v(ctx, err, fmt, ap); 157 th_io_error_v(ctx, err, fmt, ap);
158 va_end(ap); 158 va_end(ap);
159 return err; 159 return err;
160 } 160 }
161 161
162 162
163 void th_io_msg_v(th_ioctx *ctx, const int level, const char *fmt, va_list ap) 163 void th_io_msg_v(th_ioctx_t *ctx, const int level, const char *fmt, va_list ap)
164 { 164 {
165 if (ctx->msg != NULL) 165 if (ctx->msg != NULL)
166 { 166 {
167 char *msg = th_strdup_vprintf(fmt, ap); 167 char *msg = th_strdup_vprintf(fmt, ap);
168 ctx->msg((struct th_ioctx *) ctx, level, msg); 168 ctx->msg((struct th_ioctx_t *) ctx, level, msg);
169 th_free(msg); 169 th_free(msg);
170 } 170 }
171 else 171 else
172 THMSG_V(level, fmt, ap); 172 THMSG_V(level, fmt, ap);
173 } 173 }
174 174
175 175
176 void th_io_msg(th_ioctx *ctx, const int level, const char *fmt, ...) 176 void th_io_msg(th_ioctx_t *ctx, const int level, const char *fmt, ...)
177 { 177 {
178 va_list ap; 178 va_list ap;
179 va_start(ap, fmt); 179 va_start(ap, fmt);
180 th_io_msg_v(ctx, level, fmt, ap); 180 th_io_msg_v(ctx, level, fmt, ap);
181 va_end(ap); 181 va_end(ap);
182 } 182 }
183 183
184 184
185 // thfopen() and thfclose() implementations are allowed to be NULL 185 // thfopen() and thfclose() implementations are allowed to be NULL
186 int thfopen(th_ioctx *ctx) 186 int thfopen(th_ioctx_t *ctx)
187 { 187 {
188 if (ctx == NULL || ctx->fops == NULL) 188 if (ctx == NULL || ctx->fops == NULL)
189 return THERR_NULLPTR; 189 return THERR_NULLPTR;
190 190
191 if (ctx->fops->fopen == NULL) 191 if (ctx->fops->fopen == NULL)
193 193
194 return ctx->fops->fopen(ctx); 194 return ctx->fops->fopen(ctx);
195 } 195 }
196 196
197 197
198 void thfclose(th_ioctx *ctx) 198 void thfclose(th_ioctx_t *ctx)
199 { 199 {
200 if (ctx == NULL || ctx->fops == NULL) 200 if (ctx == NULL || ctx->fops == NULL)
201 return; 201 return;
202 202
203 if (ctx->fops->fclose != NULL) 203 if (ctx->fops->fclose != NULL)
204 ctx->fops->fclose(ctx); 204 ctx->fops->fclose(ctx);
205 } 205 }
206 206
207 207
208 int thfreset(th_ioctx *ctx) 208 int thfreset(th_ioctx_t *ctx)
209 { 209 {
210 th_io_update_atime(ctx); 210 th_io_update_atime(ctx);
211 return ctx->fops->freset(ctx); 211 return ctx->fops->freset(ctx);
212 } 212 }
213 213
214 214
215 int thferror(th_ioctx *ctx) 215 int thferror(th_ioctx_t *ctx)
216 { 216 {
217 th_io_update_atime(ctx); 217 th_io_update_atime(ctx);
218 return ctx->fops->ferror(ctx); 218 return ctx->fops->ferror(ctx);
219 } 219 }
220 220
221 221
222 int thfseek(th_ioctx *ctx, const off_t offset, int whence) 222 int thfseek(th_ioctx_t *ctx, const off_t offset, int whence)
223 { 223 {
224 th_io_update_atime(ctx); 224 th_io_update_atime(ctx);
225 return ctx->fops->fseek(ctx, offset, whence); 225 return ctx->fops->fseek(ctx, offset, whence);
226 } 226 }
227 227
228 228
229 off_t thfsize(th_ioctx *ctx) 229 off_t thfsize(th_ioctx_t *ctx)
230 { 230 {
231 th_io_update_atime(ctx); 231 th_io_update_atime(ctx);
232 return ctx->fops->fsize(ctx); 232 return ctx->fops->fsize(ctx);
233 } 233 }
234 234
235 235
236 off_t thftell(th_ioctx *ctx) 236 off_t thftell(th_ioctx_t *ctx)
237 { 237 {
238 th_io_update_atime(ctx); 238 th_io_update_atime(ctx);
239 return ctx->fops->ftell(ctx); 239 return ctx->fops->ftell(ctx);
240 } 240 }
241 241
242 242
243 bool thfeof(th_ioctx *ctx) 243 bool thfeof(th_ioctx_t *ctx)
244 { 244 {
245 th_io_update_atime(ctx); 245 th_io_update_atime(ctx);
246 return ctx->fops->feof(ctx); 246 return ctx->fops->feof(ctx);
247 } 247 }
248 248
249 249
250 int thfgetc(th_ioctx *ctx) 250 int thfgetc(th_ioctx_t *ctx)
251 { 251 {
252 th_io_update_atime(ctx); 252 th_io_update_atime(ctx);
253 return ctx->fops->fgetc(ctx); 253 return ctx->fops->fgetc(ctx);
254 } 254 }
255 255
256 256
257 int thfputc(int v, th_ioctx *ctx) 257 int thfputc(int v, th_ioctx_t *ctx)
258 { 258 {
259 th_io_update_atime(ctx); 259 th_io_update_atime(ctx);
260 return ctx->fops->fputc(v, ctx); 260 return ctx->fops->fputc(v, ctx);
261 } 261 }
262 262
263 263
264 size_t thfread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) 264 size_t thfread(void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx)
265 { 265 {
266 th_io_update_atime(ctx); 266 th_io_update_atime(ctx);
267 return ctx->fops->fread(ptr, size, nmemb, ctx); 267 return ctx->fops->fread(ptr, size, nmemb, ctx);
268 } 268 }
269 269
270 270
271 size_t thfwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) 271 size_t thfwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx)
272 { 272 {
273 th_io_update_atime(ctx); 273 th_io_update_atime(ctx);
274 return ctx->fops->fwrite(ptr, size, nmemb, ctx); 274 return ctx->fops->fwrite(ptr, size, nmemb, ctx);
275 } 275 }
276 276
277 277
278 char *thfgets(char *str, int size, th_ioctx *ctx) 278 char *thfgets(char *str, int size, th_ioctx_t *ctx)
279 { 279 {
280 char *ptr = str, *end = str + size - 1; 280 char *ptr = str, *end = str + size - 1;
281 int c; 281 int c;
282 282
283 if (size <= 0) 283 if (size <= 0)
293 293
294 return (ptr > str) ? str : NULL; 294 return (ptr > str) ? str : NULL;
295 } 295 }
296 296
297 297
298 int thfputs(const char *ptr, th_ioctx *ctx) 298 int thfputs(const char *ptr, th_ioctx_t *ctx)
299 { 299 {
300 if (ctx->fops->fputs != NULL) 300 if (ctx->fops->fputs != NULL)
301 return ctx->fops->fputs(ptr, ctx); 301 return ctx->fops->fputs(ptr, ctx);
302 302
303 const char *p = ptr; 303 const char *p = ptr;
305 while (*p && (rval = ctx->fops->fputc(*p, ctx)) != EOF) p++; 305 while (*p && (rval = ctx->fops->fputc(*p, ctx)) != EOF) p++;
306 return rval; 306 return rval;
307 } 307 }
308 308
309 309
310 int thvfprintf(th_ioctx *ctx, const char *fmt, va_list ap) 310 int thvfprintf(th_ioctx_t *ctx, const char *fmt, va_list ap)
311 { 311 {
312 if (ctx->fops->vfprintf != NULL) 312 if (ctx->fops->vfprintf != NULL)
313 return ctx->fops->vfprintf(ctx, fmt, ap); 313 return ctx->fops->vfprintf(ctx, fmt, ap);
314 else 314 else
315 { 315 {
319 return rval; 319 return rval;
320 } 320 }
321 } 321 }
322 322
323 323
324 int thfprintf(th_ioctx *ctx, const char *fmt, ...) 324 int thfprintf(th_ioctx_t *ctx, const char *fmt, ...)
325 { 325 {
326 int rval; 326 int rval;
327 va_list ap; 327 va_list ap;
328 va_start(ap, fmt); 328 va_start(ap, fmt);
329 rval = thvfprintf(ctx, fmt, ap); 329 rval = thvfprintf(ctx, fmt, ap);
330 va_end(ap); 330 va_end(ap);
331 return rval; 331 return rval;
332 } 332 }
333 333
334 334
335 bool thfread_str(th_ioctx *ctx, void *ptr, const size_t len) 335 bool thfread_str(th_ioctx_t *ctx, void *ptr, const size_t len)
336 { 336 {
337 return thfread(ptr, sizeof(uint8_t), len, ctx) == len; 337 return thfread(ptr, sizeof(uint8_t), len, ctx) == len;
338 } 338 }
339 339
340 340
341 bool thfread_u8(th_ioctx *ctx, uint8_t *val) 341 bool thfread_u8(th_ioctx_t *ctx, uint8_t *val)
342 { 342 {
343 return (thfread(val, sizeof(uint8_t), 1, ctx) == 1); 343 return (thfread(val, sizeof(uint8_t), 1, ctx) == 1);
344 } 344 }
345 345
346 346
347 bool thfwrite_str(th_ioctx *ctx, const void *ptr, const size_t len) 347 bool thfwrite_str(th_ioctx_t *ctx, const void *ptr, const size_t len)
348 { 348 {
349 return thfwrite(ptr, sizeof(uint8_t), len, ctx) == len; 349 return thfwrite(ptr, sizeof(uint8_t), len, ctx) == len;
350 } 350 }
351 351
352 352
353 bool thfwrite_u8(th_ioctx *ctx, const uint8_t val) 353 bool thfwrite_u8(th_ioctx_t *ctx, const uint8_t val)
354 { 354 {
355 return thfwrite(&val, sizeof(uint8_t), 1, ctx) == 1; 355 return thfwrite(&val, sizeof(uint8_t), 1, ctx) == 1;
356 } 356 }
357 357
358 358
359 // 359 //
360 // File routines for endian-dependant data 360 // File routines for endian-dependant data
361 // 361 //
362 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \ 362 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \
363 bool thfread_ ## xname (th_ioctx *ctx, xtype *v) \ 363 bool thfread_ ## xname (th_ioctx_t *ctx, xtype *v) \
364 { \ 364 { \
365 xtype result; \ 365 xtype result; \
366 if (thfread(&result, sizeof( xtype ), 1, ctx) != 1) \ 366 if (thfread(&result, sizeof( xtype ), 1, ctx) != 1) \
367 return false; \ 367 return false; \
368 *v = TH_ ## xmacro ## _TO_NATIVE (result); \ 368 *v = TH_ ## xmacro ## _TO_NATIVE (result); \
369 return true; \ 369 return true; \
370 } \ 370 } \
371 \ 371 \
372 bool thfwrite_ ## xname (th_ioctx *ctx, const xtype v) \ 372 bool thfwrite_ ## xname (th_ioctx_t *ctx, const xtype v) \
373 { \ 373 { \
374 xtype result = TH_NATIVE_TO_ ## xmacro (v); \ 374 xtype result = TH_NATIVE_TO_ ## xmacro (v); \
375 if (thfwrite(&result, sizeof( xtype ), 1, ctx) != 1) \ 375 if (thfwrite(&result, sizeof( xtype ), 1, ctx) != 1) \
376 return false; \ 376 return false; \
377 return true; \ 377 return true; \