Back to index

php5  5.3.10
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 /* For platforms with incomplete ANSI defines. Fortunately,
00017    SEEK_SET is defined to be zero by the standard. */
00018 
00019 #ifndef SEEK_SET
00020 #define SEEK_SET 0
00021 #endif /* SEEK_SET */
00022 
00023 #include <math.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include "gd.h"
00027 #include "gdhelpers.h"
00028 
00029 /* this is used for creating images in main memory */
00030 
00031 typedef struct fileIOCtx
00032 {
00033        gdIOCtx ctx;
00034        FILE *f;
00035 } fileIOCtx;
00036 
00037 gdIOCtx *newFileCtx (FILE * f);
00038 
00039 static int fileGetbuf (gdIOCtx *, void *, int);
00040 static int filePutbuf (gdIOCtx *, const void *, int);
00041 static void filePutchar (gdIOCtx *, int);
00042 static int fileGetchar (gdIOCtx * ctx);
00043 
00044 static int fileSeek (struct gdIOCtx *, const int);
00045 static long fileTell (struct gdIOCtx *);
00046 static void gdFreeFileCtx (gdIOCtx * ctx);
00047 
00048 /* return data as a dynamic pointer */
00049 gdIOCtx * gdNewFileCtx (FILE * f)
00050 {
00051        fileIOCtx *ctx;
00052 
00053        ctx = (fileIOCtx *) gdMalloc(sizeof (fileIOCtx));
00054 
00055        ctx->f = f;
00056 
00057        ctx->ctx.getC = fileGetchar;
00058        ctx->ctx.putC = filePutchar;
00059 
00060        ctx->ctx.getBuf = fileGetbuf;
00061        ctx->ctx.putBuf = filePutbuf;
00062 
00063        ctx->ctx.tell = fileTell;
00064        ctx->ctx.seek = fileSeek;
00065 
00066        ctx->ctx.gd_free = gdFreeFileCtx;
00067 
00068        return (gdIOCtx *) ctx;
00069 }
00070 
00071 static void gdFreeFileCtx (gdIOCtx * ctx)
00072 {
00073        gdFree(ctx);
00074 }
00075 
00076 
00077 static int filePutbuf (gdIOCtx * ctx, const void *buf, int size)
00078 {
00079        fileIOCtx *fctx;
00080        fctx = (fileIOCtx *) ctx;
00081 
00082        return fwrite(buf, 1, size, fctx->f);
00083 
00084 }
00085 
00086 static int fileGetbuf (gdIOCtx * ctx, void *buf, int size)
00087 {
00088        fileIOCtx *fctx;
00089        fctx = (fileIOCtx *) ctx;
00090 
00091        return fread(buf, 1, size, fctx->f);
00092 }
00093 
00094 static void filePutchar (gdIOCtx * ctx, int a)
00095 {
00096        unsigned char b;
00097        fileIOCtx *fctx;
00098        fctx = (fileIOCtx *) ctx;
00099 
00100        b = a;
00101 
00102        putc (b, fctx->f);
00103 }
00104 
00105 static int fileGetchar (gdIOCtx * ctx)
00106 {
00107        fileIOCtx *fctx;
00108        fctx = (fileIOCtx *) ctx;
00109 
00110        return getc (fctx->f);
00111 }
00112 
00113 
00114 static int fileSeek (struct gdIOCtx *ctx, const int pos)
00115 {
00116        fileIOCtx *fctx;
00117        fctx = (fileIOCtx *) ctx;
00118 
00119        return (fseek (fctx->f, pos, SEEK_SET) == 0);
00120 }
00121 
00122 static long fileTell (struct gdIOCtx *ctx)
00123 {
00124        fileIOCtx *fctx;
00125        fctx = (fileIOCtx *) ctx;
00126 
00127        return ftell (fctx->f);
00128 }