Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 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) |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 29 | extern "C" { |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #ifdef AUTOLIB |
| 33 | AUTOLIB(bio) |
| 34 | #endif |
| 35 | |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 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 */ |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 65 | vlong offset; /* offset of buffer in file */ |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 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 | #define BGETC(bp)\ |
| 74 | ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp))) |
| 75 | #define BPUTC(bp,c)\ |
| 76 | ((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c))) |
| 77 | #define BOFFSET(bp)\ |
| 78 | (((bp)->state==Bractive)?\ |
| 79 | (bp)->offset + (bp)->icount:\ |
| 80 | (((bp)->state==Bwactive)?\ |
| 81 | (bp)->offset + ((bp)->bsize + (bp)->ocount):\ |
| 82 | -1)) |
| 83 | #define BLINELEN(bp)\ |
| 84 | (bp)->rdline |
| 85 | #define BFILDES(bp)\ |
| 86 | (bp)->fid |
| 87 | |
| 88 | int Bbuffered(Biobuf*); |
| 89 | Biobuf* Bfdopen(int, int); |
| 90 | int Bfildes(Biobuf*); |
| 91 | int Bflush(Biobuf*); |
| 92 | int Bgetc(Biobuf*); |
| 93 | int Bgetd(Biobuf*, double*); |
| 94 | long Bgetrune(Biobuf*); |
| 95 | int Binit(Biobuf*, int, int); |
| 96 | int Binits(Biobuf*, int, int, unsigned char*, int); |
| 97 | int Blinelen(Biobuf*); |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 98 | vlong Boffset(Biobuf*); |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 99 | Biobuf* Bopen(char*, int); |
| 100 | int Bprint(Biobuf*, char*, ...); |
| 101 | int Bputc(Biobuf*, int); |
| 102 | int Bputrune(Biobuf*, long); |
| 103 | void* Brdline(Biobuf*, int); |
| 104 | char* Brdstr(Biobuf*, int, int); |
| 105 | long Bread(Biobuf*, void*, long); |
Russ Cox | a570eaa | 2008-10-20 17:33:51 -0700 | [diff] [blame] | 106 | vlong Bseek(Biobuf*, vlong, int); |
Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 107 | int Bterm(Biobuf*); |
| 108 | int Bungetc(Biobuf*); |
| 109 | int Bungetrune(Biobuf*); |
| 110 | long Bwrite(Biobuf*, void*, long); |
| 111 | int Bvprint(Biobuf*, char*, va_list); |
| 112 | |
| 113 | #if defined(__cplusplus) |
| 114 | } |
| 115 | #endif |
| 116 | #endif |