Back to index

plt-scheme  4.2.1
gd_io.c
Go to the documentation of this file.
00001 
00002 
00003 /*
00004    * io.c
00005    *
00006    * Implements the imple 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 #ifdef HAVE_CONFIG_H
00016 #include "config.h"
00017 #endif
00018 
00019 #include <math.h>
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include "gd.h"
00023 
00024 /* Use this for commenting out debug-print statements. */
00025 /* Just use the first '#define' to allow all the prints... */
00026 /*#define IO_DBG(s) (s) */
00027 #define IO_DBG(s)
00028 
00029 
00030 /*
00031  * Write out a word to the I/O context pointer
00032  */
00033 void
00034 Putword (int w, gdIOCtx * ctx)
00035 {
00036   unsigned char buf[2];
00037   buf[0] = w & 0xff;
00038   buf[1] = (w / 256) & 0xff;
00039   (ctx->putBuf) (ctx, (char *) buf, 2);
00040 }
00041 
00042 void
00043 Putchar (int c, gdIOCtx * ctx)
00044 {
00045   (ctx->putC) (ctx, c & 0xff);
00046 }
00047 
00048 void
00049 gdPutC (const unsigned char c, gdIOCtx * ctx)
00050 {
00051   (ctx->putC) (ctx, c);
00052 }
00053 
00054 void
00055 gdPutWord (int w, gdIOCtx * ctx)
00056 {
00057   IO_DBG (printf ("Putting word...\n"));
00058   (ctx->putC) (ctx, (unsigned char) (w >> 8));
00059   (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
00060   IO_DBG (printf ("put.\n"));
00061 }
00062 
00063 void
00064 gdPutInt (int w, gdIOCtx * ctx)
00065 {
00066   IO_DBG (printf ("Putting int...\n"));
00067   (ctx->putC) (ctx, (unsigned char) (w >> 24));
00068   (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
00069   (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
00070   (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
00071   IO_DBG (printf ("put.\n"));
00072 }
00073 
00074 int
00075 gdGetC (gdIOCtx * ctx)
00076 {
00077   return ((ctx->getC) (ctx));
00078 }
00079 
00080 
00081 
00082 int
00083 gdGetByte (int *result, gdIOCtx * ctx)
00084 {
00085   int r;
00086   r = (ctx->getC) (ctx);
00087   if (r == EOF)
00088     {
00089       return 0;
00090     }
00091   *result = r;
00092   return 1;
00093 }
00094 
00095 int
00096 gdGetWord (int *result, gdIOCtx * ctx)
00097 {
00098   int r;
00099   r = (ctx->getC) (ctx);
00100   if (r == EOF)
00101     {
00102       return 0;
00103     }
00104   *result = r << 8;
00105   r = (ctx->getC) (ctx);
00106   if (r == EOF)
00107     {
00108       return 0;
00109     }
00110   *result += r;
00111   return 1;
00112 }
00113 
00114 
00115 int
00116 gdGetInt (int *result, gdIOCtx * ctx)
00117 {
00118   int r;
00119   r = (ctx->getC) (ctx);
00120   if (r == EOF)
00121     {
00122       return 0;
00123     }
00124   *result = r << 24;
00125 
00126   r = (ctx->getC) (ctx);
00127   if (r == EOF)
00128     {
00129       return 0;
00130     }
00131   *result += r << 16;
00132 
00133   r = (ctx->getC) (ctx);
00134   if (r == EOF)
00135     {
00136       return 0;
00137     }
00138   *result += r << 8;
00139 
00140   r = (ctx->getC) (ctx);
00141   if (r == EOF)
00142     {
00143       return 0;
00144     }
00145   *result += r;
00146 
00147   return 1;
00148 }
00149 
00150 int
00151 gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
00152 {
00153   IO_DBG (printf ("Putting buf...\n"));
00154   return (ctx->putBuf) (ctx, buf, size);
00155   IO_DBG (printf ("put.\n"));
00156 }
00157 
00158 int
00159 gdGetBuf (void *buf, int size, gdIOCtx * ctx)
00160 {
00161   return (ctx->getBuf) (ctx, buf, size);
00162 }
00163 
00164 
00165 int
00166 gdSeek (gdIOCtx * ctx, const int pos)
00167 {
00168   IO_DBG (printf ("Seeking...\n"));
00169   return ((ctx->seek) (ctx, pos));
00170   IO_DBG (printf ("Done.\n"));
00171 }
00172 
00173 long
00174 gdTell (gdIOCtx * ctx)
00175 {
00176   IO_DBG (printf ("Telling...\n"));
00177   return ((ctx->tell) (ctx));
00178   IO_DBG (printf ("told.\n"));
00179 }