Back to index

plt-scheme  4.2.1
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 struct fileIOCtx *fileIOCtxPtr;
00043 
00044 gdIOCtx *newFileCtx (FILE * f);
00045 
00046 static int fileGetbuf (gdIOCtx *, void *, int);
00047 static int filePutbuf (gdIOCtx *, const void *, int);
00048 static void filePutchar (gdIOCtx *, int);
00049 static int fileGetchar (gdIOCtx * ctx);
00050 
00051 static int fileSeek (struct gdIOCtx *, const int);
00052 static long fileTell (struct gdIOCtx *);
00053 static void gdFreeFileCtx (gdIOCtx * ctx);
00054 
00055 /* return data as a dynamic pointer */
00056 gdIOCtx *
00057 gdNewFileCtx (FILE * f)
00058 {
00059   fileIOCtx *ctx;
00060 
00061   ctx = (fileIOCtx *) gdMalloc (sizeof (fileIOCtx));
00062   if (ctx == NULL)
00063     {
00064       return NULL;
00065     }
00066 
00067   ctx->f = f;
00068 
00069   ctx->ctx.getC = fileGetchar;
00070   ctx->ctx.putC = filePutchar;
00071 
00072   ctx->ctx.getBuf = fileGetbuf;
00073   ctx->ctx.putBuf = filePutbuf;
00074 
00075   ctx->ctx.tell = fileTell;
00076   ctx->ctx.seek = fileSeek;
00077 
00078   ctx->ctx.gd_free = gdFreeFileCtx;
00079 
00080   return (gdIOCtx *) ctx;
00081 }
00082 
00083 static void
00084 gdFreeFileCtx (gdIOCtx * ctx)
00085 {
00086   gdFree (ctx);
00087 }
00088 
00089 
00090 static int
00091 filePutbuf (gdIOCtx * ctx, const void *buf, int size)
00092 {
00093   fileIOCtx *fctx;
00094   fctx = (fileIOCtx *) ctx;
00095 
00096   return fwrite (buf, 1, size, fctx->f);
00097 
00098 }
00099 
00100 static int
00101 fileGetbuf (gdIOCtx * ctx, void *buf, int size)
00102 {
00103   fileIOCtx *fctx;
00104   fctx = (fileIOCtx *) ctx;
00105 
00106   return (fread (buf, 1, size, fctx->f));
00107 
00108 }
00109 
00110 static void
00111 filePutchar (gdIOCtx * ctx, int a)
00112 {
00113   unsigned char b;
00114   fileIOCtx *fctx;
00115   fctx = (fileIOCtx *) ctx;
00116 
00117   b = a;
00118 
00119   putc (b, fctx->f);
00120 }
00121 
00122 static int
00123 fileGetchar (gdIOCtx * ctx)
00124 {
00125   fileIOCtx *fctx;
00126   fctx = (fileIOCtx *) ctx;
00127 
00128   return getc (fctx->f);
00129 }
00130 
00131 
00132 static int
00133 fileSeek (struct gdIOCtx *ctx, const int pos)
00134 {
00135   fileIOCtx *fctx;
00136   fctx = (fileIOCtx *) ctx;
00137   return (fseek (fctx->f, pos, SEEK_SET) == 0);
00138 }
00139 
00140 static long
00141 fileTell (struct gdIOCtx *ctx)
00142 {
00143   fileIOCtx *fctx;
00144   fctx = (fileIOCtx *) ctx;
00145 
00146   return ftell (fctx->f);
00147 }