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