2006-04-05 08:35:53 -07:00
|
|
|
/*
|
2007-12-21 14:02:50 -08:00
|
|
|
* io_file.c
|
|
|
|
*
|
|
|
|
* Implements the file interface.
|
|
|
|
*
|
|
|
|
* As will all I/O modules, most functions are for local use only (called
|
|
|
|
* via function pointers in the I/O context).
|
|
|
|
*
|
|
|
|
* Most functions are just 'wrappers' for standard file functions.
|
|
|
|
*
|
|
|
|
* Written/Modified 1999, Philip Warner.
|
|
|
|
*
|
2006-04-05 08:42:56 -07:00
|
|
|
*/
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2006-04-05 08:44:56 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-12-21 14:02:50 -08:00
|
|
|
# include "config.h"
|
2006-04-05 08:44:56 -07:00
|
|
|
#endif
|
|
|
|
|
2006-04-05 08:38:05 -07:00
|
|
|
/* For platforms with incomplete ANSI defines. Fortunately,
|
2007-12-21 14:02:50 -08:00
|
|
|
* SEEK_SET is defined to be zero by the standard. */
|
2006-04-05 08:38:05 -07:00
|
|
|
|
|
|
|
#ifndef SEEK_SET
|
2007-12-21 14:02:50 -08:00
|
|
|
# define SEEK_SET 0
|
2006-04-05 08:38:05 -07:00
|
|
|
#endif /* SEEK_SET */
|
|
|
|
|
2006-04-05 08:35:53 -07:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "gd.h"
|
2006-04-05 08:41:55 -07:00
|
|
|
#include "gdhelpers.h"
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2006-04-05 08:42:56 -07:00
|
|
|
/* this is used for creating images in main memory */
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2013-04-03 05:23:11 -07:00
|
|
|
typedef struct fileIOCtx {
|
2007-12-21 14:02:50 -08:00
|
|
|
gdIOCtx ctx;
|
|
|
|
FILE *f;
|
2006-04-05 08:46:42 -07:00
|
|
|
}
|
2006-04-05 08:42:56 -07:00
|
|
|
fileIOCtx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
gdIOCtxPtr newFileCtx(FILE *f);
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int fileGetbuf(gdIOCtxPtr, void *, int);
|
|
|
|
static int filePutbuf(gdIOCtxPtr, const void *, int);
|
|
|
|
static void filePutchar(gdIOCtxPtr, int);
|
|
|
|
static int fileGetchar(gdIOCtxPtr ctx);
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int fileSeek(gdIOCtxPtr, const int);
|
|
|
|
static long fileTell(gdIOCtxPtr);
|
|
|
|
static void gdFreeFileCtx(gdIOCtxPtr ctx);
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2016-07-20 12:13:38 -07:00
|
|
|
/*
|
|
|
|
Function: gdNewFileCtx
|
|
|
|
|
|
|
|
Return data as a dynamic pointer.
|
|
|
|
*/
|
2021-05-28 18:19:20 -07:00
|
|
|
BGD_DECLARE(gdIOCtxPtr) gdNewFileCtx(FILE *f)
|
2006-04-05 08:42:56 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2009-06-18 06:35:26 -07:00
|
|
|
if (f == NULL) return NULL;
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx = (fileIOCtx *)gdMalloc(sizeof(fileIOCtx));
|
|
|
|
if(ctx == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx->f = f;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx->ctx.getC = fileGetchar;
|
|
|
|
ctx->ctx.putC = filePutchar;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx->ctx.getBuf = fileGetbuf;
|
|
|
|
ctx->ctx.putBuf = filePutbuf;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx->ctx.tell = fileTell;
|
|
|
|
ctx->ctx.seek = fileSeek;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
ctx->ctx.gd_free = gdFreeFileCtx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
return (gdIOCtxPtr)ctx;
|
2006-04-05 08:38:05 -07:00
|
|
|
}
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static void gdFreeFileCtx(gdIOCtxPtr ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
gdFree(ctx);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int filePutbuf(gdIOCtxPtr ctx, const void *buf, int size)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
return fwrite(buf, 1, size, fctx->f);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int fileGetbuf(gdIOCtxPtr ctx, void *buf, int size)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
return (fread(buf, 1, size, fctx->f));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static void filePutchar(gdIOCtxPtr ctx, int a)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
unsigned char b;
|
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
b = a;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
putc(b, fctx->f);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int fileGetchar(gdIOCtxPtr ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
return getc(fctx->f);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static int fileSeek(gdIOCtxPtr ctx, const int pos)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
|
|
|
return (fseek(fctx->f, pos, SEEK_SET) == 0);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2021-05-28 18:19:20 -07:00
|
|
|
static long fileTell(gdIOCtxPtr ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-12-21 14:02:50 -08:00
|
|
|
fileIOCtx *fctx;
|
|
|
|
fctx = (fileIOCtx *)ctx;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-12-21 14:02:50 -08:00
|
|
|
return ftell(fctx->f);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|