]> mj.ucw.cz Git - libucw.git/blob - lib/fb-buffer.c
9ad248a289db329ecabec693c29f103c78e47965
[libucw.git] / lib / fb-buffer.c
1 /*
2  *      UCW Library -- Fast Buffered I/O on Static Buffers
3  *
4  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12
13 #include <stdlib.h>
14
15 static int
16 fbbuf_refill(struct fastbuf *f UNUSED)
17 {
18   return 0;
19 }
20
21 static int
22 fbbuf_seek(struct fastbuf *f, sh_off_t pos, int whence)
23 {
24   /* Somebody might want to seek to the end of buffer, try to be nice to him. */
25   sh_off_t len = f->bufend - f->buffer;
26   if (whence == SEEK_END)
27     pos += len;
28   ASSERT(pos >= 0 && pos <= len);
29   f->bptr = f->buffer + pos;
30   f->bstop = f->bufend;
31   f->pos = len;
32   return 1;
33 }
34
35 void
36 fbbuf_init_read(struct fastbuf *f, byte *buf, uns size, uns can_overwrite)
37 {
38   f->buffer = f->bptr = buf;
39   f->bstop = f->bufend = buf + size;
40   f->name = "fbbuf-read";
41   f->pos = size;
42   f->refill = fbbuf_refill;
43   f->spout = NULL;
44   f->seek = fbbuf_seek;
45   f->close = NULL;
46   f->config = NULL;
47   f->can_overwrite_buffer = can_overwrite;
48 }
49
50 static void
51 fbbuf_spout(struct fastbuf *f UNUSED)
52 {
53   die("fbbuf: buffer overflow on write");
54 }
55
56 void
57 fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
58 {
59   f->buffer = f->bstop = f->bptr = buf;
60   f->bufend = buf + size;
61   f->name = "fbbuf-write";
62   f->pos = size;
63   f->refill = NULL;
64   f->spout = fbbuf_spout;
65   f->seek = NULL;
66   f->close = NULL;
67   f->config = NULL;
68   f->can_overwrite_buffer = 0;
69 }