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->bptr = f->buffer + pos;
40 fbbuf_init_read(struct fastbuf *f, byte *buf, uns size, uns can_overwrite)
42 *f = (struct fastbuf) {
49 .refill = fbbuf_refill,
51 .can_overwrite_buffer = can_overwrite };
55 fbbuf_spout(struct fastbuf *f)
57 bthrow(f, "write", "fbbuf: buffer overflow on write");
61 fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
63 *f = (struct fastbuf) {
68 .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);