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 UNUSED)
23 fbbuf_seek(struct fastbuf *f, ucw_off_t pos, int whence)
25 /* Somebody might want to seek to the end of buffer, try to be nice to him. */
26 ucw_off_t len = f->bufend - f->buffer;
27 if (whence == SEEK_END)
29 ASSERT(pos >= 0 && pos <= len);
30 f->bptr = f->buffer + pos;
37 fbbuf_init_read(struct fastbuf *f, byte *buf, uns size, uns can_overwrite)
39 *f = (struct fastbuf) {
46 .refill = fbbuf_refill,
48 .can_overwrite_buffer = can_overwrite };
52 fbbuf_spout(struct fastbuf *f)
54 bthrow(f, "fb.write", "fbbuf: buffer overflow on write");
58 fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
60 *f = (struct fastbuf) {
65 .name = "fbbuf-write",
73 int main(int argc, char *argv[])
77 fprintf(stderr, "You must specify a test (r, w, o)\n");
85 char *data = "Two\nlines\n";
86 fbbuf_init_read(&fb, data, strlen(data), 0);
88 while (bgets(&fb, buffer, 10))
97 fbbuf_init_write(&fb, buff, 20);
98 bputs(&fb, "Hello world\n");
107 fbbuf_init_write(&fb, buff, 4);