libgd/src/gd_io.c

213 lines
3.2 KiB
C
Raw Permalink Normal View History

2008-03-12 01:17:25 -07:00
/*
2007-10-28 14:15:15 -07:00
* io.c
*
* Implements the simple I/O 'helper' routines.
2007-10-28 15:04:36 -07:00
*
2007-10-28 14:15:15 -07:00
* Not really essential, but these routines were used extensively in GD,
* so they were moved here. They also make IOCtx calls look better...
2007-10-28 15:04:36 -07:00
*
2007-10-28 14:15:15 -07:00
* Written (or, at least, moved) 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
# include "config.h"
2006-04-05 08:44:56 -07:00
#endif
2006-04-05 08:35:53 -07:00
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "gd.h"
/* Use this for commenting out debug-print statements. */
/* Just use the first '#define' to allow all the prints... */
/*#define IO_DBG(s) (s) */
#define IO_DBG(s)
#define GD_IO_EOF_CHK(r) \
2007-10-28 14:15:15 -07:00
if(r == EOF) { \
return 0; \
}
2006-04-05 08:35:53 -07:00
2007-10-28 14:15:15 -07:00
void gdPutC(const unsigned char c, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
(ctx->putC)(ctx, c);
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
void gdPutWord (int w, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
IO_DBG(printf("Putting word...\n"));
(ctx->putC)(ctx, (unsigned char)(w >> 8));
(ctx->putC)(ctx, (unsigned char)(w & 0xFF));
IO_DBG(printf("put.\n"));
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
void gdPutInt (int w, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
IO_DBG(printf("Putting int...\n"));
(ctx->putC)(ctx, (unsigned char) (w >> 24));
(ctx->putC)(ctx, (unsigned char) ((w >> 16) & 0xFF));
(ctx->putC)(ctx, (unsigned char) ((w >> 8) & 0xFF));
(ctx->putC)(ctx, (unsigned char) (w & 0xFF));
IO_DBG(printf("put.\n"));
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
int gdGetC(gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
return ((ctx->getC)(ctx));
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
int gdGetByte(int *result, gdIOCtx *ctx)
{
int r;
2008-03-12 01:17:25 -07:00
2007-10-28 14:15:15 -07:00
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
2006-04-05 08:35:53 -07:00
2007-10-28 14:15:15 -07:00
*result = r;
2006-04-05 08:35:53 -07:00
2007-10-28 14:15:15 -07:00
return 1;
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
int gdGetWord(int *result, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
int r;
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result = r << 8;
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result += r;
return 1;
2006-04-05 08:35:53 -07:00
}
2008-03-12 01:17:25 -07:00
int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
{
2013-02-14 22:39:38 -08:00
int high = 0, low = 0;
low = (ctx->getC) (ctx);
if (low == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
high = (ctx->getC) (ctx);
if (high == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
if (result) {
*result = (high << 8) | low;
}
return 1;
}
2007-10-28 14:15:15 -07:00
int gdGetInt(int *result, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
unsigned int r;
2007-10-28 14:15:15 -07:00
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result = r << 24;
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result += r << 16;
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result += r << 8;
r = (ctx->getC)(ctx);
if(r == EOF) {
return 0;
}
*result += r;
return 1;
2006-04-05 08:35:53 -07:00
}
2008-03-12 01:17:25 -07:00
int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
{
unsigned int c;
unsigned int r = 0;
2007-10-28 14:15:15 -07:00
c = (ctx->getC) (ctx);
if (c == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
r |= (c << 24);
r >>= 8;
c = (ctx->getC) (ctx);
if (c == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
r |= (c << 24);
r >>= 8;
c = (ctx->getC) (ctx);
if (c == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
r |= (c << 24);
r >>= 8;
c = (ctx->getC) (ctx);
if (c == EOF) {
2013-04-03 05:23:11 -07:00
return 0;
}
r |= (c << 24);
if (result) {
*result = (signed int)r;
}
return 1;
}
2007-10-28 14:15:15 -07:00
int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
IO_DBG(printf("Putting buf...\n"));
return (ctx->putBuf)(ctx, buf, size);
IO_DBG(printf("put.\n"));
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
return (ctx->getBuf)(ctx, buf, size);
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
int gdSeek(gdIOCtx *ctx, const int pos)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
IO_DBG(printf("Seeking...\n"));
return ((ctx->seek)(ctx, pos));
IO_DBG(printf("Done.\n"));
2006-04-05 08:35:53 -07:00
}
2007-10-28 14:15:15 -07:00
long gdTell(gdIOCtx *ctx)
2006-04-05 08:35:53 -07:00
{
2007-10-28 14:15:15 -07:00
IO_DBG(printf("Telling...\n"));
return ((ctx->tell)(ctx));
IO_DBG(printf("told.\n"));
2006-04-05 08:35:53 -07:00
}