X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffastbuf.c;h=bf887a18f86acd33ae61603a62486b5a2fbf34b5;hb=b42162f5526360acc6930e3d2e296af1fef08e63;hp=717372fdfcfb42f797b1d2199d96d87f86467216;hpb=168c1f2ddcb8ce07248b4079b6398afe8c2d792a;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index 717372fd..bf887a18 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -1,40 +1,40 @@ /* * Sherlock Library -- Fast Buffered I/O * - * (c) 1997--2000 Martin Mares + * (c) 1997--2004 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ -#include -#include +#include "lib/lib.h" +#include "lib/fastbuf.h" -#include "lib.h" -#include "fastbuf.h" +#include void bclose(struct fastbuf *f) { - bflush(f); - f->close(f); - free(f); + if (f) + { + bflush(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); @@ -44,8 +44,6 @@ inline void bsetpos(struct fastbuf *f, sh_off_t pos) void bseek(struct fastbuf *f, sh_off_t pos, int whence) { - sh_off_t l; - switch (whence) { case SEEK_SET: @@ -79,20 +77,26 @@ int bpeekc_slow(struct fastbuf *f) return *f->bptr; } -void bputc_slow(struct fastbuf *f, byte c) +void bputc_slow(struct fastbuf *f, uns c) { if (f->bptr >= f->bufend) f->spout(f); *f->bptr++ = c; } -word bgetw_slow(struct fastbuf *f) +int bgetw_slow(struct fastbuf *f) { - word w = bgetc_slow(f); + int w1, w2; + w1 = bgetc_slow(f); + if (w1 < 0) + return w1; + w2 = bgetc_slow(f); + if (w2 < 0) + return w2; #ifdef CPU_BIG_ENDIAN - return (w << 8) | bgetc_slow(f); + return (w1 << 8) | w2; #else - return w | (bgetc_slow(f) << 8); + return w1 | (w2 << 8); #endif } @@ -136,7 +140,7 @@ u64 bget5_slow(struct fastbuf *f) return ((u64) h << 32) | l; } -void bputw_slow(struct fastbuf *f, word w) +void bputw_slow(struct fastbuf *f, uns w) { #ifdef CPU_BIG_ENDIAN bputc_slow(f, w >> 8); @@ -186,8 +190,9 @@ void bput5_slow(struct fastbuf *f, u64 o) #endif } -void bread_slow(struct fastbuf *f, void *b, uns l) +uns bread_slow(struct fastbuf *f, void *b, uns l, uns check) { + uns total = 0; while (l) { uns k = f->bstop - f->bptr; @@ -197,7 +202,7 @@ void bread_slow(struct fastbuf *f, void *b, uns l) f->refill(f); k = f->bstop - f->bptr; if (!k) - die("bread on %s: file exhausted", f->name); + break; } if (k > l) k = l; @@ -205,7 +210,11 @@ void bread_slow(struct fastbuf *f, void *b, uns l) f->bptr += k; b = (byte *)b + k; l -= k; + total += k; } + if (check && total && l) + die("breadb: short read"); + return total; } void bwrite_slow(struct fastbuf *f, void *b, uns l) @@ -235,11 +244,56 @@ bgets(struct fastbuf *f, byte *b, uns l) int k; k = bgetc(f); - if (k == EOF) + if (k < 0) + return NULL; + while (b < e) + { + if (k == '\n' || k < 0) + { + *b = 0; + return b; + } + *b++ = k; + k = bgetc(f); + } + die("%s: Line too long", f->name); +} + +int +bgets_nodie(struct fastbuf *f, byte *b, uns l) +{ + byte *start = b; + byte *e = b + l - 1; + int k; + + k = bgetc(f); + if (k < 0) + return 0; + while (b < e) + { + if (k == '\n' || k < 0) + { + *b++ = 0; + return b - start; + } + *b++ = k; + k = bgetc(f); + } + return -1; +} + +byte * +bgets0(struct fastbuf *f, byte *b, uns l) +{ + byte *e = b + l - 1; + int k; + + k = bgetc(f); + if (k < 0) return NULL; while (b < e) { - if (k == '\n' || k == EOF) + if (k <= 0) { *b = 0; return b; @@ -249,3 +303,42 @@ bgets(struct fastbuf *f, byte *b, uns l) } die("%s: Line too long", f->name); } + +void +bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l) +{ + while (l) + { + byte *fptr, *tptr; + uns favail, tavail, n; + + favail = bdirect_read_prepare(f, &fptr); + if (!favail) + { + if (l == ~0U) + return; + 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); + if (l != ~0U) + l -= n; + } +} + +int +bconfig(struct fastbuf *f, uns item, int value) +{ + return f->config ? f->config(f, item, value) : -1; +} + +void +brewind(struct fastbuf *f) +{ + bflush(f); + bsetpos(f, 0); +}