|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
* io.c
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Implements the simple I/O 'helper' routines.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Not really essential, but these routines were used extensively in GD,
|
|
Packit |
ed3af9 |
* so they were moved here. They also make IOCtx calls look better...
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Written (or, at least, moved) 1999, Philip Warner.
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
ed3af9 |
# include "config.h"
|
|
Packit |
ed3af9 |
#endif
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#include <math.h>
|
|
Packit |
ed3af9 |
#include <string.h>
|
|
Packit |
ed3af9 |
#include <stdlib.h>
|
|
Packit |
ed3af9 |
#include "gd.h"
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/* Use this for commenting out debug-print statements. */
|
|
Packit |
ed3af9 |
/* Just use the first '#define' to allow all the prints... */
|
|
Packit |
ed3af9 |
/*#define IO_DBG(s) (s) */
|
|
Packit |
ed3af9 |
#define IO_DBG(s)
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#define GD_IO_EOF_CHK(r) \
|
|
Packit |
ed3af9 |
if(r == EOF) { \
|
|
Packit |
ed3af9 |
return 0; \
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
* Write out a word to the I/O context pointer
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
void Putword(int w, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
unsigned char buf[2];
|
|
Packit |
ed3af9 |
buf[0] = w & 0xff;
|
|
Packit |
ed3af9 |
buf[1] = (w / 256) & 0xff;
|
|
Packit |
ed3af9 |
(ctx->putBuf)(ctx, (char *)buf, 2);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
void Putchar(int c, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, c & 0xff);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
void gdPutC(const unsigned char c, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, c);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
void gdPutWord (int w, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
IO_DBG(printf("Putting word...\n"));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char)(w >> 8));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char)(w & 0xFF));
|
|
Packit |
ed3af9 |
IO_DBG(printf("put.\n"));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
void gdPutInt (int w, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
IO_DBG(printf("Putting int...\n"));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char) (w >> 24));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char) ((w >> 16) & 0xFF));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char) ((w >> 8) & 0xFF));
|
|
Packit |
ed3af9 |
(ctx->putC)(ctx, (unsigned char) (w & 0xFF));
|
|
Packit |
ed3af9 |
IO_DBG(printf("put.\n"));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetC(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
return ((ctx->getC)(ctx));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetByte(int *result, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result = r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return 1;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetWord(int *result, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result = r << 8;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result += r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return 1;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int high = 0, low = 0;
|
|
Packit |
ed3af9 |
low = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (low == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
high = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (high == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (result) {
|
|
Packit |
ed3af9 |
*result = (high << 8) | low;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return 1;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetInt(int *result, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result = r << 24;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result += r << 16;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result += r << 8;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
r = (ctx->getC)(ctx);
|
|
Packit |
ed3af9 |
if(r == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
*result += r;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return 1;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int c = 0;
|
|
Packit |
ed3af9 |
unsigned int r = 0;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
c = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (c == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
r |= (c << 24);
|
|
Packit |
ed3af9 |
r >>= 8;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
c = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (c == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
r |= (c << 24);
|
|
Packit |
ed3af9 |
r >>= 8;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
c = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (c == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
r |= (c << 24);
|
|
Packit |
ed3af9 |
r >>= 8;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
c = (ctx->getC) (ctx);
|
|
Packit |
ed3af9 |
if (c == EOF) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
r |= (c << 24);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (result) {
|
|
Packit |
ed3af9 |
*result = (signed int)r;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return 1;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
IO_DBG(printf("Putting buf...\n"));
|
|
Packit |
ed3af9 |
return (ctx->putBuf)(ctx, buf, size);
|
|
Packit |
ed3af9 |
IO_DBG(printf("put.\n"));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
return (ctx->getBuf)(ctx, buf, size);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
int gdSeek(gdIOCtx *ctx, const int pos)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
IO_DBG(printf("Seeking...\n"));
|
|
Packit |
ed3af9 |
return ((ctx->seek)(ctx, pos));
|
|
Packit |
ed3af9 |
IO_DBG(printf("Done.\n"));
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
long gdTell(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
IO_DBG(printf("Telling...\n"));
|
|
Packit |
ed3af9 |
return ((ctx->tell)(ctx));
|
|
Packit |
ed3af9 |
IO_DBG(printf("told.\n"));
|
|
Packit |
ed3af9 |
}
|