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, uns size, uns can_overwrite)
41 *f = (struct fastbuf) {
48 .refill = fbbuf_refill,
50 .can_overwrite_buffer = can_overwrite };
54 fbbuf_spout(struct fastbuf *f)
56 bthrow(f, "write", "fbbuf: buffer overflow on write");
60 fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
62 *f = (struct fastbuf) {
67 .name = "fbbuf-write",
74 int main(int argc, char *argv[])
78 fprintf(stderr, "You must specify a test (r, w, o)\n");
86 char *data = "Two\nlines\n";
87 fbbuf_init_read(&fb, data, strlen(data), 0);
89 while (bgets(&fb, buffer, 10))
98 fbbuf_init_write(&fb, buff, 20);
99 bputs(&fb, "Hello world\n");
108 fbbuf_init_write(&fb, buff, 4);