summaryrefslogtreecommitdiff
path: root/byterun/io.h
blob: d679886cbc3a1a3bc8883aa2ecb1838a37cfc1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Buffered input/output */

#ifndef _io_
#define _io_


#include "misc.h"
#include "mlvalues.h"

#ifndef IO_BUFFER_SIZE
#define IO_BUFFER_SIZE 4096
#endif

struct channel {
  int fd;                       /* Unix file descriptor */
  long offset;                  /* Absolute position of fd in the file */
  char * curr;                  /* Current position in the buffer */
  char * max;                   /* Logical end of the buffer */
  char * end;                   /* Physical end of the buffer */
  char buff[IO_BUFFER_SIZE];    /* The buffer itself */
};

/* For an output channel:
     [offset] is the absolute position of the beginning of the buffer [buff].
   For an input channel:
     [offset] is the absolute position of the logical end of the buffer [max].
*/

#define putch(channel, ch)                                                  \
  { if ((channel)->curr >= (channel)->end) flush(channel);                  \
    *((channel)->curr)++ = (ch);                                            \
    if ((channel)->curr > (channel)->max) (channel)->max = (channel)->curr; }

#define getch(channel)                                                      \
  ((channel)->curr >= (channel)->max                                        \
   ? refill(channel)                                                        \
   : (unsigned char) *((channel))->curr++)

struct channel * open_descr P((int));
value flush P((struct channel *));
void putword P((struct channel *, uint32));
void putblock P((struct channel *, char *, unsigned));
unsigned char refill P((struct channel *));
value pos_out P((struct channel *));
value seek_out P((struct channel *, value));
uint32 getword P((struct channel *));
unsigned getblock P((struct channel *, char *, unsigned));
int really_getblock P((struct channel *, char *, unsigned long));
value close_in P((struct channel *));


#endif /* _io_ */