X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffastbuf.c;h=8df905487ed42ff9bd371aaa222b8625a3fc21c7;hb=23552983305e667eeb772406d1745ab7b865855e;hp=717372fdfcfb42f797b1d2199d96d87f86467216;hpb=168c1f2ddcb8ce07248b4079b6398afe8c2d792a;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index 717372fd..8df90548 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -4,17 +4,20 @@ * (c) 1997--2000 Martin Mares */ +#include "lib/lib.h" +#include "lib/fastbuf.h" + #include #include -#include "lib.h" -#include "fastbuf.h" - void bclose(struct fastbuf *f) { - bflush(f); - f->close(f); - free(f); + if (f) + { + bflush(f); + f->close(f); + xfree(f); + } } void bflush(struct fastbuf *f) @@ -44,8 +47,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: @@ -86,13 +87,19 @@ void bputc_slow(struct fastbuf *f, byte c) *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 } @@ -186,8 +193,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 +205,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 +213,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) @@ -249,3 +261,53 @@ bgets(struct fastbuf *f, byte *b, uns l) } die("%s: Line too long", f->name); } + +byte * +bgets0(struct fastbuf *f, byte *b, uns l) +{ + byte *e = b + l - 1; + int k; + + k = bgetc(f); + if (k == EOF) + return NULL; + while (b < e) + { + if (!k || k == EOF) + { + *b = 0; + return b; + } + *b++ = k; + k = bgetc(f); + } + die("%s: Line too long", f->name); +} + +int +bdirect_read(struct fastbuf *f, byte **buf) +{ + int len; + + if (f->bptr == f->bstop && !f->refill(f)) + return EOF; + *buf = f->bptr; + len = f->bstop - f->bptr; + f->bptr += len; + return len; +} + +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) +{ + f->bptr = pos; +}