2 * Sherlock Library -- Fast File Buffering
4 * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
12 byte *bptr, *bstop; /* Access pointers */
13 byte *buffer, *bufend; /* Start and end of the buffer */
14 byte *name; /* File name for error messages */
15 uns buflen; /* Size of standard portion of the buffer */
16 uns pos; /* Position of bptr in the file */
17 uns fdpos; /* Current position in the file */
18 int fd; /* File descriptor */
21 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
22 struct fastbuf *bfdopen(int fd, uns buffer);
23 void bclose(struct fastbuf *f);
24 void bflush(struct fastbuf *f);
26 void bseek(struct fastbuf *f, uns pos, int whence);
27 void bsetpos(struct fastbuf *f, uns pos);
29 extern inline uns btell(struct fastbuf *f)
31 return f->pos + (f->bptr - f->buffer);
34 int bgetc_slow(struct fastbuf *f);
35 extern inline int bgetc(struct fastbuf *f)
37 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
40 extern inline void bungetc(struct fastbuf *f, byte c)
45 void bputc_slow(struct fastbuf *f, byte c);
46 extern inline void bputc(struct fastbuf *f, byte c)
48 if (f->bptr < f->bufend)
54 word bgetw_slow(struct fastbuf *f);
55 extern inline word bgetw(struct fastbuf *f)
57 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
59 if (f->bptr + 2 <= f->bstop)
61 w = * ((word *) f->bptr);
70 return (w << 8) | bgetc(f);
72 return w | (bgetc(f) << 8);
77 ulg bgetl_slow(struct fastbuf *f);
78 extern inline ulg bgetl(struct fastbuf *f)
80 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
82 if (f->bptr + 4 <= f->bstop)
84 l = * ((ulg *) f->bptr);
93 l = (l << 8) | bgetc(f);
94 l = (l << 8) | bgetc(f);
95 return (l << 8) | bgetc(f);
97 l = (bgetc(f) << 8) | l;
98 l = (bgetc(f) << 16) | l;
99 return (bgetc(f) << 24) | l;
104 void bputw_slow(struct fastbuf *f, word w);
105 extern inline void bputw(struct fastbuf *f, word w)
107 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
108 if (f->bptr + 2 <= f->bufend)
110 * ((word *) f->bptr) = w;
116 #ifdef CPU_BIG_ENDIAN
126 void bputl_slow(struct fastbuf *f, ulg l);
127 extern inline void bputl(struct fastbuf *f, ulg l)
129 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
130 if (f->bptr + 4 <= f->bufend)
132 * ((ulg *) f->bptr) = l;
138 #ifdef CPU_BIG_ENDIAN
152 void bread_slow(struct fastbuf *f, void *b, uns l);
153 extern inline void bread(struct fastbuf *f, void *b, uns l)
155 if (f->bptr + l <= f->bstop)
157 memcpy(b, f->bptr, l);
164 void bwrite_slow(struct fastbuf *f, void *b, uns l);
165 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
167 if (f->bptr + l <= f->bufend)
169 memcpy(f->bptr, b, l);
173 bwrite_slow(f, b, l);
176 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
178 extern inline byte * /* Non-standard */
179 bgets(struct fastbuf *f, byte *b, uns l)
189 if (k == '\n' || k == EOF)
197 die("%s: Line too long", f->name);
201 bputs(struct fastbuf *f, byte *b)
208 bputsn(struct fastbuf *f, byte *b)