Back to index

php5  5.3.10
gd_io_ss.c
Go to the documentation of this file.
00001 
00002 /*
00003    * io_ss.c
00004    *
00005    * Implements the Source/Sink interface.
00006    *
00007    * As will all I/O modules, most functions are for local use only (called
00008    * via function pointers in the I/O context).
00009    *
00010    * The Source/Sink model is the primary 'user' interface for alternate data
00011    * sources; the IOCtx interface is intended (at least in version 1.5) to be
00012    * used internally until it settles down a bit.
00013    *
00014    * This module just layers the Source/Sink interface on top of the IOCtx; no
00015    * support is provided for tell/seek, so GD2 writing is not possible, and
00016    * retrieving parts of GD2 files is also not possible.
00017    *
00018    * A new SS context does not need to be created with both a Source and a Sink.
00019    *
00020    * Written/Modified 1999, Philip Warner.
00021    *
00022  */
00023 
00024 #include <math.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include "gd.h"
00028 #include "gdhelpers.h"
00029 
00030 /* this is used for creating images in main memory */
00031 
00032 typedef struct ssIOCtx
00033 {
00034        gdIOCtx ctx;
00035        gdSourcePtr src;
00036        gdSinkPtr snk;
00037 } ssIOCtx;
00038 
00039 typedef struct ssIOCtx *ssIOCtxPtr;
00040 
00041 gdIOCtx *gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk);
00042 
00043 static int sourceGetbuf (gdIOCtx *, void *, int);
00044 static int sourceGetchar (gdIOCtx * ctx);
00045 static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size);
00046 static void sinkPutchar (gdIOCtx * ctx, int a);
00047 static void gdFreeSsCtx (gdIOCtx * ctx);
00048 
00049 /* return data as a dynamic pointer */
00050 gdIOCtx * gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
00051 {
00052        ssIOCtxPtr ctx;
00053 
00054        ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
00055 
00056        ctx->src = src;
00057        ctx->snk = snk;
00058 
00059        ctx->ctx.getC = sourceGetchar;
00060        ctx->ctx.getBuf = sourceGetbuf;
00061 
00062        ctx->ctx.putC = sinkPutchar;
00063        ctx->ctx.putBuf = sinkPutbuf;
00064 
00065        ctx->ctx.tell = NULL;
00066        ctx->ctx.seek = NULL;
00067 
00068        ctx->ctx.gd_free = gdFreeSsCtx;
00069 
00070        return (gdIOCtx *) ctx;
00071 }
00072 
00073 static void gdFreeSsCtx (gdIOCtx * ctx)
00074 {
00075        gdFree(ctx);
00076 }
00077 
00078 
00079 static int sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
00080 {
00081        ssIOCtx *lctx;
00082        int res;
00083 
00084        lctx = (ssIOCtx *) ctx;
00085 
00086        res = ((lctx->src->source) (lctx->src->context, buf, size));
00087 
00088        /*
00089         * Translate the return values from the Source object:
00090         * 0 is EOF, -1 is error
00091         */
00092 
00093        if (res == 0) {
00094               return EOF;
00095        } else if (res < 0) {
00096               return 0;
00097        } else {
00098               return res;
00099        }
00100 }
00101 
00102 static int sourceGetchar (gdIOCtx * ctx)
00103 {
00104        int res;
00105        unsigned char buf;
00106 
00107        res = sourceGetbuf (ctx, &buf, 1);
00108 
00109        if (res == 1) {
00110               return buf;
00111        } else {
00112               return EOF;
00113        }
00114 }
00115 
00116 static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
00117 {
00118        ssIOCtxPtr lctx;
00119        int res;
00120 
00121        lctx = (ssIOCtx *) ctx;
00122 
00123        res = (lctx->snk->sink) (lctx->snk->context, buf, size);
00124 
00125        if (res <= 0) {
00126               return 0;
00127        } else {
00128               return res;
00129        }
00130 }
00131 
00132 static void sinkPutchar (gdIOCtx * ctx, int a)
00133 {
00134        unsigned char b;
00135 
00136        b = a;
00137        sinkPutbuf (ctx, &b, 1);
00138 }