/*
- * Sherlock Library -- Fast Buffered I/O
+ * UCW Library -- Fast Buffered I/O
*
- * (c) 1997--2004 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>
void bclose(struct fastbuf *f)
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;
}
*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;
}
}
-byte * /* Non-standard */
-bgets(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 < 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 <= 0)
- {
- *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)
{
bsetpos(f, 0);
}
-void
-bskip(struct fastbuf *f, uns len)
+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;
}