Back to index

tetex-bin  3.0
gd_io_file.c
Go to the documentation of this file.
00001 
00002 /*
00003    * io_file.c
00004    *
00005    * Implements the file 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    * Most functions are just 'wrappers' for standard file functions.
00011    *
00012    * Written/Modified 1999, Philip Warner.
00013    *
00014  */
00015 
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif
00019 
00020 /* For platforms with incomplete ANSI defines. Fortunately,
00021    SEEK_SET is defined to be zero by the standard. */
00022 
00023 #ifndef SEEK_SET
00024 #define SEEK_SET 0
00025 #endif /* SEEK_SET */
00026 
00027 #include <math.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include "gd.h"
00031 #include "gdhelpers.h"
00032 
00033 /* this is used for creating images in main memory */
00034 
00035 typedef struct fileIOCtx
00036 {
00037   gdIOCtx ctx;
00038   FILE *f;
00039 }
00040 fileIOCtx;
00041 
00042 gdIOCtx *newFileCtx (FILE * f);
00043 
00044 static int fileGetbuf (gdIOCtx *, void *, int);
00045 static int filePutbuf (gdIOCtx *, const void *, int);
00046 static void filePutchar (gdIOCtx *, int);
00047 static int fileGetchar (gdIOCtx * ctx);
00048 
00049 static int fileSeek (struct gdIOCtx *, const int);
00050 static long fileTell (struct gdIOCtx *);
00051 static void gdFreeFileCtx (gdIOCtx * ctx);
00052 
00053 /* return data as a dynamic pointer */
00054 BGD_DECLARE(gdIOCtx *) gdNewFileCtx (FILE * f)
00055 {
00056   fileIOCtx *ctx;
00057 
00058   ctx = (fileIOCtx *) gdMalloc (sizeof (fileIOCtx));
00059   if (ctx == NULL)
00060     {
00061       return NULL;
00062     }
00063 
00064   ctx->f = f;
00065 
00066   ctx->ctx.getC = fileGetchar;
00067   ctx->ctx.putC = filePutchar;
00068 
00069   ctx->ctx.getBuf = fileGetbuf;
00070   ctx->ctx.putBuf = filePutbuf;
00071 
00072   ctx->ctx.tell = fileTell;
00073   ctx->ctx.seek = fileSeek;
00074 
00075   ctx->ctx.gd_free = gdFreeFileCtx;
00076 
00077   return (gdIOCtx *) ctx;
00078 }
00079 
00080 static void
00081 gdFreeFileCtx (gdIOCtx * ctx)
00082 {
00083   gdFree (ctx);
00084 }
00085 
00086 
00087 static int
00088 filePutbuf (gdIOCtx * ctx, const void *buf, int size)
00089 {
00090   fileIOCtx *fctx;
00091   fctx = (fileIOCtx *) ctx;
00092 
00093   return fwrite (buf, 1, size, fctx->f);
00094 
00095 }
00096 
00097 static int
00098 fileGetbuf (gdIOCtx * ctx, void *buf, int size)
00099 {
00100   fileIOCtx *fctx;
00101   fctx = (fileIOCtx *) ctx;
00102 
00103   return (fread (buf, 1, size, fctx->f));
00104 
00105 }
00106 
00107 static void
00108 filePutchar (gdIOCtx * ctx, int a)
00109 {
00110   unsigned char b;
00111   fileIOCtx *fctx;
00112   fctx = (fileIOCtx *) ctx;
00113 
00114   b = a;
00115 
00116   putc (b, fctx->f);
00117 }
00118 
00119 static int
00120 fileGetchar (gdIOCtx * ctx)
00121 {
00122   fileIOCtx *fctx;
00123   fctx = (fileIOCtx *) ctx;
00124 
00125   return getc (fctx->f);
00126 }
00127 
00128 
00129 static int
00130 fileSeek (struct gdIOCtx *ctx, const int pos)
00131 {
00132   fileIOCtx *fctx;
00133   fctx = (fileIOCtx *) ctx;
00134   return (fseek (fctx->f, pos, SEEK_SET) == 0);
00135 }
00136 
00137 static long
00138 fileTell (struct gdIOCtx *ctx)
00139 {
00140   fileIOCtx *fctx;
00141   fctx = (fileIOCtx *) ctx;
00142 
00143   return ftell (fctx->f);
00144 }