]> mj.ucw.cz Git - libucw.git/blob - lib/fb-buffer.c
09c7429357e6c518e9630b418067037e4986a43f
[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 void
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 }
33
34 void
35 fbbuf_init_read(struct fastbuf *f, byte *buf, uns size, uns can_overwrite)
36 {
37   f->buffer = f->bptr = buf;
38   f->bstop = f->bufend = buf + size;
39   f->name = "fbbuf-read";
40   f->pos = size;
41   f->refill = fbbuf_refill;
42   f->spout = NULL;
43   f->seek = fbbuf_seek;
44   f->close = NULL;
45   f->config = NULL;
46   f->can_overwrite_buffer = can_overwrite;
47 }
48
49 static void
50 fbbuf_spout(struct fastbuf *f UNUSED)
51 {
52   die("fbbuf: buffer overflow on write");
53 }
54
55 void
56 fbbuf_init_write(struct fastbuf *f, byte *buf, uns size)
57 {
58   f->buffer = f->bstop = f->bptr = buf;
59   f->bufend = buf + size;
60   f->name = "fbbuf-write";
61   f->pos = size;
62   f->refill = NULL;
63   f->spout = fbbuf_spout;
64   f->seek = NULL;
65   f->close = NULL;
66   f->config = NULL;
67   f->can_overwrite_buffer = 0;
68 }