Mercurial > hg > th-libs
comparison th_ioctx.c @ 735:31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 12:14:39 +0200 |
parents | 29e44a58bc73 |
children | 8eca15bde07d |
comparison
equal
deleted
inserted
replaced
734:2ae1045f6c18 | 735:31bc1ed07cf5 |
---|---|
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 |
43 ctx->allocated = TRUE; | 43 ctx->allocated = true; |
44 ctx->fops = fops; | 44 ctx->fops = fops; |
45 | 45 |
46 ctx->filename = th_strdup(filename); | 46 ctx->filename = th_strdup(filename); |
47 ctx->fallocated = TRUE; | 47 ctx->fallocated = true; |
48 if (filename != NULL && ctx->filename == NULL) | 48 if (filename != NULL && ctx->filename == NULL) |
49 goto err; | 49 goto err; |
50 | 50 |
51 if (mode != NULL && (ctx->mode = th_strdup(mode)) == NULL) | 51 if (mode != NULL && (ctx->mode = th_strdup(mode)) == NULL) |
52 goto err; | 52 goto err; |
53 | 53 |
54 ctx->mallocated = TRUE; | 54 ctx->mallocated = true; |
55 | 55 |
56 return ctx; | 56 return ctx; |
57 | 57 |
58 err: | 58 err: |
59 th_io_close(ctx); | 59 th_io_close(ctx); |
93 if ((ctx->status = th_pstr_cpy(&ctx->mode, mode)) != THERR_OK) | 93 if ((ctx->status = th_pstr_cpy(&ctx->mode, mode)) != THERR_OK) |
94 return ctx->status; | 94 return ctx->status; |
95 } | 95 } |
96 else | 96 else |
97 { | 97 { |
98 ctx->mallocated = TRUE; | 98 ctx->mallocated = true; |
99 if ((ctx->mode = th_strdup(mode)) == NULL) | 99 if ((ctx->mode = th_strdup(mode)) == NULL) |
100 return THERR_MALLOC; | 100 return THERR_MALLOC; |
101 } | 101 } |
102 | 102 |
103 return (ctx->status = thfopen(ctx)); | 103 return (ctx->status = thfopen(ctx)); |
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 *ctx, |
126 void (*error)(th_ioctx *, const int, const char *msg), | 126 void (*error)(th_ioctx *, const int, const char *msg), |
127 void (*msg)(th_ioctx *, const int, const char *msg)) | 127 void (*msg)(th_ioctx *, 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; |
133 ctx->msg = msg; | 133 ctx->msg = msg; |
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 *ctx, const int err, const char *fmt, va_list ap) |
140 { | 140 { |
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 *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 |
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 *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 *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 *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 *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 *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 *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; \ |
378 } | 378 } |
379 | 379 |
380 | 380 |
381 TH_DEFINE_FUNC(le16, uint16_t, LE16) | 381 TH_DEFINE_FUNC(le16, uint16_t, LE16) |
382 TH_DEFINE_FUNC(le32, uint32_t, LE32) | 382 TH_DEFINE_FUNC(le32, uint32_t, LE32) |