summaryrefslogtreecommitdiff
path: root/Build/source/libs/gd/gd_io_ss.c
blob: 7a68fd16510787f79f80cee15a40ed384386d134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

/*
   * io_ss.c
   *
   * Implements the Source/Sink interface.
   *
   * As will all I/O modules, most functions are for local use only (called
   * via function pointers in the I/O context).
   *
   * The Source/Sink model is the primary 'user' interface for alternate data
   * sources; the IOCtx interface is intended (at least in version 1.5) to be
   * used internally until it settles down a bit.
   *
   * This module just layers the Source/Sink interface on top of the IOCtx; no
   * support is provided for tell/seek, so GD2 writing is not possible, and 
   * retrieving parts of GD2 files is also not possible.
   *
   * A new SS context does not need to be created with both a Source and a Sink.
   *
   * Written/Modified 1999, Philip Warner.
   *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "gd.h"
#include "gdhelpers.h"

/* this is used for creating images in main memory */

typedef struct ssIOCtx
{
  gdIOCtx ctx;
  gdSourcePtr src;
  gdSinkPtr snk;
}
ssIOCtx;

typedef struct ssIOCtx *ssIOCtxPtr;

static int sourceGetbuf (gdIOCtx *, void *, int);
static int sourceGetchar (gdIOCtx * ctx);
static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size);
static void sinkPutchar (gdIOCtx * ctx, int a);
static void gdFreeSsCtx (gdIOCtx * ctx);

/* return data as a dynamic pointer */
BGD_DECLARE(gdIOCtx *) gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
{
  ssIOCtxPtr ctx;

  ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
  if (ctx == NULL)
    {
      return NULL;
    }

  ctx->src = src;
  ctx->snk = snk;

  ctx->ctx.getC = sourceGetchar;
  ctx->ctx.getBuf = sourceGetbuf;

  ctx->ctx.putC = sinkPutchar;
  ctx->ctx.putBuf = sinkPutbuf;

  ctx->ctx.tell = NULL;
  ctx->ctx.seek = NULL;

  ctx->ctx.gd_free = gdFreeSsCtx;

  return (gdIOCtx *) ctx;
}

static void
gdFreeSsCtx (gdIOCtx * ctx)
{
  gdFree (ctx);
}


static int
sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
{
  ssIOCtx *lctx;
  int res;

  lctx = (ssIOCtx *) ctx;

  res = ((lctx->src->source) (lctx->src->context, buf, size));

/*
   ** Translate the return values from the Source object: 
   ** 0 is EOF, -1 is error
 */

  if (res == 0)
    {
      return EOF;
    }
  else if (res < 0)
    {
      return 0;
    }
  else
    {
      return res;
    };

}

static int
sourceGetchar (gdIOCtx * ctx)
{
  int res;
  unsigned char buf;

  res = sourceGetbuf (ctx, &buf, 1);

  if (res == 1)
    {
      return buf;
    }
  else
    {
      return EOF;
    };

}

static int
sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
{
  ssIOCtxPtr lctx;
  int res;

  lctx = (ssIOCtx *) ctx;

  res = (lctx->snk->sink) (lctx->snk->context, buf, size);

  if (res <= 0)
    {
      return 0;
    }
  else
    {
      return res;
    };

}

static void
sinkPutchar (gdIOCtx * ctx, int a)
{
  unsigned char b;

  b = a;
  sinkPutbuf (ctx, &b, 1);

}