X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Ffastbuf.c;h=f83b40210132db4a87258d9d77f9078f0465b6bd;hb=be8d99292164b5708998ce8d46424574fc866f80;hp=115719422b8844875edefb015f61170297c0494a;hpb=e38455f76749ff06f64649871ed5e33302389948;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index 11571942..f83b4021 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -1,7 +1,7 @@ /* - * Sherlock Library -- Fast Buffered I/O + * UCW Library -- Fast Buffered I/O * - * (c) 1997--2000 Martin Mares + * (c) 1997--2005 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -244,11 +244,11 @@ 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 == EOF) + if (k == '\n' || k < 0) { *b = 0; return b; @@ -259,6 +259,29 @@ bgets(struct fastbuf *f, byte *b, uns l) 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) { @@ -266,11 +289,11 @@ bgets0(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 || k == EOF) + if (k <= 0) { *b = 0; return b; @@ -290,15 +313,20 @@ bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l) uns favail, tavail, n; favail = bdirect_read_prepare(f, &fptr); - if (favail == (uns)EOF) - die("bbcopy: source exhausted"); + 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); - l -= n; + if (l != ~0U) + l -= n; } } @@ -307,3 +335,38 @@ 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); +} + +int +bskip_slow(struct fastbuf *f, uns len) +{ + while (len) + { + byte *buf; + uns l = bdirect_read_prepare(f, &buf); + if (!l) + return 0; + l = MIN(l, len); + bdirect_read_commit(f, buf+l); + len -= l; + } + return 1; +} + +sh_off_t +bfilesize(struct fastbuf *f) +{ + if (!f) + return 0; + sh_off_t pos = btell(f); + bseek(f, 0, SEEK_END); + sh_off_t len = btell(f); + bsetpos(f, pos); + return len; +}