4
* Implements the Source/Sink interface.
6
* As will all I/O modules, most functions are for local use only (called
7
* via function pointers in the I/O context).
9
* The Source/Sink model is the primary 'user' interface for alternate data
10
* sources; the IOCtx interface is intended (at least in version 1.5) to be
11
* used internally until it settles down a bit.
13
* This module just layers the Source/Sink interface on top of the IOCtx; no
14
* support is provided for tell/seek, so GD2 writing is not possible, and
15
* retrieving parts of GD2 files is also not possible.
17
* A new SS context does not need to be created with both a Source and a Sink.
19
* Written/Modified 1999, Philip Warner.
28
/* this is used for creating images in main memory*/
30
typedef struct ssIOCtx {
36
typedef struct ssIOCtx *ssIOCtxPtr;
38
gdIOCtx* gdNewSSCtx(gdSourcePtr src, gdSinkPtr snk);
40
static int sourceGetbuf( gdIOCtx*, void *, int );
41
static int sourceGetchar( gdIOCtx* ctx);
42
static int sinkPutbuf( gdIOCtx* ctx, const void *buf, int size );
43
static void sinkPutchar( gdIOCtx* ctx, int a );
44
static void freeSsCtx(gdIOCtx *ctx);
46
/* return data as a dynamic pointer */
47
gdIOCtx* gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk) {
50
ctx = (ssIOCtxPtr) malloc(sizeof(ssIOCtx));
58
ctx->ctx.getC = sourceGetchar;
59
ctx->ctx.getBuf = sourceGetbuf;
61
ctx->ctx.putC = sinkPutchar;
62
ctx->ctx.putBuf = sinkPutbuf;
67
ctx->ctx.free = freeSsCtx;
73
void freeSsCtx(gdIOCtx *ctx)
80
sourceGetbuf( gdIOCtx* ctx, void *buf, int size )
85
lctx = (ssIOCtx*) ctx;
87
res = ((lctx->src->source)(lctx->src->context, buf, size));
90
** Translate the return values from the Source object:
91
** 0 is EOF, -1 is error
104
static int sourceGetchar( gdIOCtx* ctx)
109
res = sourceGetbuf(ctx, &buf, 1);
120
sinkPutbuf( gdIOCtx* ctx, const void *buf, int size )
125
lctx = (ssIOCtx*) ctx;
127
res = (lctx->snk->sink)(lctx->snk->context, buf, size);
138
sinkPutchar( gdIOCtx* ctx, int a )
143
sinkPutbuf(ctx, &b, 1);