libgd/src/gd_io_ss.c

147 lines
2.7 KiB
C
Raw Permalink Normal View History

2006-04-05 08:35:53 -07:00
/*
2007-12-21 12:10:47 -08:00
* 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
2013-04-03 05:23:11 -07:00
* support is provided for tell/seek, so GD2 writing is not possible, and
2007-12-21 12:10:47 -08:00
* 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.
*
2006-04-05 08:42:56 -07:00
*/
2006-04-05 08:35:53 -07:00
2006-04-05 08:44:56 -07:00
#ifdef HAVE_CONFIG_H
2007-12-21 12:10:47 -08:00
# include "config.h"
2006-04-05 08:44:56 -07:00
#endif
2006-04-05 08:35:53 -07:00
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "gd.h"
2006-04-05 08:41:55 -07:00
#include "gdhelpers.h"
2006-04-05 08:35:53 -07:00
2006-04-05 08:42:56 -07:00
/* this is used for creating images in main memory */
2006-04-05 08:35:53 -07:00
2013-04-03 05:23:11 -07:00
typedef struct ssIOCtx {
2007-12-21 12:10:47 -08:00
gdIOCtx ctx;
gdSourcePtr src;
gdSinkPtr snk;
2006-04-05 08:46:42 -07:00
}
2006-04-05 08:42:56 -07:00
ssIOCtx;
2006-04-05 08:35:53 -07:00
2006-04-05 08:42:56 -07:00
typedef struct ssIOCtx *ssIOCtxPtr;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
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);
2006-04-05 08:35:53 -07:00
/*
Function: gdNewSSCtx
Return data as a dynamic pointer.
*/
2007-12-21 12:10:47 -08:00
BGD_DECLARE(gdIOCtx *) gdNewSSCtx(gdSourcePtr src, gdSinkPtr snk)
2006-04-05 08:42:56 -07:00
{
2007-12-21 12:10:47 -08:00
ssIOCtxPtr ctx;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx = (ssIOCtxPtr)gdMalloc(sizeof (ssIOCtx));
if (ctx == NULL) {
return NULL;
}
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx->src = src;
ctx->snk = snk;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx->ctx.getC = sourceGetchar;
ctx->ctx.getBuf = sourceGetbuf;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx->ctx.putC = sinkPutchar;
ctx->ctx.putBuf = sinkPutbuf;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx->ctx.tell = NULL;
ctx->ctx.seek = NULL;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
ctx->ctx.gd_free = gdFreeSsCtx;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
return (gdIOCtx *)ctx;
2006-04-05 08:38:05 -07:00
}
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
static void gdFreeSsCtx(gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-12-21 12:10:47 -08:00
gdFree(ctx);
2006-04-05 08:35:53 -07:00
}
2007-12-21 12:10:47 -08:00
static int sourceGetbuf(gdIOCtx *ctx, void *buf, int size)
2006-04-05 08:35:53 -07:00
{
2007-12-21 12:10:47 -08:00
ssIOCtx *lctx;
int res;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
lctx = (ssIOCtx *)ctx;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
res = ((lctx->src->source)(lctx->src->context, buf, size));
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
/*
2013-04-03 05:23:11 -07:00
* Translate the return values from the Source object:
2007-12-21 12:10:47 -08:00
* 0 is EOF, -1 is error
*/
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
if (res == 0) {
return 0;
} else if (res < 0) {
return 0;
} else {
return res;
}
2006-04-05 08:35:53 -07:00
}
2007-12-21 12:10:47 -08:00
static int sourceGetchar(gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-12-21 12:10:47 -08:00
int res;
unsigned char buf;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
res = sourceGetbuf(ctx, &buf, 1);
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
if (res == 1) {
return buf;
} else {
return EOF;
}
2006-04-05 08:35:53 -07:00
}
2007-12-21 12:10:47 -08:00
static int sinkPutbuf(gdIOCtx *ctx, const void *buf, int size)
2006-04-05 08:35:53 -07:00
{
2007-12-21 12:10:47 -08:00
ssIOCtxPtr lctx;
int res;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
lctx = (ssIOCtx *)ctx;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
res = (lctx->snk->sink)(lctx->snk->context, buf, size);
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
if (res <= 0) {
return 0;
} else {
return res;
}
2006-04-05 08:35:53 -07:00
}
2007-12-21 12:10:47 -08:00
static void sinkPutchar(gdIOCtx *ctx, int a)
2006-04-05 08:35:53 -07:00
{
2007-12-21 12:10:47 -08:00
unsigned char b;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
b = a;
2006-04-05 08:35:53 -07:00
2007-12-21 12:10:47 -08:00
sinkPutbuf(ctx, &b, 1);
2006-04-05 08:35:53 -07:00
}