X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Ffastbuf.c;h=b7be624b5645d5ac34e1e52ca83b1ce2a951be62;hb=cd5b7ea487ab7692635ba7eca98fcb55f6d996a7;hp=9f5d36924da5dbbbb88e1b76227d96c6d0bcb694;hpb=49ed04e2e93a6a5b01058638224621d5c07db01c;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index 9f5d3692..b7be624b 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -10,7 +10,6 @@ #include "lib/lib.h" #include "lib/fastbuf.h" -#include #include void bclose(struct fastbuf *f) @@ -18,29 +17,24 @@ void bclose(struct fastbuf *f) if (f) { bflush(f); - f->close(f); - xfree(f); + if (f->close) + f->close(f); } } void bflush(struct fastbuf *f) { - if (f->bptr != f->buffer) - { /* Have something to flush */ - if (f->bstop > f->buffer) /* Read data? */ - { - f->bptr = f->bstop = f->buffer; - f->pos = f->fdpos; - } - else /* Write data... */ - f->spout(f); - } + if (f->bptr > f->bstop) + f->spout(f); + else if (f->bstop > f->buffer) + f->bptr = f->bstop = f->buffer; } inline void bsetpos(struct fastbuf *f, sh_off_t pos) { - if (pos >= f->pos && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer))) - f->bptr = f->buffer + (pos - f->pos); + /* We can optimize seeks only when reading */ + if (pos >= f->pos - (f->bstop - f->buffer) && pos <= f->pos) + f->bptr = f->bstop + (pos - f->pos); else { bflush(f); @@ -287,30 +281,29 @@ bgets0(struct fastbuf *f, byte *b, uns l) die("%s: Line too long", f->name); } -int -bdirect_read(struct fastbuf *f, byte **buf) +void +bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l) { - int len; + while (l) + { + byte *fptr, *tptr; + uns favail, tavail, n; - if (f->bptr == f->bstop && !f->refill(f)) - return EOF; - *buf = f->bptr; - len = f->bstop - f->bptr; - f->bptr += len; - return len; + favail = bdirect_read_prepare(f, &fptr); + if (!favail) + die("bbcopy: source exhausted"); + tavail = bdirect_write_prepare(t, &tptr); + n = MIN(l, favail); + n = MIN(n, tavail); + memcpy(tptr, fptr, n); + bdirect_read_commit(f, fptr + n); + bdirect_write_commit(t, tptr + n); + l -= n; + } } int -bdirect_write_prepare(struct fastbuf *f, byte **buf) -{ - if (f->bptr == f->bufend) - f->spout(f); - *buf = f->bptr; - return f->bufend - f->bptr; -} - -void -bdirect_write_commit(struct fastbuf *f, byte *pos) +bconfig(struct fastbuf *f, uns item, int value) { - f->bptr = pos; + return f->config ? f->config(f, item, value) : -1; }