]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-null.c
Merge branch 'master' of ssh://git.ucw.cz/projects/libucw/GIT/libucw
[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(uns bufsize)
21 {
22   struct fastbuf *b = xmalloc(sizeof(*b) + bufsize);
23   bzero(b, sizeof(*b));
24   b->close = fbnull_close;
25   fbnull_start(b, (byte *)(b + 1), bufsize);
26   return b;
27 }
28
29 static int fbnull_refill(struct fastbuf *b UNUSED)
30 {
31   return 0;
32 }
33
34 static void fbnull_spout(struct fastbuf *b)
35 {
36   b->pos += b->bptr - b->bstop;
37   b->bptr = b->bstop;
38 }
39
40 static int fbnull_seek(struct fastbuf *b, ucw_off_t pos, int whence)
41 {
42   b->pos = (whence == SEEK_END) ? 0 : pos;
43   b->bptr = b->bstop;
44   return 1;
45 }
46
47 void fbnull_start(struct fastbuf *b, byte *buf, uns bufsize)
48 {
49   ASSERT(buf && bufsize);
50   b->pos = btell(b);
51   b->buffer = b->bptr = b->bstop = buf;
52   b->bufend = buf + bufsize;
53   b->refill = fbnull_refill;
54   b->spout = fbnull_spout;
55   b->seek = fbnull_seek;
56   b->can_overwrite_buffer = 2;
57 }
58
59 bool fbnull_test(struct fastbuf *b)
60 {
61   return b->refill == fbnull_refill;
62 }
63
64 #ifdef TEST
65 int main(void)
66 {
67   struct fastbuf *b = fbnull_open(7);
68   for (uns i = 0; i < 100; i++)
69     {
70       if (btell(b) != i * 10)
71         ASSERT(0);
72       if (bgetc(b) >= 0)
73         ASSERT(0);
74       bputs(b, "0123456789");
75       bflush(b);
76     }
77   if (bfilesize(b) != 0)
78     ASSERT(0);
79   if (btell(b) != 100 * 10)
80     ASSERT(0);
81   bclose(b);
82   return 0;
83 }
84 #endif