home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gifbla20.zip / SOURCE / UFFILE.H < prev   
C/C++ Source or Header  |  1992-12-15  |  4KB  |  98 lines

  1.  
  2. /* uffile.h - Include file for portable fast file routines, UNIX version. */
  3.  
  4. #ifndef P_DEFINED
  5. #define P_DEFINED
  6. #ifdef NOPROTOS
  7. #define P(a,b) b
  8. #else
  9. #define P(a,b) a
  10. #endif
  11. #endif
  12.  
  13. #define FFILEBUFSIZE 16384
  14. #define BITS_IN_CHAR 8
  15.  
  16. enum {FF_READ,FF_WRITE};
  17. typedef struct {
  18.     FILE *f;
  19.     int mode;
  20.     size_t pos,count;
  21.     long bits,tmpbits;
  22.     char nbits;
  23.     char onebitpos;
  24.     unsigned char buf[FFILEBUFSIZE];
  25. } FFILE;
  26.  
  27. /* functions/macros used internally only: */
  28. extern int ff___inbuf P((FFILE *ff),());
  29. extern int ff___outbuf P((int c, FFILE *ff),());
  30. extern int ff___in1bit P((FFILE *ff),());
  31. extern int ff___out1bit P((FFILE *ff, int bit),());
  32. #define ff___advance_put1bitpos(ff) ((ff)->onebitpos==BITS_IN_CHAR-1 \
  33.     ? ((ff)->onebitpos=0, (ff)->count++) : (ff)->onebitpos++)
  34.  
  35. /* functions/macros for external use: */
  36.  
  37. /* The following work similarly to the standard i/o library. */
  38. extern FFILE *ff_open P((char *fpath, int mode),());
  39. /* Opens a new file (mode is FF_READ or FF_WRITE). Returns NULL on failure. */
  40. #define ff_getc(ff) ((ff)->pos==(ff)->count \
  41.     ? ff___inbuf(ff) : (ff)->buf[(ff)->pos++])
  42. /* Reads a single character from ff. Returns EOF on end of file. */
  43. extern int ff_ungetc P((int c, FFILE *ff),());
  44. /* Undoes a single character read from ff. The next read on ff will return c.
  45.     At least one character may always be undone. May not be called with EOF
  46.     as argument. Returns c, or EOF on failure. */
  47. extern size_t ff_read P((char *buf, size_t n, FFILE *ff),());
  48. /* Reads n characters from ff and places them into buf. Returns the number
  49.     of characters successfully read. This may be less than n if end of file
  50.     was reached during the read. */
  51. #define ff_putc(c,ff) ((ff)->count==FFILEBUFSIZE \
  52.     ? ff___outbuf(c,ff) : ((ff)->buf[(ff)->count++]=(c)))
  53. /* Writes a single character to ff. Returns EOF on failure. */
  54. extern int ff_unputc P((FFILE *ff),());
  55. /* Undoes a single character written to ff. If any characters have been
  56.     written to ff, and ff_seek or ff_flush has not been called in the
  57.     interim, then at least one character may be undone. Returns the last
  58.     character written to ff, or EOF on failure. */
  59. extern size_t ff_write P((char *buf, size_t n, FFILE *ff),());
  60. /* Writes n characters from buf to ff. Returns the number of characters
  61.     successfully written. This may be less than n if a disk write was
  62.     unsuccessful (usually caused by a full disk). */
  63. extern int ff_seek P((FFILE *ff, long offset, int whence),());
  64. /* Seeks to a position in ff. Returns 0 on success, nonzero on failure. */
  65. extern long ff_tell P((FFILE *ff),());
  66. /* Returns the current position in ff, -1L on  failure. */
  67. extern int ff_flush P((FFILE *ff),());
  68. /* If ff is open for reading, discards any buffered input. Otherwise,
  69.     writes all buffered output. Returns 0 on success, EOF on failure. */
  70. extern int ff_close P((FFILE *ff),());
  71. /* Closes ff, flushing buffers and freeing the storage used by ff. Returns 0
  72.     on success, EOF on failure. */
  73.  
  74. /* The following is an optimized set of 1bitstream functions for the case
  75.     where we only read or write one bit at a time. To begin, ff_start1bit
  76.     must be called. Then, a series of ff_get1bit or ff_put1bit calls may
  77.     be made, accordingly as the fast file structure is open for reading
  78.     or writing. Finally, ff_end1bit must be called to end 1bitstream mode.
  79.     None of the other routines above should be called while in 1bitstream
  80.     mode (between ff_start1bit and ff_end1bit calls). */
  81. extern void ff_start1bit P((FFILE *ff),());
  82. /* Initializes 1bitstream state for ff. */
  83. #define ff_get1bit(ff) ((ff)->pos==(ff)->count ? ff___in1bit(ff) \
  84.     : ((ff)->onebitpos==BITS_IN_CHAR-1 \
  85.         ? ((ff)->onebitpos=0, \
  86.             (((ff)->buf[(ff)->pos++]&(1<<(BITS_IN_CHAR-1))) != 0)) \
  87.         : (((ff)->buf[(ff)->pos]&(1<<(ff->onebitpos++))) != 0)))
  88. /* Reads one bit from ff. Returns -1 on failure. */
  89. #define ff_put1bit(ff,b) ((ff)->count==FFILEBUFSIZE ? ff___out1bit(ff,b) \
  90.     : (((ff)->onebitpos==0 && ((ff)->buf[(ff)->count]=0)), \
  91.         (((b)&1)==0 ? (ff___advance_put1bitpos(ff), 0) \
  92.             : ((ff)->buf[(ff)->count]|=(1<<(ff)->onebitpos), \
  93.                 ff___advance_put1bitpos(ff), 1))))
  94. /* Writes the low bit of b to ff. Returns the low bit of b, or -1
  95.     on failure. */
  96. extern int ff_end1bit P((FFILE *ff),());
  97. /* Ends 1bitstream state for ff. Returns 0 on success, or -1 on failure. */
  98.