gd_io_file.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * io_file.c
  3. *
  4. * Implements the file interface.
  5. *
  6. * As will all I/O modules, most functions are for local use only (called
  7. * via function pointers in the I/O context).
  8. *
  9. * Most functions are just 'wrappers' for standard file functions.
  10. *
  11. * Written/Modified 1999, Philip Warner.
  12. *
  13. */
  14. /* For platforms with incomplete ANSI defines. Fortunately,
  15. SEEK_SET is defined to be zero by the standard. */
  16. #ifndef SEEK_SET
  17. #define SEEK_SET 0
  18. #endif /* SEEK_SET */
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include "gd.h"
  23. #include "gdhelpers.h"
  24. /* this is used for creating images in main memory */
  25. typedef struct fileIOCtx
  26. {
  27. gdIOCtx ctx;
  28. FILE *f;
  29. } fileIOCtx;
  30. gdIOCtx *newFileCtx (FILE * f);
  31. static int fileGetbuf (gdIOCtx *, void *, int);
  32. static int filePutbuf (gdIOCtx *, const void *, int);
  33. static void filePutchar (gdIOCtx *, int);
  34. static int fileGetchar (gdIOCtx * ctx);
  35. static int fileSeek (struct gdIOCtx *, const int);
  36. static long fileTell (struct gdIOCtx *);
  37. static void gdFreeFileCtx (gdIOCtx * ctx);
  38. /* return data as a dynamic pointer */
  39. gdIOCtx * gdNewFileCtx (FILE * f)
  40. {
  41. fileIOCtx *ctx;
  42. ctx = (fileIOCtx *) gdMalloc(sizeof (fileIOCtx));
  43. ctx->f = f;
  44. ctx->ctx.getC = fileGetchar;
  45. ctx->ctx.putC = filePutchar;
  46. ctx->ctx.getBuf = fileGetbuf;
  47. ctx->ctx.putBuf = filePutbuf;
  48. ctx->ctx.tell = fileTell;
  49. ctx->ctx.seek = fileSeek;
  50. ctx->ctx.gd_free = gdFreeFileCtx;
  51. return (gdIOCtx *) ctx;
  52. }
  53. static void gdFreeFileCtx (gdIOCtx * ctx)
  54. {
  55. gdFree(ctx);
  56. }
  57. static int filePutbuf (gdIOCtx * ctx, const void *buf, int size)
  58. {
  59. fileIOCtx *fctx;
  60. fctx = (fileIOCtx *) ctx;
  61. return fwrite(buf, 1, size, fctx->f);
  62. }
  63. static int fileGetbuf (gdIOCtx * ctx, void *buf, int size)
  64. {
  65. fileIOCtx *fctx;
  66. fctx = (fileIOCtx *) ctx;
  67. return fread(buf, 1, size, fctx->f);
  68. }
  69. static void filePutchar (gdIOCtx * ctx, int a)
  70. {
  71. unsigned char b;
  72. fileIOCtx *fctx;
  73. fctx = (fileIOCtx *) ctx;
  74. b = a;
  75. putc (b, fctx->f);
  76. }
  77. static int fileGetchar (gdIOCtx * ctx)
  78. {
  79. fileIOCtx *fctx;
  80. fctx = (fileIOCtx *) ctx;
  81. return getc (fctx->f);
  82. }
  83. static int fileSeek (struct gdIOCtx *ctx, const int pos)
  84. {
  85. fileIOCtx *fctx;
  86. fctx = (fileIOCtx *) ctx;
  87. return (fseek (fctx->f, pos, SEEK_SET) == 0);
  88. }
  89. static long fileTell (struct gdIOCtx *ctx)
  90. {
  91. fileIOCtx *fctx;
  92. fctx = (fileIOCtx *) ctx;
  93. return ftell (fctx->f);
  94. }