1 :
2 : /*
3 : * io_ss.c
4 : *
5 : * Implements the Source/Sink interface.
6 : *
7 : * As will all I/O modules, most functions are for local use only (called
8 : * via function pointers in the I/O context).
9 : *
10 : * The Source/Sink model is the primary 'user' interface for alternate data
11 : * sources; the IOCtx interface is intended (at least in version 1.5) to be
12 : * used internally until it settles down a bit.
13 : *
14 : * This module just layers the Source/Sink interface on top of the IOCtx; no
15 : * support is provided for tell/seek, so GD2 writing is not possible, and
16 : * retrieving parts of GD2 files is also not possible.
17 : *
18 : * A new SS context does not need to be created with both a Source and a Sink.
19 : *
20 : * Written/Modified 1999, Philip Warner.
21 : *
22 : */
23 :
24 : #include <math.h>
25 : #include <string.h>
26 : #include <stdlib.h>
27 : #include "gd.h"
28 : #include "gdhelpers.h"
29 :
30 : /* this is used for creating images in main memory */
31 :
32 : typedef struct ssIOCtx
33 : {
34 : gdIOCtx ctx;
35 : gdSourcePtr src;
36 : gdSinkPtr snk;
37 : } ssIOCtx;
38 :
39 : typedef struct ssIOCtx *ssIOCtxPtr;
40 :
41 : gdIOCtx *gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk);
42 :
43 : static int sourceGetbuf (gdIOCtx *, void *, int);
44 : static int sourceGetchar (gdIOCtx * ctx);
45 : static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size);
46 : static void sinkPutchar (gdIOCtx * ctx, int a);
47 : static void gdFreeSsCtx (gdIOCtx * ctx);
48 :
49 : /* return data as a dynamic pointer */
50 : gdIOCtx * gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
51 0 : {
52 : ssIOCtxPtr ctx;
53 :
54 0 : ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
55 :
56 0 : ctx->src = src;
57 0 : ctx->snk = snk;
58 :
59 0 : ctx->ctx.getC = sourceGetchar;
60 0 : ctx->ctx.getBuf = sourceGetbuf;
61 :
62 0 : ctx->ctx.putC = sinkPutchar;
63 0 : ctx->ctx.putBuf = sinkPutbuf;
64 :
65 0 : ctx->ctx.tell = NULL;
66 0 : ctx->ctx.seek = NULL;
67 :
68 0 : ctx->ctx.gd_free = gdFreeSsCtx;
69 :
70 0 : return (gdIOCtx *) ctx;
71 : }
72 :
73 : static void gdFreeSsCtx (gdIOCtx * ctx)
74 0 : {
75 0 : gdFree(ctx);
76 0 : }
77 :
78 :
79 : static int sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
80 0 : {
81 : ssIOCtx *lctx;
82 : int res;
83 :
84 0 : lctx = (ssIOCtx *) ctx;
85 :
86 0 : res = ((lctx->src->source) (lctx->src->context, buf, size));
87 :
88 : /*
89 : * Translate the return values from the Source object:
90 : * 0 is EOF, -1 is error
91 : */
92 :
93 0 : if (res == 0) {
94 0 : return EOF;
95 0 : } else if (res < 0) {
96 0 : return 0;
97 : } else {
98 0 : return res;
99 : }
100 : }
101 :
102 : static int sourceGetchar (gdIOCtx * ctx)
103 0 : {
104 : int res;
105 : unsigned char buf;
106 :
107 0 : res = sourceGetbuf (ctx, &buf, 1);
108 :
109 0 : if (res == 1) {
110 0 : return buf;
111 : } else {
112 0 : return EOF;
113 : }
114 : }
115 :
116 : static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
117 0 : {
118 : ssIOCtxPtr lctx;
119 : int res;
120 :
121 0 : lctx = (ssIOCtx *) ctx;
122 :
123 0 : res = (lctx->snk->sink) (lctx->snk->context, buf, size);
124 :
125 0 : if (res <= 0) {
126 0 : return 0;
127 : } else {
128 0 : return res;
129 : }
130 : }
131 :
132 : static void sinkPutchar (gdIOCtx * ctx, int a)
133 0 : {
134 : unsigned char b;
135 :
136 0 : b = a;
137 0 : sinkPutbuf (ctx, &b, 1);
138 0 : }
|