github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/include/bio.h (about) 1 /* 2 http://code.google.com/p/inferno-os/source/browse/include/bio.h 3 4 Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 5 Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved. 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 */ 25 26 #ifndef _BIO_H_ 27 #define _BIO_H_ 1 28 #if defined(__cplusplus) 29 extern "C" { 30 #endif 31 32 #ifdef AUTOLIB 33 AUTOLIB(bio) 34 #endif 35 36 #include <fcntl.h> /* for O_RDONLY, O_WRONLY */ 37 38 typedef struct Biobuf Biobuf; 39 40 enum 41 { 42 Bsize = 8*1024, 43 Bungetsize = 4, /* space for ungetc */ 44 Bmagic = 0x314159, 45 Beof = -1, 46 Bbad = -2, 47 48 Binactive = 0, /* states */ 49 Bractive, 50 Bwactive, 51 Bracteof, 52 53 Bend 54 }; 55 56 struct Biobuf 57 { 58 int icount; /* neg num of bytes at eob */ 59 int ocount; /* num of bytes at bob */ 60 int rdline; /* num of bytes after rdline */ 61 int runesize; /* num of bytes of last getrune */ 62 int state; /* r/w/inactive */ 63 int fid; /* open file */ 64 int flag; /* magic if malloc'ed */ 65 vlong offset; /* offset of buffer in file */ 66 int bsize; /* size of buffer */ 67 unsigned char* bbuf; /* pointer to beginning of buffer */ 68 unsigned char* ebuf; /* pointer to end of buffer */ 69 unsigned char* gbuf; /* pointer to good data in buf */ 70 unsigned char b[Bungetsize+Bsize]; 71 }; 72 73 /* 74 * These macros get 1-, 2-, and 4-byte integer values by reading the 75 * next few bytes in little-endian order. 76 */ 77 #define BGETC(bp)\ 78 ((bp)->icount?(bp)->ebuf[(bp)->icount++]:Bgetc((bp))) 79 #define BGETLE2(bp)\ 80 ((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp))) 81 #define BGETLE4(bp)\ 82 ((bp)->icount<=-4?((bp)->icount+=4,((bp)->ebuf[(bp)->icount-4])|((bp)->ebuf[(bp)->icount-3]<<8)|((bp)->ebuf[(bp)->icount-2]<<16)|((bp)->ebuf[(bp)->icount-1]<<24)):Bgetle4((bp))) 83 84 /* 85 * These macros put 1-, 2-, and 4-byte integer values by writing the 86 * next few bytes in little-endian order. 87 */ 88 #define BPUTC(bp,c)\ 89 ((bp)->ocount?(bp)->ebuf[(bp)->ocount++]=(unsigned char)(c),0:Bputc((bp),(c))) 90 #define BPUTLE2(bp,c)\ 91 ((bp)->ocount<=-2?(bp)->ocount+=2,(bp)->ebuf[(bp)->ocount-2]=(unsigned char)(c),(bp)->ebuf[(bp)->ocount-1]=(unsigned char)(c>>8),0:Bputle2((bp),(c))) 92 #define BPUTLE4(bp,c)\ 93 ((bp)->ocount<=-4?(bp)->ocount+=4,(bp)->ebuf[(bp)->ocount-4]=(unsigned char)(c),(bp)->ebuf[(bp)->ocount-3]=(unsigned char)(c>>8),(bp)->ebuf[(bp)->ocount-2]=(unsigned char)(c>>16),(bp)->ebuf[(bp)->ocount-1]=(unsigned char)(c>>24),0:Bputle4((bp),(c))) 94 95 #define BOFFSET(bp)\ 96 (((bp)->state==Bractive)?\ 97 (bp)->offset + (bp)->icount:\ 98 (((bp)->state==Bwactive)?\ 99 (bp)->offset + ((bp)->bsize + (bp)->ocount):\ 100 -1)) 101 #define BLINELEN(bp)\ 102 (bp)->rdline 103 #define BFILDES(bp)\ 104 (bp)->fid 105 106 int Bbuffered(Biobuf*); 107 Biobuf* Bfdopen(int, int); 108 int Bfildes(Biobuf*); 109 int Bflush(Biobuf*); 110 int Bgetc(Biobuf*); 111 int Bgetle2(Biobuf*); 112 int Bgetle4(Biobuf*); 113 int Bgetd(Biobuf*, double*); 114 long Bgetrune(Biobuf*); 115 int Binit(Biobuf*, int, int); 116 int Binits(Biobuf*, int, int, unsigned char*, int); 117 int Blinelen(Biobuf*); 118 vlong Boffset(Biobuf*); 119 Biobuf* Bopen(char*, int); 120 int Bprint(Biobuf*, char*, ...); 121 int Bputc(Biobuf*, int); 122 int Bputle2(Biobuf*, int); 123 int Bputle4(Biobuf*, int); 124 int Bputrune(Biobuf*, long); 125 void* Brdline(Biobuf*, int); 126 char* Brdstr(Biobuf*, int, int); 127 long Bread(Biobuf*, void*, long); 128 vlong Bseek(Biobuf*, vlong, int); 129 int Bterm(Biobuf*); 130 int Bungetc(Biobuf*); 131 int Bungetrune(Biobuf*); 132 long Bwrite(Biobuf*, void*, long); 133 int Bvprint(Biobuf*, char*, va_list); 134 135 #if defined(__cplusplus) 136 } 137 #endif 138 #endif