X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Ffastbuf.c;h=980d53aa619ceb0509d64987ef57765dc8f42199;hb=7442c1c86d2210ca3ef1538115f4a0423cd2f046;hp=d976730d2b7affb829a2b4322e0f7ca57b2abb54;hpb=2a5686e35c9d6be6cefe22d49cfff6d23ec2e842;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index d976730d..980d53aa 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--2006 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -84,112 +84,6 @@ void bputc_slow(struct fastbuf *f, uns c) *f->bptr++ = c; } -int bgetw_slow(struct fastbuf *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 (w1 << 8) | w2; -#else - return w1 | (w2 << 8); -#endif -} - -u32 bgetl_slow(struct fastbuf *f) -{ - u32 l = bgetc_slow(f); -#ifdef CPU_BIG_ENDIAN - l = (l << 8) | bgetc_slow(f); - l = (l << 8) | bgetc_slow(f); - return (l << 8) | bgetc_slow(f); -#else - l = (bgetc_slow(f) << 8) | l; - l = (bgetc_slow(f) << 16) | l; - return (bgetc_slow(f) << 24) | l; -#endif -} - -u64 bgetq_slow(struct fastbuf *f) -{ - u32 l, h; -#ifdef CPU_BIG_ENDIAN - h = bgetl_slow(f); - l = bgetl_slow(f); -#else - l = bgetl_slow(f); - h = bgetl_slow(f); -#endif - return ((u64) h << 32) | l; -} - -u64 bget5_slow(struct fastbuf *f) -{ - u32 l, h; -#ifdef CPU_BIG_ENDIAN - h = bgetc_slow(f); - l = bgetl_slow(f); -#else - l = bgetl_slow(f); - h = bgetc_slow(f); -#endif - return ((u64) h << 32) | l; -} - -void bputw_slow(struct fastbuf *f, uns w) -{ -#ifdef CPU_BIG_ENDIAN - bputc_slow(f, w >> 8); - bputc_slow(f, w); -#else - bputc_slow(f, w); - bputc_slow(f, w >> 8); -#endif -} - -void bputl_slow(struct fastbuf *f, u32 l) -{ -#ifdef CPU_BIG_ENDIAN - bputc_slow(f, l >> 24); - bputc_slow(f, l >> 16); - bputc_slow(f, l >> 8); - bputc_slow(f, l); -#else - bputc_slow(f, l); - bputc_slow(f, l >> 8); - bputc_slow(f, l >> 16); - bputc_slow(f, l >> 24); -#endif -} - -void bputq_slow(struct fastbuf *f, u64 q) -{ -#ifdef CPU_BIG_ENDIAN - bputl_slow(f, q >> 32); - bputl_slow(f, q); -#else - bputl_slow(f, q); - bputl_slow(f, q >> 32); -#endif -} - -void bput5_slow(struct fastbuf *f, u64 o) -{ - u32 hi = o >> 32; - u32 low = o; -#ifdef CPU_BIG_ENDIAN - bputc_slow(f, hi); - bputl_slow(f, low); -#else - bputl_slow(f, low); - bputc_slow(f, hi); -#endif -} - uns bread_slow(struct fastbuf *f, void *b, uns l, uns check) { uns total = 0; @@ -237,50 +131,6 @@ void bwrite_slow(struct fastbuf *f, void *b, uns l) } } -byte * /* Non-standard */ -bgets(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 == '\n' || k == EOF) - { - *b = 0; - return b; - } - *b++ = k; - k = bgetc(f); - } - 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); -} - void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l) { @@ -290,14 +140,60 @@ 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; + } +} + +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); +} + +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; }