annotate th_ioctx.c @ 72:43df05a632cb

Break I/O context API, add function pointers to init/open/new functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 15 Nov 2012 19:52:09 +0200
parents ce49160d2599
children 3882efea2640
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 */
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
13 BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename,
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
14 void (*error)(struct _th_ioctx_t *, const int, const char *msg),
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
15 void (*msg)(struct _th_ioctx_t *, const char *msg))
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
16
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 if (ctx == NULL || filename == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
71
ce49160d2599 Oops, the I/O context initialization was memsetting &pointer instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
21 th_memset(ctx, 0, sizeof(ctx));
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
22 ctx->error = error;
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
23 ctx->msg = msg;
71
ce49160d2599 Oops, the I/O context initialization was memsetting &pointer instead of
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
24
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 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
26 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 return TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
32 BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode,
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
33 void (*error)(struct _th_ioctx_t *, const int, const char *msg),
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
34 void (*msg)(struct _th_ioctx_t *, const char *msg))
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 {
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
36 if (!th_ioctx_init(ctx, filename, error, msg) || mode == NULL)
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 return FALSE;
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 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
40 return FALSE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 return TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
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 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
47 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 if (ctx != 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 th_free(ctx->filename);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 ctx->filename = NULL;
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 if (ctx->fp != NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 fclose(ctx->fp);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 ctx->fp = NULL;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 }
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
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 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
61 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 char *msg;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 va_list ap;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 va_start(ap, fmt);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 msg = th_strdup_vprintf(fmt, ap);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 va_end(ap);
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 if (ctx->error != NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 ctx->error(ctx, err, msg);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 else
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 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
73
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 th_free(msg);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
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
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
78 th_ioctx_t *th_ioctx_new(const char *filename,
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
79 void (*error)(struct _th_ioctx_t *, const int, const char *msg),
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
80 void (*msg)(struct _th_ioctx_t *, const char *msg))
67
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 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
83 if (ctx == NULL)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 return NULL;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
72
43df05a632cb Break I/O context API, add function pointers to init/open/new functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
86 th_ioctx_init(ctx, filename, error, msg);
67
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 ctx->allocated = TRUE;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 return ctx;
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 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
93 {
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 th_ioctx_close(ctx);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 if (ctx->allocated)
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 th_free(ctx);
d94af48cef7b Added new module, a simple I/O context helper.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }