Blob


1 #ifndef _BIOH_
2 #define _BIOH_ 1
4 #include <sys/types.h> /* for off_t */
5 #include <fcntl.h> /* for O_RDONLY, O_WRONLY */
7 typedef struct Biobuf Biobuf;
9 enum
10 {
11 Bsize = 8*1024,
12 Bungetsize = 4, /* space for ungetc */
13 Bmagic = 0x314159,
14 Beof = -1,
15 Bbad = -2,
17 Binactive = 0, /* states */
18 Bractive,
19 Bwactive,
20 Bracteof,
22 Bend
23 };
25 struct Biobuf
26 {
27 int icount; /* neg num of bytes at eob */
28 int ocount; /* num of bytes at bob */
29 int rdline; /* num of bytes after rdline */
30 int runesize; /* num of bytes of last getrune */
31 int state; /* r/w/inactive */
32 int fid; /* open file */
33 int flag; /* magic if malloc'ed */
34 off_t offset; /* offset of buffer in file */
35 int bsize; /* size of buffer */
36 unsigned char* bbuf; /* pointer to beginning of buffer */
37 unsigned char* ebuf; /* pointer to end of buffer */
38 unsigned char* gbuf; /* pointer to good data in buf */
39 unsigned char b[Bungetsize+Bsize];
40 };
42 #define BGETC(bp)\
43 ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))
44 #define BPUTC(bp,c)\
45 ((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))
46 #define BOFFSET(bp)\
47 (((bp)->state==Bractive)?\
48 (bp)->offset + (bp)->icount:\
49 (((bp)->state==Bwactive)?\
50 (bp)->offset + ((bp)->bsize + (bp)->ocount):\
51 -1))
52 #define BLINELEN(bp)\
53 (bp)->rdline
54 #define BFILDES(bp)\
55 (bp)->fid
57 int Bbuffered(Biobuf*);
58 Biobuf* Bfdopen(int, int);
59 int Bfildes(Biobuf*);
60 int Bflush(Biobuf*);
61 int Bgetc(Biobuf*);
62 int Bgetd(Biobuf*, double*);
63 int Binit(Biobuf*, int, int);
64 int Binits(Biobuf*, int, int, unsigned char*, int);
65 int Blinelen(Biobuf*);
66 off_t Boffset(Biobuf*);
67 Biobuf* Bopen(char*, int);
68 int Bprint(Biobuf*, char*, ...);
69 int Bputc(Biobuf*, int);
70 void* Brdline(Biobuf*, int);
71 long Bread(Biobuf*, void*, long);
72 off_t Bseek(Biobuf*, off_t, int);
73 int Bterm(Biobuf*);
74 int Bungetc(Biobuf*);
75 int Bungetrune(Biobuf*);
76 long Bwrite(Biobuf*, void*, long);
77 char* Brdstr(Biobuf*, int, int);
78 long Bgetrune(Biobuf*);
79 int Bputrune(Biobuf*, long);
81 #endif