/*
- * Sherlock Library -- Fast Buffered I/O
+ * UCW Library -- Fast Buffered I/O
*
- * (c) 1997--2000 Martin Mares <mj@ucw.cz>
+ * (c) 1997--2007 Martin Mares <mj@ucw.cz>
+ *
+ * This software may be freely distributed and used according to the terms
+ * of the GNU Lesser General Public License.
*/
+#include "lib/lib.h"
+#include "lib/fastbuf.h"
+
#include <stdio.h>
#include <stdlib.h>
-#include "lib.h"
-#include "fastbuf.h"
-
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);
- f->seek(f, pos, SEEK_SET);
+ if (!f->seek || !f->seek(f, pos, SEEK_SET))
+ die("bsetpos: stream not seekable");
}
}
return bsetpos(f, btell(f) + pos);
case SEEK_END:
bflush(f);
- f->seek(f, pos, SEEK_END);
+ if (!f->seek || !f->seek(f, pos, SEEK_END))
+ die("bseek: stream not seekable");
break;
default:
die("bseek: invalid whence=%d", whence);
if (f->bptr < f->bstop)
return *f->bptr++;
if (!f->refill(f))
- return EOF;
+ return -1;
return *f->bptr++;
}
if (f->bptr < f->bstop)
return *f->bptr;
if (!f->refill(f))
- return EOF;
+ return -1;
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)
-{
- word w = bgetc_slow(f);
-#ifdef CPU_BIG_ENDIAN
- return (w << 8) | bgetc_slow(f);
-#else
- return w | (bgetc_slow(f) << 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, word 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
-}
-
-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;
f->refill(f);
k = f->bstop - f->bptr;
if (!k)
- die("bread on %s: file exhausted", f->name);
+ break;
}
if (k > l)
k = 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)
}
}
-byte * /* Non-standard */
-bgets(struct fastbuf *f, byte *b, uns l)
+void
+bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
{
- byte *e = b + l - 1;
- int k;
-
- k = bgetc(f);
- if (k == EOF)
- return NULL;
- while (b < e)
+ while (l)
{
- if (k == '\n' || k == EOF)
+ byte *fptr, *tptr;
+ uns favail, tavail, n;
+
+ favail = bdirect_read_prepare(f, &fptr);
+ if (!favail)
{
- *b = 0;
- return b;
+ if (l == ~0U)
+ return;
+ die("bbcopy: source exhausted");
}
- *b++ = k;
- k = bgetc(f);
+ 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;
}
- die("%s: Line too long", f->name);
+}
+
+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);
+ bflush(f);
+ if (!f->seek(f, pos, SEEK_END))
+ return -1;
+ sh_off_t len = btell(f);
+ bsetpos(f, pos);
+ return len;
}