gd_io.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * io.c
  3. *
  4. * Implements the simple I/O 'helper' routines.
  5. *
  6. * Not really essential, but these routines were used extensively in GD,
  7. * so they were moved here. They also make IOCtx calls look better...
  8. *
  9. * Written (or, at least, moved) 1999, Philip Warner.
  10. *
  11. */
  12. #include <math.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "gd.h"
  16. /* Use this for commenting out debug-print statements. */
  17. /* Just use the first '#define' to allow all the prints... */
  18. /*#define IO_DBG(s) (s) */
  19. #define IO_DBG(s)
  20. #define GD_IO_EOF_CHK(r) \
  21. if (r == EOF) { \
  22. return 0; \
  23. } \
  24. /*
  25. * Write out a word to the I/O context pointer
  26. */
  27. void Putword (int w, gdIOCtx * ctx)
  28. {
  29. unsigned char buf[2];
  30. buf[0] = w & 0xff;
  31. buf[1] = (w / 256) & 0xff;
  32. (ctx->putBuf) (ctx, (char *) buf, 2);
  33. }
  34. void Putchar (int c, gdIOCtx * ctx)
  35. {
  36. (ctx->putC) (ctx, c & 0xff);
  37. }
  38. void gdPutC (const unsigned char c, gdIOCtx * ctx)
  39. {
  40. (ctx->putC) (ctx, c);
  41. }
  42. void gdPutWord (int w, gdIOCtx * ctx)
  43. {
  44. IO_DBG (gd_error("Putting word..."));
  45. (ctx->putC) (ctx, (unsigned char) (w >> 8));
  46. (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
  47. IO_DBG (gd_error("put."));
  48. }
  49. void gdPutInt (int w, gdIOCtx * ctx)
  50. {
  51. IO_DBG (gd_error("Putting int..."));
  52. (ctx->putC) (ctx, (unsigned char) (w >> 24));
  53. (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
  54. (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
  55. (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
  56. IO_DBG (gd_error("put."));
  57. }
  58. int gdGetC (gdIOCtx * ctx)
  59. {
  60. return ((ctx->getC) (ctx));
  61. }
  62. int gdGetByte (int *result, gdIOCtx * ctx)
  63. {
  64. int r;
  65. r = (ctx->getC) (ctx);
  66. GD_IO_EOF_CHK(r);
  67. *result = r;
  68. return 1;
  69. }
  70. int gdGetWord (int *result, gdIOCtx * ctx)
  71. {
  72. int r;
  73. r = (ctx->getC) (ctx);
  74. GD_IO_EOF_CHK(r);
  75. *result = r << 8;
  76. r = (ctx->getC) (ctx);
  77. GD_IO_EOF_CHK(r);
  78. *result += r;
  79. return 1;
  80. }
  81. int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
  82. {
  83. int high = 0, low = 0;
  84. low = (ctx->getC) (ctx);
  85. if (low == EOF) {
  86. return 0;
  87. }
  88. high = (ctx->getC) (ctx);
  89. if (high == EOF) {
  90. return 0;
  91. }
  92. if (result) {
  93. *result = (high << 8) | low;
  94. }
  95. return 1;
  96. }
  97. int gdGetInt (int *result, gdIOCtx * ctx)
  98. {
  99. unsigned int r;
  100. r = (ctx->getC) (ctx);
  101. GD_IO_EOF_CHK(r);
  102. *result = r << 24;
  103. r = (ctx->getC) (ctx);
  104. GD_IO_EOF_CHK(r);
  105. *result += r << 16;
  106. r = (ctx->getC) (ctx);
  107. if (r == EOF) {
  108. return 0;
  109. }
  110. *result += r << 8;
  111. r = (ctx->getC) (ctx);
  112. GD_IO_EOF_CHK(r);
  113. *result += r;
  114. return 1;
  115. }
  116. int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
  117. {
  118. unsigned int c;
  119. unsigned int r = 0;
  120. c = (ctx->getC) (ctx);
  121. if (c == EOF) {
  122. return 0;
  123. }
  124. r |= (c << 24);
  125. r >>= 8;
  126. c = (ctx->getC) (ctx);
  127. if (c == EOF) {
  128. return 0;
  129. }
  130. r |= (c << 24);
  131. r >>= 8;
  132. c = (ctx->getC) (ctx);
  133. if (c == EOF) {
  134. return 0;
  135. }
  136. r |= (c << 24);
  137. r >>= 8;
  138. c = (ctx->getC) (ctx);
  139. if (c == EOF) {
  140. return 0;
  141. }
  142. r |= (c << 24);
  143. if (result) {
  144. *result = (signed int)r;
  145. }
  146. return 1;
  147. }
  148. int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
  149. {
  150. IO_DBG (gd_error("Putting buf..."));
  151. return (ctx->putBuf) (ctx, buf, size);
  152. IO_DBG (gd_error("put."));
  153. }
  154. int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
  155. {
  156. return (ctx->getBuf) (ctx, buf, size);
  157. }
  158. int gdSeek (gdIOCtx * ctx, const int pos)
  159. {
  160. IO_DBG (gd_error("Seeking..."));
  161. return ((ctx->seek) (ctx, pos));
  162. IO_DBG (gd_error("Done."));
  163. }
  164. long gdTell (gdIOCtx * ctx)
  165. {
  166. IO_DBG (gd_error("Telling..."));
  167. return ((ctx->tell) (ctx));
  168. IO_DBG (gd_error ("told."));
  169. }