2 * UCW Library -- Fast Buffered I/O
4 * (c) 1997--2007 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"
16 void bclose(struct fastbuf *f)
26 void bflush(struct fastbuf *f)
28 if (f->bptr > f->bstop)
30 else if (f->bstop > f->buffer)
31 f->bptr = f->bstop = f->buffer;
34 inline void bsetpos(struct fastbuf *f, sh_off_t pos)
36 /* We can optimize seeks only when reading */
37 if (pos >= f->pos - (f->bstop - f->buffer) && pos <= f->pos)
38 f->bptr = f->bstop + (pos - f->pos);
42 if (!f->seek || !f->seek(f, pos, SEEK_SET))
43 die("bsetpos: stream not seekable");
47 void bseek(struct fastbuf *f, sh_off_t pos, int whence)
52 return bsetpos(f, pos);
54 return bsetpos(f, btell(f) + pos);
57 if (!f->seek || !f->seek(f, pos, SEEK_END))
58 die("bseek: stream not seekable");
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 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check)
95 uns k = f->bstop - f->bptr;
100 k = f->bstop - f->bptr;
106 memcpy(b, f->bptr, k);
114 die("breada: short read");
116 die("breadb: short read");
120 void bwrite_slow(struct fastbuf *f, void *b, uns l)
124 uns k = f->bufend - f->bptr;
129 k = f->bufend - f->bptr;
133 memcpy(f->bptr, b, k);
141 bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
146 uns favail, tavail, n;
148 favail = bdirect_read_prepare(f, &fptr);
153 die("bbcopy: source exhausted");
155 tavail = bdirect_write_prepare(t, &tptr);
158 memcpy(tptr, fptr, n);
159 bdirect_read_commit(f, fptr + n);
160 bdirect_write_commit(t, tptr + n);
167 bconfig(struct fastbuf *f, uns item, int value)
169 return f->config ? f->config(f, item, value) : -1;
173 brewind(struct fastbuf *f)
180 bskip_slow(struct fastbuf *f, uns len)
185 uns l = bdirect_read_prepare(f, &buf);
189 bdirect_read_commit(f, buf+l);
196 bfilesize(struct fastbuf *f)
200 sh_off_t pos = btell(f);
202 if (!f->seek(f, 0, SEEK_END))
204 sh_off_t len = btell(f);