2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2000 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
15 void bclose(struct fastbuf *f)
25 void bflush(struct fastbuf *f)
27 if (f->bptr != f->buffer)
28 { /* Have something to flush */
29 if (f->bstop > f->buffer) /* Read data? */
30 f->bptr = f->bstop = f->buffer;
31 else /* Write data... */
36 inline void bsetpos(struct fastbuf *f, sh_off_t pos)
38 /* We can optimize seeks only when reading */
39 if (pos >= f->pos - (f->bstop - f->buffer) && pos <= f->pos)
40 f->bptr = f->bstop + (pos - f->pos);
44 f->seek(f, pos, SEEK_SET);
48 void bseek(struct fastbuf *f, sh_off_t pos, int whence)
53 return bsetpos(f, pos);
55 return bsetpos(f, btell(f) + pos);
58 f->seek(f, pos, SEEK_END);
61 die("bseek: invalid whence=%d", whence);
65 int bgetc_slow(struct fastbuf *f)
67 if (f->bptr < f->bstop)
74 int bpeekc_slow(struct fastbuf *f)
76 if (f->bptr < f->bstop)
83 void bputc_slow(struct fastbuf *f, uns c)
85 if (f->bptr >= f->bufend)
90 int bgetw_slow(struct fastbuf *f)
100 return (w1 << 8) | w2;
102 return w1 | (w2 << 8);
106 u32 bgetl_slow(struct fastbuf *f)
108 u32 l = bgetc_slow(f);
109 #ifdef CPU_BIG_ENDIAN
110 l = (l << 8) | bgetc_slow(f);
111 l = (l << 8) | bgetc_slow(f);
112 return (l << 8) | bgetc_slow(f);
114 l = (bgetc_slow(f) << 8) | l;
115 l = (bgetc_slow(f) << 16) | l;
116 return (bgetc_slow(f) << 24) | l;
120 u64 bgetq_slow(struct fastbuf *f)
123 #ifdef CPU_BIG_ENDIAN
130 return ((u64) h << 32) | l;
133 u64 bget5_slow(struct fastbuf *f)
136 #ifdef CPU_BIG_ENDIAN
143 return ((u64) h << 32) | l;
146 void bputw_slow(struct fastbuf *f, uns w)
148 #ifdef CPU_BIG_ENDIAN
149 bputc_slow(f, w >> 8);
153 bputc_slow(f, w >> 8);
157 void bputl_slow(struct fastbuf *f, u32 l)
159 #ifdef CPU_BIG_ENDIAN
160 bputc_slow(f, l >> 24);
161 bputc_slow(f, l >> 16);
162 bputc_slow(f, l >> 8);
166 bputc_slow(f, l >> 8);
167 bputc_slow(f, l >> 16);
168 bputc_slow(f, l >> 24);
172 void bputq_slow(struct fastbuf *f, u64 q)
174 #ifdef CPU_BIG_ENDIAN
175 bputl_slow(f, q >> 32);
179 bputl_slow(f, q >> 32);
183 void bput5_slow(struct fastbuf *f, u64 o)
187 #ifdef CPU_BIG_ENDIAN
196 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check)
201 uns k = f->bstop - f->bptr;
206 k = f->bstop - f->bptr;
212 memcpy(b, f->bptr, k);
218 if (check && total && l)
219 die("breadb: short read");
223 void bwrite_slow(struct fastbuf *f, void *b, uns l)
227 uns k = f->bufend - f->bptr;
232 k = f->bufend - f->bptr;
236 memcpy(f->bptr, b, k);
243 byte * /* Non-standard */
244 bgets(struct fastbuf *f, byte *b, uns l)
254 if (k == '\n' || k == EOF)
262 die("%s: Line too long", f->name);
266 bgets0(struct fastbuf *f, byte *b, uns l)
284 die("%s: Line too long", f->name);
288 bdirect_read_prepare(struct fastbuf *f, byte **buf)
290 if (f->bptr == f->bstop && !f->refill(f))
293 return f->bstop - f->bptr;
297 bdirect_read_commit(struct fastbuf *f, byte *pos)
303 bdirect_write_prepare(struct fastbuf *f, byte **buf)
305 if (f->bptr == f->bufend)
308 return f->bufend - f->bptr;
312 bdirect_write_commit(struct fastbuf *f, byte *pos)