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_close(struct fastbuf *f) {
155 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
158 mp_delete(FB_MULTI(f)->mp);
162 fbmulti_create(uns bufsize, ...)
164 struct mempool *mp = mp_new(bufsize);
165 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
166 FB_MULTI(fb_out)->mp = mp;
168 struct fastbuf *fb_in;
169 clist* subbufs = mp_alloc(mp, sizeof(clist));
171 FB_MULTI(fb_out)->subbufs = subbufs;
174 va_start(args, bufsize);
175 while (fb_in = va_arg(args, struct fastbuf *)) {
176 struct subbuf *sb = mp_alloc(mp, sizeof(struct subbuf));
178 clist_add_tail(subbufs, &(sb->n));
182 FB_MULTI(fb_out)->cur = clist_head(subbufs);
184 fb_out->buffer = mp_alloc(mp, bufsize);
185 fb_out->bptr = fb_out->bstop = fb_out->buffer;
186 fb_out->bufend = fb_out->buffer + bufsize;
187 fb_out->name = "<multi>";
189 fbmulti_update_capability(fb_out);
190 fb_out->close = fbmulti_close;
197 int main(int argc, char ** argv)
201 fprintf(stderr, "You must specify a test (r, w, o)\n");
208 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
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], &fb[2], NULL);
216 while (bgets(f, buffer, 9))
224 char *data[] = { "Mnl", "ige" };
225 struct fastbuf fb[ARRAY_SIZE(data)];
226 for (uns i=0;i<ARRAY_SIZE(data);i++)
227 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
229 struct fastbuf* f = fbmulti_create(4, &fb[0], &fb[1], NULL);
231 int pos[] = {0, 3, 1, 4, 2, 5};
233 for (uns i=0;i<ARRAY_SIZE(pos);i++) {
234 bseek(f, pos[i], SEEK_SET);
243 char *data = "Insae";
244 struct fastbuf fb[4];
245 fbbuf_init_read(&fb[0], data, 1, 0);
246 fbbuf_init_read(&fb[1], data + 1, 1, 0);
247 fbbuf_init_read(&fb[2], data + 2, 2, 0);
248 fbbuf_init_read(&fb[3], data + 4, 1, 0);
250 struct fastbuf* f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
253 while(bgets(f, buffer, 9))
261 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
262 struct fastbuf fb[ARRAY_SIZE(data)];
263 for (uns i=0;i<ARRAY_SIZE(data);i++)
264 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
267 fbbuf_init_read(&sp, " ", 1, 0);
270 fbbuf_init_read(&nl, "\n", 1, 0);
272 struct fastbuf *f = fbmulti_create(4,
294 while (bgets(f, buffer, 20))