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>
17 #define FB_MULTI_NAME "<multi>"
27 #define FB_MULTI(f) ((struct fb_multi *)(f))
37 fbmulti_subbuf_get_end(struct subbuf *s)
41 bseek(s->fb, 0, SEEK_END);
42 s->end = s->begin + btell(s->fb);
47 fbmulti_subbuf_next(struct fastbuf *f)
49 struct subbuf* next = clist_next(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
56 next->begin = FB_MULTI(f)->cur->end;
59 FB_MULTI(f)->cur = next;
64 fbmulti_refill(struct fastbuf *f)
66 if (f->bufend == f->bstop)
67 f->bptr = f->bstop = f->buffer;
69 uns len = bread(FB_MULTI(f)->cur->fb, f->bstop, (f->bufend - f->bstop));
75 // Current buf returned EOF
76 // Update the information on end of this buffer
77 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
79 // Take the next one if exists
80 if (fbmulti_subbuf_next(f))
81 return fbmulti_refill(f);
87 fbmulti_get_len(struct fastbuf *f)
92 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
94 n->begin = FB_MULTI(f)->len;
95 fbmulti_subbuf_get_end(n);
96 FB_MULTI(f)->len = n->end;
101 fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
108 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
109 FB_MULTI(f)->cur->begin = 0;
111 return fbmulti_seek(f, pos, SEEK_SET);
116 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
117 if (pos < FB_MULTI(f)->cur->end)
120 if (!fbmulti_subbuf_next(f))
122 if (pos == FB_MULTI(f)->cur->end)
125 bthrow(f, "seek", "Seek out of range");
131 bsetpos(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin));
133 f->bptr = f->bstop = f->buffer;
139 return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_SET);
148 fbmulti_update_capability(struct fastbuf *f)
150 // FB Multi is only a proxy to other fastbufs ... if any of them lacks
151 // support of any feature, FB Multi also provides no support of that feature
152 f->refill = fbmulti_refill;
153 f->seek = fbmulti_seek;
155 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
157 ASSERT(n->fb->refill)
165 fbmulti_close(struct fastbuf *f)
167 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
171 mp_delete(FB_MULTI(f)->mp);
175 fbmulti_create(uns bufsize, ...)
177 struct mempool *mp = mp_new(bufsize);
178 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
179 FB_MULTI(fb_out)->mp = mp;
181 struct fastbuf *fb_in;
182 clist* subbufs = mp_alloc(mp, sizeof(clist));
184 FB_MULTI(fb_out)->subbufs = subbufs;
187 va_start(args, bufsize);
188 while (fb_in = va_arg(args, struct fastbuf *))
189 fbmulti_append(fb_out, fb_in, 1);
193 FB_MULTI(fb_out)->cur = clist_head(subbufs);
195 fb_out->buffer = mp_alloc(mp, bufsize);
196 fb_out->bptr = fb_out->bstop = fb_out->buffer;
197 fb_out->bufend = fb_out->buffer + bufsize;
198 fb_out->name = FB_MULTI_NAME;
200 fbmulti_update_capability(fb_out);
201 fb_out->close = fbmulti_close;
207 fbmulti_append(struct fastbuf *f, struct fastbuf *fb, int allow_close)
209 struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(struct subbuf));
211 sb->allow_close = allow_close;
212 clist_add_tail(FB_MULTI(f)->subbufs, &(sb->n));
215 static void fbmulti_flatten_internal(struct fastbuf *f, clist* c, int allow_close)
217 CLIST_FOR_EACH(struct subbuf *, n, *c)
219 if (strcmp(n->fb->name, FB_MULTI_NAME))
220 fbmulti_append(f, n->fb, n->allow_close && allow_close);
224 fbmulti_flatten_internal(f, FB_MULTI(n->fb)->subbufs, allow_close && n->allow_close);
225 if (allow_close && n->allow_close)
227 FB_MULTI(n->fb)->subbufs = mp_alloc(FB_MULTI(n->fb)->mp, sizeof(clist));
228 clist_init(FB_MULTI(n->fb)->subbufs);
236 fbmulti_flatten(struct fastbuf *f)
238 if (strcmp(f->name, FB_MULTI_NAME))
240 DBG("fbmulti: given fastbuf isn't fbmulti");
244 clist* c = FB_MULTI(f)->subbufs;
245 FB_MULTI(f)->subbufs = mp_alloc(FB_MULTI(f)->mp, sizeof(clist));
246 clist_init(FB_MULTI(f)->subbufs);
248 fbmulti_flatten_internal(f, c, 1);
249 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
250 f->bptr = f->bstop = f->buffer;
256 int main(int argc, char ** argv)
260 fprintf(stderr, "You must specify a test (r, w, o)\n");
267 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
268 struct fastbuf fb[ARRAY_SIZE(data)];
269 for (uns i=0;i<ARRAY_SIZE(data);i++)
270 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
272 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
275 while (bgets(f, buffer, 9))
283 char *data[] = { "Mnl", "ige" };
284 struct fastbuf fb[ARRAY_SIZE(data)];
285 for (uns i=0;i<ARRAY_SIZE(data);i++)
286 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
288 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], NULL);
290 int pos[] = {0, 3, 1, 4, 2, 5};
292 for (uns i=0;i<ARRAY_SIZE(pos);i++)
303 char *data = "Insae";
304 struct fastbuf fb[4];
305 fbbuf_init_read(&fb[0], data, 1, 0);
306 fbbuf_init_read(&fb[1], data + 1, 1, 0);
307 fbbuf_init_read(&fb[2], data + 2, 2, 0);
308 fbbuf_init_read(&fb[3], data + 4, 1, 0);
310 struct fastbuf* f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
313 while(bgets(f, buffer, 9))
322 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
323 struct fastbuf fb[ARRAY_SIZE(data)];
324 for (uns i=0;i<ARRAY_SIZE(data);i++)
325 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
328 fbbuf_init_read(&sp, " ", 1, 0);
331 fbbuf_init_read(&nl, "\n", 1, 0);
333 struct fastbuf *f = fbmulti_create(4,
358 while (bgets(f, buffer, 20))