2 * UCW Library -- Fast Buffered I/O on Static Buffers
4 * (c) 2003--2006 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 <ucw/fastbuf.h>
17 fbbuf_refill(struct fastbuf *f)
20 f->pos = f->bstop - f->buffer;
21 return f->bptr < f->bstop;
25 fbbuf_seek(struct fastbuf *f, ucw_off_t pos, int whence)
27 /* Somebody might want to seek to the end of buffer, try to be nice to him. */
28 ucw_off_t len = f->bufend - f->buffer;
29 if (whence == SEEK_END)
31 if (pos < 0 || pos > len)
32 bthrow(f, "seek", "Seek out of range");
33 f->bstop = f->bptr = f->buffer + pos;
39 fbbuf_init_read(struct fastbuf *f, byte *buf, uint size, uint can_overwrite)
41 *f = (struct fastbuf) {
48 .refill = fbbuf_refill,
50 .can_overwrite_buffer = can_overwrite
55 fbbuf_spout(struct fastbuf *f)
57 if (f->bptr >= f->bufend)
58 bthrow(f, "write", "fbbuf: buffer overflow on write");
62 fbbuf_init_write(struct fastbuf *f, byte *buf, uint size)
64 *f = (struct fastbuf) {
69 .name = "fbbuf-write",
76 int main(int argc, char *argv[])
80 fprintf(stderr, "You must specify a test (r, w, o)\n");
88 char *data = "Two\nlines\n";
89 fbbuf_init_read(&fb, data, strlen(data), 0);
91 while (bgets(&fb, buffer, 10))
100 fbbuf_init_write(&fb, buff, 20);
101 bputs(&fb, "Hello world\n");
110 fbbuf_init_write(&fb, buff, 4);