blob: 2c2b9566faf523e40ff1cc901e170b16611abd80 [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
rsc1a0954a2005-01-04 21:18:08 +00007AUTOLIB(bio)
8
rscb2cfc4e2003-09-30 17:47:41 +00009#include <sys/types.h> /* for off_t */
10#include <fcntl.h> /* for O_RDONLY, O_WRONLY */
11
12typedef struct Biobuf Biobuf;
13
14enum
15{
16 Bsize = 8*1024,
17 Bungetsize = 4, /* space for ungetc */
18 Bmagic = 0x314159,
19 Beof = -1,
20 Bbad = -2,
21
22 Binactive = 0, /* states */
23 Bractive,
24 Bwactive,
25 Bracteof,
26
27 Bend
28};
29
30struct Biobuf
31{
32 int icount; /* neg num of bytes at eob */
33 int ocount; /* num of bytes at bob */
34 int rdline; /* num of bytes after rdline */
rsc669250d2003-12-03 22:50:48 +000035 int runesize; /* num of bytes of last getrune */
rscb2cfc4e2003-09-30 17:47:41 +000036 int state; /* r/w/inactive */
37 int fid; /* open file */
38 int flag; /* magic if malloc'ed */
39 off_t offset; /* offset of buffer in file */
40 int bsize; /* size of buffer */
41 unsigned char* bbuf; /* pointer to beginning of buffer */
42 unsigned char* ebuf; /* pointer to end of buffer */
43 unsigned char* gbuf; /* pointer to good data in buf */
44 unsigned char b[Bungetsize+Bsize];
45};
46
47#define BGETC(bp)\
48 ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))
49#define BPUTC(bp,c)\
50 ((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))
51#define BOFFSET(bp)\
52 (((bp)->state==Bractive)?\
53 (bp)->offset + (bp)->icount:\
54 (((bp)->state==Bwactive)?\
55 (bp)->offset + ((bp)->bsize + (bp)->ocount):\
56 -1))
57#define BLINELEN(bp)\
58 (bp)->rdline
59#define BFILDES(bp)\
60 (bp)->fid
61
62int Bbuffered(Biobuf*);
rsc986b36b2003-11-23 18:14:35 +000063Biobuf* Bfdopen(int, int);
rscb2cfc4e2003-09-30 17:47:41 +000064int Bfildes(Biobuf*);
65int Bflush(Biobuf*);
66int Bgetc(Biobuf*);
67int Bgetd(Biobuf*, double*);
rsc623ae4f2004-12-28 23:14:59 +000068long Bgetrune(Biobuf*);
rscb2cfc4e2003-09-30 17:47:41 +000069int Binit(Biobuf*, int, int);
70int Binits(Biobuf*, int, int, unsigned char*, int);
71int Blinelen(Biobuf*);
72off_t Boffset(Biobuf*);
73Biobuf* Bopen(char*, int);
74int Bprint(Biobuf*, char*, ...);
75int Bputc(Biobuf*, int);
rsc623ae4f2004-12-28 23:14:59 +000076int Bputrune(Biobuf*, long);
rscb2cfc4e2003-09-30 17:47:41 +000077void* Brdline(Biobuf*, int);
rsc623ae4f2004-12-28 23:14:59 +000078char* Brdstr(Biobuf*, int, int);
rscb2cfc4e2003-09-30 17:47:41 +000079long Bread(Biobuf*, void*, long);
80off_t Bseek(Biobuf*, off_t, int);
81int Bterm(Biobuf*);
82int Bungetc(Biobuf*);
rsc522b0682003-09-30 19:05:50 +000083int Bungetrune(Biobuf*);
rscb2cfc4e2003-09-30 17:47:41 +000084long Bwrite(Biobuf*, void*, long);
rsc623ae4f2004-12-28 23:14:59 +000085int Bvprint(Biobuf*, char*, va_list);
rscb2cfc4e2003-09-30 17:47:41 +000086
rscf7012582003-11-25 01:40:27 +000087#if defined(__cplusplus)
88}
89#endif
rscb2cfc4e2003-09-30 17:47:41 +000090#endif