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

  1.  
  2. /*
  3.    * io_file.c
  4.    *
  5.    * Implements the file 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.    * Most functions are just 'wrappers' for standard file functions.
  11.    *
  12.    * Written/Modified 1999, Philip Warner.
  13.    *
  14.  */
  15.  
  16. /* For platforms with incomplete ANSI defines. Fortunately,
  17.    SEEK_SET is defined to be zero by the standard. */
  18.  
  19. #ifndef SEEK_SET
  20. #define SEEK_SET 0
  21. #endif /* SEEK_SET */
  22.  
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "gd.h"
  27. #include "gdhelpers.h"
  28.  
  29. /* this is used for creating images in main memory */
  30.  
  31. typedef struct fileIOCtx
  32.   {
  33.     gdIOCtx ctx;
  34.     FILE *f;
  35.   }
  36. fileIOCtx;
  37.  
  38. struct fileIOCtx *fileIOCtxPtr;
  39.  
  40. gdIOCtx *newFileCtx (FILE * f);
  41.  
  42. static int fileGetbuf (gdIOCtx *, void *, int);
  43. static int filePutbuf (gdIOCtx *, const void *, int);
  44. static void filePutchar (gdIOCtx *, int);
  45. static int fileGetchar (gdIOCtx * ctx);
  46.  
  47. static int fileSeek (struct gdIOCtx *, const int);
  48. static long fileTell (struct gdIOCtx *);
  49. static void gdFreeFileCtx (gdIOCtx * ctx);
  50.  
  51. /* return data as a dynamic pointer */
  52. gdIOCtx *
  53. gdNewFileCtx (FILE * f)
  54. {
  55.   fileIOCtx *ctx;
  56.  
  57.   ctx = (fileIOCtx *) gdMalloc (sizeof (fileIOCtx));
  58.   if (ctx == NULL)
  59.     {
  60.       return NULL;
  61.     }
  62.  
  63.   ctx->f = f;
  64.  
  65.   ctx->ctx.getC = fileGetchar;
  66.   ctx->ctx.putC = filePutchar;
  67.  
  68.   ctx->ctx.getBuf = fileGetbuf;
  69.   ctx->ctx.putBuf = filePutbuf;
  70.  
  71.   ctx->ctx.tell = fileTell;
  72.   ctx->ctx.seek = fileSeek;
  73.  
  74.   ctx->ctx.free = gdFreeFileCtx;
  75.  
  76.   return (gdIOCtx *) ctx;
  77. }
  78.  
  79. static
  80. void
  81. gdFreeFileCtx (gdIOCtx * ctx)
  82. {
  83.   gdFree (ctx);
  84. }
  85.  
  86.  
  87. static int
  88. filePutbuf (gdIOCtx * ctx, const void *buf, int size)
  89. {
  90.   fileIOCtx *fctx;
  91.   fctx = (fileIOCtx *) ctx;
  92.  
  93.   return fwrite (buf, 1, size, fctx->f);
  94.  
  95. }
  96.  
  97. static int
  98. fileGetbuf (gdIOCtx * ctx, void *buf, int size)
  99. {
  100.   fileIOCtx *fctx;
  101.   fctx = (fileIOCtx *) ctx;
  102.  
  103.   return (fread (buf, 1, size, fctx->f));
  104.  
  105. }
  106.  
  107. static void
  108. filePutchar (gdIOCtx * ctx, int a)
  109. {
  110.   unsigned char b;
  111.   fileIOCtx *fctx;
  112.   fctx = (fileIOCtx *) ctx;
  113.  
  114.   b = a;
  115.  
  116.   putc (b, fctx->f);
  117. }
  118.  
  119. static int
  120. fileGetchar (gdIOCtx * ctx)
  121. {
  122.   fileIOCtx *fctx;
  123.   fctx = (fileIOCtx *) ctx;
  124.  
  125.   return getc (fctx->f);
  126. }
  127.  
  128.  
  129. static int
  130. fileSeek (struct gdIOCtx *ctx, const int pos)
  131. {
  132.   fileIOCtx *fctx;
  133.   fctx = (fileIOCtx *) ctx;
  134.  
  135.   return (fseek (fctx->f, pos, SEEK_SET) == 0);
  136. }
  137.  
  138. static long
  139. fileTell (struct gdIOCtx *ctx)
  140. {
  141.   fileIOCtx *fctx;
  142.   fctx = (fileIOCtx *) ctx;
  143.  
  144.   return ftell (fctx->f);
  145. }
  146.