home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / io.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  1.8 KB  |  51 lines  |  [TEXT/R*ch]

  1. /* Buffered input/output */
  2.  
  3. #ifndef _io_
  4. #define _io_
  5.  
  6.  
  7. #include "misc.h"
  8. #include "mlvalues.h"
  9.  
  10. #ifndef IO_BUFFER_SIZE
  11. #define IO_BUFFER_SIZE 4096
  12. #endif
  13.  
  14. struct channel {
  15.   int fd;                       /* Unix file descriptor */
  16.   long offset;                  /* Absolute position of fd in the file */
  17.   char * curr;                  /* Current position in the buffer */
  18.   char * max;                   /* Logical end of the buffer */
  19.   char * end;                   /* Physical end of the buffer */
  20.   char buff[IO_BUFFER_SIZE];    /* The buffer itself */
  21. };
  22.  
  23. /* For an output channel:
  24.      [offset] is the absolute position of the beginning of the buffer [buff].
  25.    For an input channel:
  26.      [offset] is the absolute position of the logical end of the buffer [max].
  27. */
  28.  
  29. #define putch(channel, ch)                                                    \
  30.   { if ((channel)->curr >= (channel)->end) flush(channel);                    \
  31.     *((channel)->curr)++ = (ch);                                              \
  32.     if ((channel)->curr > (channel)->max) (channel)->max = (channel)->curr; }
  33.  
  34. #define getch(channel)                                                        \
  35.   ((channel)->curr >= (channel)->max                                          \
  36.    ? refill(channel)                                                          \
  37.    : (unsigned char) *((channel))->curr++)
  38.  
  39. struct channel * open_descr P((int));
  40. value flush P((struct channel *));
  41. void putword P((struct channel *, uint32));
  42. void putblock P((struct channel *, char *, unsigned));
  43. unsigned char refill P((struct channel *));
  44. uint32 getword P((struct channel *));
  45. unsigned getblock P((struct channel *, char *, unsigned));
  46. int really_getblock P((struct channel *, char *, unsigned long));
  47. value close_in P((struct channel *));
  48. void close_stdouterr P((void));
  49.  
  50. #endif /* _io_ */
  51.