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 bseek(next->fb, 0, SEEK_SET);
51 next->begin = FB_MULTI(f)->cur->end;
54 FB_MULTI(f)->cur = next;
59 fbmulti_refill(struct fastbuf *f)
61 if (f->bufend == f->bstop)
62 f->bptr = f->bstop = f->buffer;
63 uns len = bread(FB_MULTI(f)->cur->fb, f->bstop, (f->bufend - f->bstop));
69 // Current buf returned EOF
70 // Update the information on end of this buffer
71 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
73 // Take the next one if exists
74 if (fbmulti_subbuf_next(f))
75 return fbmulti_refill(f);
81 fbmulti_get_len(struct fastbuf *f)
86 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
88 n->begin = FB_MULTI(f)->len;
89 fbmulti_subbuf_get_end(n);
90 FB_MULTI(f)->len = n->end;
95 fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
101 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
102 FB_MULTI(f)->cur->begin = 0;
104 return fbmulti_seek(f, pos, SEEK_SET);
108 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
109 if (pos < FB_MULTI(f)->cur->end)
112 if (!fbmulti_subbuf_next(f)) {
113 if (pos == FB_MULTI(f)->cur->end)
116 bthrow(f, "seek", "Seek out of range");
121 bseek(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin), SEEK_SET);
123 f->bptr = f->bstop = f->buffer;
129 return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_SET);
138 fbmulti_update_capability(struct fastbuf *f) {
139 // FB Multi is only a proxy to other fastbufs ... if any of them lacks
140 // support of any feature, FB Multi also provides no support of that feature
141 f->refill = fbmulti_refill;
142 f->seek = fbmulti_seek;
144 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs)) {
154 fbmulti_create(uns bufsize, ...)
156 struct mempool *mp = mp_new(bufsize);
157 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
158 FB_MULTI(fb_out)->mp = mp;
160 struct fastbuf *fb_in;
161 clist* subbufs = mp_alloc(mp, sizeof(clist));
163 FB_MULTI(fb_out)->subbufs = subbufs;
166 va_start(args, bufsize);
167 while (fb_in = va_arg(args, struct fastbuf *)) {
168 struct subbuf *sb = mp_alloc(mp, sizeof(struct subbuf));
170 clist_add_tail(subbufs, &(sb->n));
174 FB_MULTI(fb_out)->cur = clist_head(subbufs);
176 fb_out->buffer = mp_alloc(mp, bufsize);
177 fb_out->bptr = fb_out->bstop = fb_out->buffer;
178 fb_out->bufend = fb_out->buffer + bufsize;
179 fb_out->name = "<multi>";
181 fbmulti_update_capability(fb_out);
188 int main(int argc, char ** argv)
192 fprintf(stderr, "You must specify a test (r, w, o)\n");
199 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
200 struct fastbuf fb[ARRAY_SIZE(data)];
201 for (uns i=0;i<ARRAY_SIZE(data);i++)
202 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
204 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
207 while (bgets(f, buffer, 9))
215 char *data[] = { "Mnl", "ige" };
216 struct fastbuf fb[ARRAY_SIZE(data)];
217 for (uns i=0;i<ARRAY_SIZE(data);i++)
218 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
220 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], NULL);
222 int pos[] = {0, 3, 1, 4, 2, 5};
224 for (uns i=0;i<ARRAY_SIZE(pos);i++) {
225 bseek(f, pos[i], SEEK_SET);
234 char *data = "Insae";
235 struct fastbuf fb[4];
236 fbbuf_init_read(&fb[0], data, 1, 0);
237 fbbuf_init_read(&fb[1], data + 1, 1, 0);
238 fbbuf_init_read(&fb[2], data + 2, 2, 0);
239 fbbuf_init_read(&fb[3], data + 4, 1, 0);
241 struct fastbuf* f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
244 while(bgets(f, buffer, 9))
252 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
253 struct fastbuf fb[ARRAY_SIZE(data)];
254 for (uns i=0;i<ARRAY_SIZE(data);i++)
255 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
258 fbbuf_init_read(&sp, " ", 1, 0);
261 fbbuf_init_read(&nl, "\n", 1, 0);
263 struct fastbuf *f = fbmulti_create(4,
285 while (bgets(f, buffer, 20))