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>"
26 #define FB_MULTI(f) ((struct fb_multi *)(f))
34 #define SUBBUF(f) ((struct subbuf *)(f))
37 fbmulti_subbuf_get_end(struct subbuf *s)
40 bseek(s->fb, 0, SEEK_END);
41 s->end = s->begin + btell(s->fb);
46 fbmulti_subbuf_next(struct fastbuf *f)
48 struct subbuf* next = clist_next(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
53 bseek(next->fb, 0, SEEK_SET);
54 next->begin = FB_MULTI(f)->cur->end;
57 FB_MULTI(f)->cur = next;
62 fbmulti_refill(struct fastbuf *f)
64 if (f->bufend == f->bstop)
65 f->bptr = f->bstop = f->buffer;
66 uns len = bread(FB_MULTI(f)->cur->fb, f->bstop, (f->bufend - f->bstop));
72 // Current buf returned EOF
73 // Update the information on end of this buffer
74 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
76 // Take the next one if exists
77 if (fbmulti_subbuf_next(f))
78 return fbmulti_refill(f);
84 fbmulti_get_len(struct fastbuf *f)
89 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
91 n->begin = FB_MULTI(f)->len;
92 fbmulti_subbuf_get_end(n);
93 FB_MULTI(f)->len = n->end;
98 fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
104 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
105 FB_MULTI(f)->cur->begin = 0;
107 return fbmulti_seek(f, pos, SEEK_SET);
111 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
112 if (pos < FB_MULTI(f)->cur->end)
115 if (!fbmulti_subbuf_next(f)) {
116 if (pos == FB_MULTI(f)->cur->end)
119 bthrow(f, "seek", "Seek out of range");
124 bseek(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin), SEEK_SET);
126 f->bptr = f->bstop = f->buffer;
132 return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_SET);
141 fbmulti_update_capability(struct fastbuf *f) {
142 // FB Multi is only a proxy to other fastbufs ... if any of them lacks
143 // support of any feature, FB Multi also provides no support of that feature
144 f->refill = fbmulti_refill;
145 f->seek = fbmulti_seek;
147 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs)) {
157 fbmulti_close(struct fastbuf *f) {
158 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
162 mp_delete(FB_MULTI(f)->mp);
166 fbmulti_create(uns bufsize, ...)
168 struct mempool *mp = mp_new(bufsize);
169 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
170 FB_MULTI(fb_out)->mp = mp;
172 struct fastbuf *fb_in;
173 clist* subbufs = mp_alloc(mp, sizeof(clist));
175 FB_MULTI(fb_out)->subbufs = subbufs;
178 va_start(args, bufsize);
179 while (fb_in = va_arg(args, struct fastbuf *)) {
180 fbmulti_append(fb_out, fb_in, 1);
184 FB_MULTI(fb_out)->cur = clist_head(subbufs);
186 fb_out->buffer = mp_alloc(mp, bufsize);
187 fb_out->bptr = fb_out->bstop = fb_out->buffer;
188 fb_out->bufend = fb_out->buffer + bufsize;
189 fb_out->name = FB_MULTI_NAME;
191 fbmulti_update_capability(fb_out);
192 fb_out->close = fbmulti_close;
198 fbmulti_append(struct fastbuf *f, struct fastbuf *fb, int allow_close) {
199 struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(struct subbuf));
201 sb->allow_close = allow_close;
202 clist_add_tail(FB_MULTI(f)->subbufs, &(sb->n));
205 static void fbmulti_flatten_internal(struct fastbuf *f, clist* c, int allow_close) {
206 CLIST_FOR_EACH(struct subbuf *, n, *c) {
207 if (strcmp(n->fb->name, FB_MULTI_NAME))
208 fbmulti_append(f, n->fb, n->allow_close && allow_close);
210 fbmulti_flatten_internal(f, FB_MULTI(n->fb)->subbufs, allow_close && n->allow_close);
211 if (allow_close && n->allow_close) {
212 FB_MULTI(n->fb)->subbufs = mp_alloc(FB_MULTI(n->fb)->mp, sizeof(clist));
213 clist_init(FB_MULTI(n->fb)->subbufs);
221 fbmulti_flatten(struct fastbuf *f) {
222 if (strcmp(f->name, FB_MULTI_NAME)) {
223 DBG("fbmulti: given fastbuf isn't fbmulti");
227 clist* c = FB_MULTI(f)->subbufs;
228 FB_MULTI(f)->subbufs = mp_alloc(FB_MULTI(f)->mp, sizeof(clist));
229 clist_init(FB_MULTI(f)->subbufs);
231 fbmulti_flatten_internal(f, c, 1);
232 FB_MULTI(f)->cur = clist_head(FB_MULTI(f)->subbufs);
233 f->bptr = f->bstop = f->buffer;
239 int main(int argc, char ** argv)
243 fprintf(stderr, "You must specify a test (r, w, o)\n");
250 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
251 struct fastbuf fb[ARRAY_SIZE(data)];
252 for (uns i=0;i<ARRAY_SIZE(data);i++)
253 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
255 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
258 while (bgets(f, buffer, 9))
266 char *data[] = { "Mnl", "ige" };
267 struct fastbuf fb[ARRAY_SIZE(data)];
268 for (uns i=0;i<ARRAY_SIZE(data);i++)
269 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
271 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], NULL);
273 int pos[] = {0, 3, 1, 4, 2, 5};
275 for (uns i=0;i<ARRAY_SIZE(pos);i++) {
276 bseek(f, pos[i], SEEK_SET);
285 char *data = "Insae";
286 struct fastbuf fb[4];
287 fbbuf_init_read(&fb[0], data, 1, 0);
288 fbbuf_init_read(&fb[1], data + 1, 1, 0);
289 fbbuf_init_read(&fb[2], data + 2, 2, 0);
290 fbbuf_init_read(&fb[3], data + 4, 1, 0);
292 struct fastbuf* f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
295 while(bgets(f, buffer, 9))
304 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
305 struct fastbuf fb[ARRAY_SIZE(data)];
306 for (uns i=0;i<ARRAY_SIZE(data);i++)
307 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
310 fbbuf_init_read(&sp, " ", 1, 0);
313 fbbuf_init_read(&nl, "\n", 1, 0);
315 struct fastbuf *f = fbmulti_create(4,
340 while (bgets(f, buffer, 20))