|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
* io_file.c
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Implements the file interface.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* As will all I/O modules, most functions are for local use only (called
|
|
Packit |
ed3af9 |
* via function pointers in the I/O context).
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Most functions are just 'wrappers' for standard file functions.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Written/Modified 1999, Philip Warner.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
ed3af9 |
# include "config.h"
|
|
Packit |
ed3af9 |
#endif
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/* For platforms with incomplete ANSI defines. Fortunately,
|
|
Packit |
ed3af9 |
* SEEK_SET is defined to be zero by the standard. */
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#ifndef SEEK_SET
|
|
Packit |
ed3af9 |
# define SEEK_SET 0
|
|
Packit |
ed3af9 |
#endif /* SEEK_SET */
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#include <math.h>
|
|
Packit |
ed3af9 |
#include <string.h>
|
|
Packit |
ed3af9 |
#include <stdlib.h>
|
|
Packit |
ed3af9 |
#include "gd.h"
|
|
Packit |
ed3af9 |
#include "gdhelpers.h"
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/* this is used for creating images in main memory */
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
typedef struct fileIOCtx {
|
|
Packit |
ed3af9 |
gdIOCtx ctx;
|
|
Packit |
ed3af9 |
FILE *f;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
fileIOCtx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
gdIOCtx *newFileCtx(FILE *f);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int fileGetbuf(gdIOCtx *, void *, int);
|
|
Packit |
ed3af9 |
static int filePutbuf(gdIOCtx *, const void *, int);
|
|
Packit |
ed3af9 |
static void filePutchar(gdIOCtx *, int);
|
|
Packit |
ed3af9 |
static int fileGetchar(gdIOCtx *ctx);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int fileSeek(struct gdIOCtx *, const int);
|
|
Packit |
ed3af9 |
static long fileTell(struct gdIOCtx *);
|
|
Packit |
ed3af9 |
static void gdFreeFileCtx(gdIOCtx *ctx);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
Function: gdNewFileCtx
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
Return data as a dynamic pointer.
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
BGD_DECLARE(gdIOCtx *) gdNewFileCtx(FILE *f)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (f == NULL) return NULL;
|
|
Packit |
ed3af9 |
ctx = (fileIOCtx *)gdMalloc(sizeof(fileIOCtx));
|
|
Packit |
ed3af9 |
if(ctx == NULL) {
|
|
Packit |
ed3af9 |
return NULL;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->f = f;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.getC = fileGetchar;
|
|
Packit |
ed3af9 |
ctx->ctx.putC = filePutchar;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.getBuf = fileGetbuf;
|
|
Packit |
ed3af9 |
ctx->ctx.putBuf = filePutbuf;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.tell = fileTell;
|
|
Packit |
ed3af9 |
ctx->ctx.seek = fileSeek;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.gd_free = gdFreeFileCtx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return (gdIOCtx *)ctx;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static void gdFreeFileCtx(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
gdFree(ctx);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int filePutbuf(gdIOCtx *ctx, const void *buf, int size)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return fwrite(buf, 1, size, fctx->f);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int fileGetbuf(gdIOCtx *ctx, void *buf, int size)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return (fread(buf, 1, size, fctx->f));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static void filePutchar(gdIOCtx *ctx, int a)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
unsigned char b;
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
b = a;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
putc(b, fctx->f);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int fileGetchar(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return getc(fctx->f);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int fileSeek(struct gdIOCtx *ctx, const int pos)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
return (fseek(fctx->f, pos, SEEK_SET) == 0);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static long fileTell (struct gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
fileIOCtx *fctx;
|
|
Packit |
ed3af9 |
fctx = (fileIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return ftell(fctx->f);
|
|
Packit |
ed3af9 |
}
|