2 * UCW Library -- Fast Buffered I/O on itself
4 * (c) 2012 Jan Moskyto Matejka <mq@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/clists.h>
12 #include <ucw/fastbuf.h>
13 #include <ucw/mempool.h>
24 #define FB_MULTI(f) ((struct fb_multi *)(f))
31 #define SUBBUF(f) ((struct subbuf *)(f))
34 fbmulti_subbuf_get_end(struct subbuf *s)
37 bseek(s->fb, 0, SEEK_END);
38 s->end = s->begin + btell(s->fb);
43 fbmulti_subbuf_next(struct fastbuf *f)
45 struct subbuf* next = clist_next(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
50 next->begin = FB_MULTI(f)->cur->end;
51 FB_MULTI(f)->cur = next;
56 fbmulti_refill(struct fastbuf *f)
58 if (f->bufend == f->bstop)
59 f->bptr = f->bstop = f->buffer;
60 uns len = bread(FB_MULTI(f)->cur->fb, f->bstop, (f->bufend - f->bstop));
66 // Current buf returned EOF
67 // Update the information on end of this buffer
68 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
70 // Take the next one if exists
71 if (fbmulti_subbuf_next(f))
72 return fbmulti_refill(f);
78 fbmulti_get_len(struct fastbuf *f)
83 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
85 n->begin = FB_MULTI(f)->len;
86 fbmulti_subbuf_get_end(n);
87 FB_MULTI(f)->len = n->end;
92 fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
98 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
99 FB_MULTI(f)->cur->begin = 0;
101 return fbmulti_seek(f, pos, SEEK_SET);
105 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
106 if (pos < FB_MULTI(f)->cur->end)
109 if (!fbmulti_subbuf_next(f))
110 bthrow(f, "seek", "Seek out of range");
114 bseek(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin), SEEK_SET);
116 f->bptr = f->bstop = f->buffer;
122 return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_CUR);
131 fbmulti_update_capability(struct fastbuf *f) {
132 // FB Multi is only a proxy to other fastbufs ... if any of them lacks
133 // support of any feature, FB Multi also provides no support of that feature
134 f->refill = fbmulti_refill;
135 f->seek = fbmulti_seek;
137 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs)) {
147 fbmulti_create(uns bufsize, ...)
149 struct mempool *mp = mp_new(bufsize);
150 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
151 FB_MULTI(fb_out)->mp = mp;
153 struct fastbuf *fb_in;
154 clist* subbufs = mp_alloc(mp, sizeof(clist));
156 FB_MULTI(fb_out)->subbufs = subbufs;
159 va_start(args, bufsize);
160 while (fb_in = va_arg(args, struct fastbuf *)) {
161 struct subbuf *sb = mp_alloc(mp, sizeof(struct subbuf));
163 clist_add_tail(subbufs, &(sb->n));
167 FB_MULTI(fb_out)->cur = clist_head(subbufs);
169 fb_out->buffer = mp_alloc(mp, bufsize);
170 fb_out->bptr = fb_out->bstop = fb_out->buffer;
171 fb_out->bufend = fb_out->buffer + bufsize;
172 fb_out->name = "<multi>";
174 fbmulti_update_capability(fb_out);
181 int main(int argc, char ** argv)
185 fprintf(stderr, "You must specify a test (r, w, o)\n");
192 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
193 struct fastbuf fb[ARRAY_SIZE(data)];
194 for (uns i=0;i<ARRAY_SIZE(data);i++)
195 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
197 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
200 while (bgets(f, buffer, 9))
208 char *data[] = { "Mnl", "ige" };
209 struct fastbuf fb[ARRAY_SIZE(data)];
210 for (uns i=0;i<ARRAY_SIZE(data);i++)
211 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
213 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], NULL);
215 int pos[] = {0, 3, 1, 4, 2, 5};
217 for (uns i=0;i<ARRAY_SIZE(pos);i++) {
218 bseek(f, pos[i], SEEK_SET);