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