blob: 07d7c5fe9cff1d64db728f1a70e5f4bed17e44f1 [file] [log] [blame]
rscf7012582003-11-25 01:40:27 +00001#ifndef _BIO_H_
2#define _BIO_H_ 1
3#if defined(__cplusplus)
4extern "C" {
5#endif
rscb2cfc4e2003-09-30 17:47:41 +00006
rsc78e51a82005-01-14 03:45:44 +00007#ifdef AUTOLIB
rsc1a0954a2005-01-04 21:18:08 +00008AUTOLIB(bio)
rsc78e51a82005-01-14 03:45:44 +00009#endif
rsc1a0954a2005-01-04 21:18:08 +000010
rscb2cfc4e2003-09-30 17:47:41 +000011#include <fcntl.h> /* for O_RDONLY, O_WRONLY */
12
13typedef struct Biobuf Biobuf;
14
15enum
16{
17 Bsize = 8*1024,
18 Bungetsize = 4, /* space for ungetc */
19 Bmagic = 0x314159,
20 Beof = -1,
21 Bbad = -2,
22
23 Binactive = 0, /* states */
24 Bractive,
25 Bwactive,
26 Bracteof,
27
28 Bend
29};
30
31struct Biobuf
32{
33 int icount; /* neg num of bytes at eob */
34 int ocount; /* num of bytes at bob */
35 int rdline; /* num of bytes after rdline */
rsc669250d2003-12-03 22:50:48 +000036 int runesize; /* num of bytes of last getrune */
rscb2cfc4e2003-09-30 17:47:41 +000037 int state; /* r/w/inactive */
38 int fid; /* open file */
39 int flag; /* magic if malloc'ed */
Russ Coxf533d4e2008-06-03 08:32:36 -040040 long long offset; /* offset of buffer in file */
rscb2cfc4e2003-09-30 17:47:41 +000041 int bsize; /* size of buffer */
42 unsigned char* bbuf; /* pointer to beginning of buffer */
43 unsigned char* ebuf; /* pointer to end of buffer */
44 unsigned char* gbuf; /* pointer to good data in buf */
45 unsigned char b[Bungetsize+Bsize];
46};
47
48#define BGETC(bp)\
49 ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))
50#define BPUTC(bp,c)\
51 ((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))
52#define BOFFSET(bp)\
53 (((bp)->state==Bractive)?\
54 (bp)->offset + (bp)->icount:\
55 (((bp)->state==Bwactive)?\
56 (bp)->offset + ((bp)->bsize + (bp)->ocount):\
57 -1))
58#define BLINELEN(bp)\
59 (bp)->rdline
60#define BFILDES(bp)\
61 (bp)->fid
62
63int Bbuffered(Biobuf*);
rsc986b36b2003-11-23 18:14:35 +000064Biobuf* Bfdopen(int, int);
rscb2cfc4e2003-09-30 17:47:41 +000065int Bfildes(Biobuf*);
66int Bflush(Biobuf*);
67int Bgetc(Biobuf*);
68int Bgetd(Biobuf*, double*);
rsc623ae4f2004-12-28 23:14:59 +000069long Bgetrune(Biobuf*);
rscb2cfc4e2003-09-30 17:47:41 +000070int Binit(Biobuf*, int, int);
71int Binits(Biobuf*, int, int, unsigned char*, int);
72int Blinelen(Biobuf*);
Russ Coxf533d4e2008-06-03 08:32:36 -040073long long Boffset(Biobuf*);
rscb2cfc4e2003-09-30 17:47:41 +000074Biobuf* Bopen(char*, int);
75int Bprint(Biobuf*, char*, ...);
76int Bputc(Biobuf*, int);
rsc623ae4f2004-12-28 23:14:59 +000077int Bputrune(Biobuf*, long);
rscb2cfc4e2003-09-30 17:47:41 +000078void* Brdline(Biobuf*, int);
rsc623ae4f2004-12-28 23:14:59 +000079char* Brdstr(Biobuf*, int, int);
rscb2cfc4e2003-09-30 17:47:41 +000080long Bread(Biobuf*, void*, long);
Russ Coxf533d4e2008-06-03 08:32:36 -040081long long Bseek(Biobuf*, long long, int);
rscb2cfc4e2003-09-30 17:47:41 +000082int Bterm(Biobuf*);
83int Bungetc(Biobuf*);
rsc522b0682003-09-30 19:05:50 +000084int Bungetrune(Biobuf*);
rscb2cfc4e2003-09-30 17:47:41 +000085long Bwrite(Biobuf*, void*, long);
rsc623ae4f2004-12-28 23:14:59 +000086int Bvprint(Biobuf*, char*, va_list);
rscb2cfc4e2003-09-30 17:47:41 +000087
rscf7012582003-11-25 01:40:27 +000088#if defined(__cplusplus)
89}
90#endif
rscb2cfc4e2003-09-30 17:47:41 +000091#endif