]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-null.c
xtypes: added first shot on unit parser
[libucw.git] / ucw / fb-null.c
1 /*
2  *      UCW Library -- Null fastbuf
3  *
4  *      (c) 2014 Pavel Charvat <pchar@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 <ucw/lib.h>
11 #include <ucw/fastbuf.h>
12
13 #include <stdio.h>
14
15 static void fbnull_close(struct fastbuf *b)
16 {
17   xfree(b);
18 }
19
20 struct fastbuf *fbnull_open(uint bufsize)
21 {
22   struct fastbuf *b = xmalloc(sizeof(*b) + bufsize);
23   bzero(b, sizeof(*b));
24   b->name = "<fbnull>";
25   b->close = fbnull_close;
26   fbnull_start(b, (byte *)(b + 1), bufsize);
27   return b;
28 }
29
30 static int fbnull_refill(struct fastbuf *b UNUSED)
31 {
32   return 0;
33 }
34
35 static void fbnull_spout(struct fastbuf *b)
36 {
37   b->pos += b->bptr - b->bstop;
38   b->bptr = b->bstop;
39 }
40
41 static int fbnull_seek(struct fastbuf *b, ucw_off_t pos, int whence)
42 {
43   b->pos = (whence == SEEK_END) ? 0 : pos;
44   b->bptr = b->bstop;
45   return 1;
46 }
47
48 void fbnull_start(struct fastbuf *b, byte *buf, uint bufsize)
49 {
50   ASSERT(buf && bufsize);
51   b->pos = btell(b);
52   b->buffer = b->bptr = b->bstop = buf;
53   b->bufend = buf + bufsize;
54   b->refill = fbnull_refill;
55   b->spout = fbnull_spout;
56   b->seek = fbnull_seek;
57   b->can_overwrite_buffer = 2;
58 }
59
60 bool fbnull_test(struct fastbuf *b)
61 {
62   return b->refill == fbnull_refill;
63 }
64
65 #ifdef TEST
66 int main(void)
67 {
68   struct fastbuf *b = fbnull_open(7);
69   for (uint i = 0; i < 100; i++)
70     {
71       if (btell(b) != i * 10)
72         ASSERT(0);
73       if (bgetc(b) >= 0)
74         ASSERT(0);
75       bputs(b, "0123456789");
76       bflush(b);
77     }
78   if (bfilesize(b) != 0)
79     ASSERT(0);
80   if (btell(b) != 100 * 10)
81     ASSERT(0);
82   bclose(b);
83   return 0;
84 }
85 #endif