annotate th_ioctx.c @ 71:ce49160d2599

Oops, the I/O context initialization was memsetting &pointer instead of pointer directly, causing nasty (but easily noticeable) memory corruption.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 15 Nov 2012 19:44:59 +0200
parents d94af48cef7b
children 43df05a632cb
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 /*
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Standard I/O context helpers
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
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2012 Tecnic Software productions (TNSP)
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"
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 /* Simple STD I/O contexts
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 */
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 if (ctx == NULL || filename == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
71
ce49160d2599 Oops, the I/O context initialization was memsetting &pointer instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
18 th_memset(ctx, 0, sizeof(ctx));
ce49160d2599 Oops, the I/O context initialization was memsetting &pointer instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
19
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 if ((ctx->filename = th_strdup(filename)) == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 return TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 if (!th_ioctx_init(ctx, filename) || mode == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 if ((ctx->fp = fopen(filename, mode)) == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 return TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 void th_ioctx_close(th_ioctx_t *ctx)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 if (ctx != NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 th_free(ctx->filename);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 ctx->filename = NULL;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 if (ctx->fp != NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 fclose(ctx->fp);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 ctx->fp = NULL;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 void th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 char *msg;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 va_list ap;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 va_start(ap, fmt);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 msg = th_strdup_vprintf(fmt, ap);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 va_end(ap);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 if (ctx->error != NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 ctx->error(ctx, err, msg);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 else
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 THERR("'%s' #%d: %s\n", ctx->filename, (unsigned int) ctx->line, msg);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 th_free(msg);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 th_ioctx_t *th_ioctx_new(const char *filename)
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 th_ioctx_t *ctx = th_malloc0(sizeof(th_ioctx_t));
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 if (ctx == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 return NULL;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 th_ioctx_init(ctx, filename);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 ctx->allocated = TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 return ctx;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 void th_ioctx_free(th_ioctx_t *ctx)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 th_ioctx_close(ctx);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 if (ctx->allocated)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 th_free(ctx);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }