blob: c754d7a57d5feef1ed5b6cb31640076fc3a60a90 [file] [log] [blame]
Rob Pike69b74c32008-06-12 13:26:16 -07001/*
2http://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
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE.
24*/
25
26#ifndef _BIO_H_
27#define _BIO_H_ 1
28#if defined(__cplusplus)
Russ Coxa570eaa2008-10-20 17:33:51 -070029extern "C" {
Rob Pike69b74c32008-06-12 13:26:16 -070030#endif
31
32#ifdef AUTOLIB
33AUTOLIB(bio)
34#endif
35
Rob Pike69b74c32008-06-12 13:26:16 -070036#include <fcntl.h> /* for O_RDONLY, O_WRONLY */
37
38typedef struct Biobuf Biobuf;
39
40enum
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
56struct 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 Coxa570eaa2008-10-20 17:33:51 -070065 vlong offset; /* offset of buffer in file */
Rob Pike69b74c32008-06-12 13:26:16 -070066 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
88int Bbuffered(Biobuf*);
89Biobuf* Bfdopen(int, int);
90int Bfildes(Biobuf*);
91int Bflush(Biobuf*);
92int Bgetc(Biobuf*);
93int Bgetd(Biobuf*, double*);
94long Bgetrune(Biobuf*);
95int Binit(Biobuf*, int, int);
96int Binits(Biobuf*, int, int, unsigned char*, int);
97int Blinelen(Biobuf*);
Russ Coxa570eaa2008-10-20 17:33:51 -070098vlong Boffset(Biobuf*);
Rob Pike69b74c32008-06-12 13:26:16 -070099Biobuf* Bopen(char*, int);
100int Bprint(Biobuf*, char*, ...);
101int Bputc(Biobuf*, int);
102int Bputrune(Biobuf*, long);
103void* Brdline(Biobuf*, int);
104char* Brdstr(Biobuf*, int, int);
105long Bread(Biobuf*, void*, long);
Russ Coxa570eaa2008-10-20 17:33:51 -0700106vlong Bseek(Biobuf*, vlong, int);
Rob Pike69b74c32008-06-12 13:26:16 -0700107int Bterm(Biobuf*);
108int Bungetc(Biobuf*);
109int Bungetrune(Biobuf*);
110long Bwrite(Biobuf*, void*, long);
111int Bvprint(Biobuf*, char*, va_list);
112
113#if defined(__cplusplus)
114}
115#endif
116#endif