/** 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.**/#include <math.h>#include <string.h>#include <stdlib.h>#include "gd.h"/* this is used for creating images in main memory*/typedefstructfileIOCtx{gdIOCtxctx;FILE*f;}fileIOCtx;structfileIOCtx*fileIOCtxPtr;gdIOCtx*newFileCtx(FILE*f);staticintfileGetbuf(gdIOCtx*,void*,int);staticintfilePutbuf(gdIOCtx*,constvoid*,int);staticvoidfilePutchar(gdIOCtx*,int);staticintfileGetchar(gdIOCtx*ctx);staticintfileSeek(structgdIOCtx*,constint);staticlongfileTell(structgdIOCtx*);staticvoidfreeFileCtx(gdIOCtx*ctx);/* return data as a dynamic pointer */gdIOCtx*gdNewFileCtx(FILE*f){fileIOCtx*ctx;ctx=(fileIOCtx*)malloc(sizeof(fileIOCtx));if(ctx==NULL){returnNULL;}ctx->f=f;ctx->ctx.getC=fileGetchar;ctx->ctx.putC=filePutchar;ctx->ctx.getBuf=fileGetbuf;ctx->ctx.putBuf=filePutbuf;ctx->ctx.tell=fileTell;ctx->ctx.seek=fileSeek;ctx->ctx.free=freeFileCtx;return(gdIOCtx*)ctx;};staticvoidfreeFileCtx(gdIOCtx*ctx){free(ctx);}staticintfilePutbuf(gdIOCtx*ctx,constvoid*buf,intsize){fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;returnfwrite(buf,1,size,fctx->f);}staticintfileGetbuf(gdIOCtx*ctx,void*buf,intsize){fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;return(fread(buf,1,size,fctx->f));}staticvoidfilePutchar(gdIOCtx*ctx,inta){unsignedcharb;fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;b=a;putc(b,fctx->f);}staticintfileGetchar(gdIOCtx*ctx){fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;returngetc(fctx->f);}staticintfileSeek(structgdIOCtx*ctx,constintpos){fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;return(fseek(fctx->f,pos,SEEK_SET)==0);}staticlongfileTell(structgdIOCtx*ctx){fileIOCtx*fctx;fctx=(fileIOCtx*)ctx;returnftell(fctx->f);}