github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/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 typedef struct Biobuf Biobuf; 37 38 enum 39 { 40 Bsize = 8*1024, 41 Bungetsize = 4, /* space for ungetc */ 42 Bmagic = 0x314159, 43 Beof = -1, 44 Bbad = -2, 45 46 Binactive = 0, /* states */ 47 Bractive, 48 Bwactive, 49 Bracteof, 50 51 Bend 52 }; 53 54 struct Biobuf 55 { 56 int icount; /* neg num of bytes at eob */ 57 int ocount; /* num of bytes at bob */ 58 int rdline; /* num of bytes after rdline */ 59 int runesize; /* num of bytes of last getrune */ 60 int state; /* r/w/inactive */ 61 int fid; /* open file */ 62 int flag; /* magic if malloc'ed */ 63 vlong offset; /* offset of buffer in file */ 64 int bsize; /* size of buffer */ 65 unsigned char* bbuf; /* pointer to beginning of buffer */ 66 unsigned char* ebuf; /* pointer to end of buffer */ 67 unsigned char* gbuf; /* pointer to good data in buf */ 68 unsigned char b[Bungetsize+Bsize]; 69 }; 70 71 /* 72 * These macros get 1-, 2-, and 4-byte integer values by reading the 73 * next few bytes in little-endian order. 74 */ 75 #define BGETC(bp)\ 76 ((bp)->icount?(int)((bp)->ebuf[(bp)->icount++]):Bgetc((bp))) 77 #define BGETLE2(bp)\ 78 ((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp))) 79 #define BGETLE4(bp)\ 80 (int)((bp)->icount<=-4?((bp)->icount+=4,((bp)->ebuf[(bp)->icount-4])|((bp)->ebuf[(bp)->icount-3]<<8)|((bp)->ebuf[(bp)->icount-2]<<16)|((uint32)(bp)->ebuf[(bp)->icount-1]<<24)):Bgetle4((bp))) 81 82 /* 83 * These macros put 1-, 2-, and 4-byte integer values by writing the 84 * next few bytes in little-endian order. 85 */ 86 #define BPUTC(bp,c)\ 87 ((bp)->ocount?(bp)->ebuf[(bp)->ocount++]=(unsigned char)(c),0:Bputc((bp),(c))) 88 #define BPUTLE2(bp,c)\ 89 ((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))) 90 #define BPUTLE4(bp,c)\ 91 ((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))) 92 93 #define BOFFSET(bp)\ 94 (((bp)->state==Bractive)?\ 95 (bp)->offset + (bp)->icount:\ 96 (((bp)->state==Bwactive)?\ 97 (bp)->offset + ((bp)->bsize + (bp)->ocount):\ 98 -1)) 99 #define BLINELEN(bp)\ 100 (bp)->rdline 101 #define BFILDES(bp)\ 102 (bp)->fid 103 104 int Bbuffered(Biobuf*); 105 Biobuf* Bfdopen(int, int); 106 int Bfildes(Biobuf*); 107 int Bflush(Biobuf*); 108 int Bgetc(Biobuf*); 109 int Bgetle2(Biobuf*); 110 int Bgetle4(Biobuf*); 111 int Bgetd(Biobuf*, double*); 112 long Bgetrune(Biobuf*); 113 int Binit(Biobuf*, int, int); 114 int Binits(Biobuf*, int, int, unsigned char*, int); 115 int Blinelen(Biobuf*); 116 vlong Boffset(Biobuf*); 117 Biobuf* Bopen(char*, int); 118 int Bprint(Biobuf*, char*, ...); 119 int Bputc(Biobuf*, int); 120 int Bputle2(Biobuf*, int); 121 int Bputle4(Biobuf*, int); 122 int Bputrune(Biobuf*, long); 123 void* Brdline(Biobuf*, int); 124 char* Brdstr(Biobuf*, int, int); 125 long Bread(Biobuf*, void*, long); 126 vlong Bseek(Biobuf*, vlong, int); 127 int Bterm(Biobuf*); 128 int Bungetc(Biobuf*); 129 int Bungetrune(Biobuf*); 130 long Bwrite(Biobuf*, void*, long); 131 int Bvprint(Biobuf*, char*, va_list); 132 /*c2go 133 int BGETC(Biobuf*); 134 int BGETLE2(Biobuf*); 135 int BGETLE4(Biobuf*); 136 int BPUTC(Biobuf*, int); 137 int BPUTLE2(Biobuf*, int); 138 int BPUTLE4(Biobuf*, int); 139 */ 140 141 #if defined(__cplusplus) 142 } 143 #endif 144 #endif