5
* Implements the file interface.
7
* As will all I/O modules, most functions are for local use only (called
8
* via function pointers in the I/O context).
10
* Most functions are just 'wrappers' for standard file functions.
12
* Written/Modified 1999, Philip Warner.
16
/* For platforms with incomplete ANSI defines. Fortunately,
17
SEEK_SET is defined to be zero by the standard. */
27
#include "gdhelpers.h"
29
/* this is used for creating images in main memory */
31
typedef struct fileIOCtx
38
struct fileIOCtx *fileIOCtxPtr;
40
gdIOCtx *newFileCtx (FILE * f);
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);
47
static int fileSeek (struct gdIOCtx *, const int);
48
static long fileTell (struct gdIOCtx *);
49
static void gdFreeFileCtx (gdIOCtx * ctx);
51
/* return data as a dynamic pointer */
53
gdNewFileCtx (FILE * f)
57
ctx = (fileIOCtx *) gdMalloc (sizeof (fileIOCtx));
65
ctx->ctx.getC = fileGetchar;
66
ctx->ctx.putC = filePutchar;
68
ctx->ctx.getBuf = fileGetbuf;
69
ctx->ctx.putBuf = filePutbuf;
71
ctx->ctx.tell = fileTell;
72
ctx->ctx.seek = fileSeek;
74
ctx->ctx.free = gdFreeFileCtx;
76
return (gdIOCtx *) ctx;
81
gdFreeFileCtx (gdIOCtx * ctx)
88
filePutbuf (gdIOCtx * ctx, const void *buf, int size)
91
fctx = (fileIOCtx *) ctx;
93
return fwrite (buf, 1, size, fctx->f);
98
fileGetbuf (gdIOCtx * ctx, void *buf, int size)
101
fctx = (fileIOCtx *) ctx;
103
return (fread (buf, 1, size, fctx->f));
108
filePutchar (gdIOCtx * ctx, int a)
112
fctx = (fileIOCtx *) ctx;
120
fileGetchar (gdIOCtx * ctx)
123
fctx = (fileIOCtx *) ctx;
125
return getc (fctx->f);
130
fileSeek (struct gdIOCtx *ctx, const int pos)
133
fctx = (fileIOCtx *) ctx;
135
return (fseek (fctx->f, pos, SEEK_SET) == 0);
139
fileTell (struct gdIOCtx *ctx)
142
fctx = (fileIOCtx *) ctx;
144
return ftell (fctx->f);