Back to index

php5  5.3.10
gd_io.c
Go to the documentation of this file.
00001 
00002 
00003 /*
00004    * io.c
00005    *
00006    * Implements the simple I/O 'helper' routines.
00007    *
00008    * Not really essential, but these routines were used extensively in GD,
00009    * so they were moved here. They also make IOCtx calls look better...
00010    *
00011    * Written (or, at least, moved) 1999, Philip Warner.
00012    *
00013  */
00014 
00015 #include <math.h>
00016 #include <string.h>
00017 #include <stdlib.h>
00018 #include "gd.h"
00019 
00020 /* Use this for commenting out debug-print statements. */
00021 /* Just use the first '#define' to allow all the prints... */
00022 /*#define IO_DBG(s) (s) */
00023 #define IO_DBG(s)
00024 
00025 
00026 #define GD_IO_EOF_CHK(r)    \
00027        if (r == EOF) {             \
00028               return 0;     \
00029        }                    \
00030 
00031 /*
00032  * Write out a word to the I/O context pointer
00033  */
00034 void Putword (int w, gdIOCtx * ctx)
00035 {
00036        unsigned char buf[2];
00037 
00038        buf[0] = w & 0xff;
00039        buf[1] = (w / 256) & 0xff;
00040        (ctx->putBuf) (ctx, (char *) buf, 2);
00041 }
00042 
00043 void Putchar (int c, gdIOCtx * ctx)
00044 {
00045        (ctx->putC) (ctx, c & 0xff);
00046 }
00047 
00048 void gdPutC (const unsigned char c, gdIOCtx * ctx)
00049 {
00050        (ctx->putC) (ctx, c);
00051 }
00052 
00053 void gdPutWord (int w, gdIOCtx * ctx)
00054 {
00055        IO_DBG (php_gd_error("Putting word..."));
00056        (ctx->putC) (ctx, (unsigned char) (w >> 8));
00057        (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
00058        IO_DBG (php_gd_error("put."));
00059 }
00060 
00061 void gdPutInt (int w, gdIOCtx * ctx)
00062 {
00063        IO_DBG (php_gd_error("Putting int..."));
00064        (ctx->putC) (ctx, (unsigned char) (w >> 24));
00065        (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
00066        (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
00067        (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
00068        IO_DBG (php_gd_error("put."));
00069 }
00070 
00071 int gdGetC (gdIOCtx * ctx)
00072 {
00073        return ((ctx->getC) (ctx));
00074 }
00075 
00076 int gdGetByte (int *result, gdIOCtx * ctx)
00077 {
00078        int r;
00079        r = (ctx->getC) (ctx);
00080        GD_IO_EOF_CHK(r);
00081        *result = r;
00082        return 1;
00083 }
00084 
00085 int gdGetWord (int *result, gdIOCtx * ctx)
00086 {
00087        int r;
00088        r = (ctx->getC) (ctx);
00089        GD_IO_EOF_CHK(r);
00090        *result = r << 8;
00091        r = (ctx->getC) (ctx);
00092        GD_IO_EOF_CHK(r);
00093        *result += r;
00094        return 1;
00095 }
00096 
00097 
00098 int gdGetInt (int *result, gdIOCtx * ctx)
00099 {
00100        int r;
00101        r = (ctx->getC) (ctx);
00102        GD_IO_EOF_CHK(r);
00103        *result = r << 24;
00104 
00105        r = (ctx->getC) (ctx);
00106        GD_IO_EOF_CHK(r);
00107        *result += r << 16;
00108 
00109        r = (ctx->getC) (ctx);
00110        if (r == EOF) {
00111               return 0;
00112        }
00113        *result += r << 8;
00114 
00115        r = (ctx->getC) (ctx);
00116        GD_IO_EOF_CHK(r);
00117        *result += r;
00118 
00119        return 1;
00120 }
00121 
00122 int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
00123 {
00124        IO_DBG (php_gd_error("Putting buf..."));
00125        return (ctx->putBuf) (ctx, buf, size);
00126        IO_DBG (php_gd_error("put."));
00127 }
00128 
00129 int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
00130 {
00131        return (ctx->getBuf) (ctx, buf, size);
00132 }
00133 
00134 int gdSeek (gdIOCtx * ctx, const int pos)
00135 {
00136        IO_DBG (php_gd_error("Seeking..."));
00137        return ((ctx->seek) (ctx, pos));
00138        IO_DBG (php_gd_error("Done."));
00139 }
00140 
00141 long gdTell (gdIOCtx * ctx)
00142 {
00143        IO_DBG (php_gd_error("Telling..."));
00144        return ((ctx->tell) (ctx));
00145        IO_DBG (php_gd_error ("told."));
00146 }