This source file includes following definitions.
- gdNewSSCtx
- gdFreeSsCtx
- sourceGetbuf
- sourceGetchar
- sinkPutbuf
- sinkPutchar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
50 gdIOCtx * gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
51 {
52 ssIOCtxPtr ctx;
53
54 ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
55
56 ctx->src = src;
57 ctx->snk = snk;
58
59 ctx->ctx.getC = sourceGetchar;
60 ctx->ctx.getBuf = sourceGetbuf;
61
62 ctx->ctx.putC = sinkPutchar;
63 ctx->ctx.putBuf = sinkPutbuf;
64
65 ctx->ctx.tell = NULL;
66 ctx->ctx.seek = NULL;
67
68 ctx->ctx.gd_free = gdFreeSsCtx;
69
70 return (gdIOCtx *) ctx;
71 }
72
73 static void gdFreeSsCtx (gdIOCtx * ctx)
74 {
75 gdFree(ctx);
76 }
77
78
79 static int sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
80 {
81 ssIOCtx *lctx;
82 int res;
83
84 lctx = (ssIOCtx *) ctx;
85
86 res = ((lctx->src->source) (lctx->src->context, buf, size));
87
88
89
90
91
92
93 if (res == 0) {
94 return EOF;
95 } else if (res < 0) {
96 return 0;
97 } else {
98 return res;
99 }
100 }
101
102 static int sourceGetchar (gdIOCtx * ctx)
103 {
104 int res;
105 unsigned char buf;
106
107 res = sourceGetbuf (ctx, &buf, 1);
108
109 if (res == 1) {
110 return buf;
111 } else {
112 return EOF;
113 }
114 }
115
116 static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
117 {
118 ssIOCtxPtr lctx;
119 int res;
120
121 lctx = (ssIOCtx *) ctx;
122
123 res = (lctx->snk->sink) (lctx->snk->context, buf, size);
124
125 if (res <= 0) {
126 return 0;
127 } else {
128 return res;
129 }
130 }
131
132 static void sinkPutchar (gdIOCtx * ctx, int a)
133 {
134 unsigned char b;
135
136 b = a;
137 sinkPutbuf (ctx, &b, 1);
138 }