home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gd201.zip / gd-2.0.1 / gd_io_ss.c < prev    next >
C/C++ Source or Header  |  2001-04-03  |  3KB  |  166 lines

  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.   }
  38. ssIOCtx;
  39.  
  40. typedef struct ssIOCtx *ssIOCtxPtr;
  41.  
  42. gdIOCtx *gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk);
  43.  
  44. static int sourceGetbuf (gdIOCtx *, void *, int);
  45. static int sourceGetchar (gdIOCtx * ctx);
  46. static int sinkPutbuf (gdIOCtx * ctx, const void *buf, int size);
  47. static void sinkPutchar (gdIOCtx * ctx, int a);
  48. static void gdFreeSsCtx (gdIOCtx * ctx);
  49.  
  50. /* return data as a dynamic pointer */
  51. gdIOCtx *
  52. gdNewSSCtx (gdSourcePtr src, gdSinkPtr snk)
  53. {
  54.   ssIOCtxPtr ctx;
  55.  
  56.   ctx = (ssIOCtxPtr) gdMalloc (sizeof (ssIOCtx));
  57.   if (ctx == NULL)
  58.     {
  59.       return NULL;
  60.     }
  61.  
  62.   ctx->src = src;
  63.   ctx->snk = snk;
  64.  
  65.   ctx->ctx.getC = sourceGetchar;
  66.   ctx->ctx.getBuf = sourceGetbuf;
  67.  
  68.   ctx->ctx.putC = sinkPutchar;
  69.   ctx->ctx.putBuf = sinkPutbuf;
  70.  
  71.   ctx->ctx.tell = NULL;
  72.   ctx->ctx.seek = NULL;
  73.  
  74.   ctx->ctx.free = gdFreeSsCtx;
  75.  
  76.   return (gdIOCtx *) ctx;
  77. }
  78.  
  79. static
  80. void
  81. gdFreeSsCtx (gdIOCtx * ctx)
  82. {
  83.   gdFree (ctx);
  84. }
  85.  
  86.  
  87. static int
  88. sourceGetbuf (gdIOCtx * ctx, void *buf, int size)
  89. {
  90.   ssIOCtx *lctx;
  91.   int res;
  92.  
  93.   lctx = (ssIOCtx *) ctx;
  94.  
  95.   res = ((lctx->src->source) (lctx->src->context, buf, size));
  96.  
  97. /*
  98.    ** Translate the return values from the Source object: 
  99.    ** 0 is EOF, -1 is error
  100.  */
  101.  
  102.   if (res == 0)
  103.     {
  104.       return EOF;
  105.     }
  106.   else if (res < 0)
  107.     {
  108.       return 0;
  109.     }
  110.   else
  111.     {
  112.       return res;
  113.     };
  114.  
  115. }
  116.  
  117. static int
  118. sourceGetchar (gdIOCtx * ctx)
  119. {
  120.   int res;
  121.   unsigned char buf;
  122.  
  123.   res = sourceGetbuf (ctx, &buf, 1);
  124.  
  125.   if (res == 1)
  126.     {
  127.       return buf;
  128.     }
  129.   else
  130.     {
  131.       return EOF;
  132.     };
  133.  
  134. }
  135.  
  136. static int
  137. sinkPutbuf (gdIOCtx * ctx, const void *buf, int size)
  138. {
  139.   ssIOCtxPtr lctx;
  140.   int res;
  141.  
  142.   lctx = (ssIOCtx *) ctx;
  143.  
  144.   res = (lctx->snk->sink) (lctx->snk->context, buf, size);
  145.  
  146.   if (res <= 0)
  147.     {
  148.       return 0;
  149.     }
  150.   else
  151.     {
  152.       return res;
  153.     };
  154.  
  155. }
  156.  
  157. static void
  158. sinkPutchar (gdIOCtx * ctx, int a)
  159. {
  160.   unsigned char b;
  161.  
  162.   b = a;
  163.   sinkPutbuf (ctx, &b, 1);
  164.  
  165. }
  166.