/*
- * Sherlock Library -- Fast File Buffering
+ * Sherlock Library -- Fast Buffered I/O
*
- * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ * (c) 1997--2000 Martin Mares <mj@ucw.cz>
*/
+#include "lib/lib.h"
+#include "lib/fastbuf.h"
+
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "lib.h"
-#include "fastbuf.h"
-
-struct fastbuf *__bfdopen(int fd, uns buffer, byte *name)
-{
- struct fastbuf *b = xmalloc(sizeof(struct fastbuf));
-
- b->buflen = buffer;
- b->buffer = xmalloc(buffer);
- b->bptr = b->bstop = b->buffer;
- b->bufend = b->buffer + buffer;
- b->name = stralloc(name);
- b->pos = b->fdpos = 0;
- b->fd = fd;
- return b;
-}
-
-struct fastbuf *
-bopen(byte *name, uns mode, uns buffer)
-{
- int fd = open(name, mode, 0666);
-
- if (fd < 0)
- die("Unable to %s file %s: %m",
- (mode & O_CREAT) ? "create" : "open", name);
- return __bfdopen(fd, buffer, name);
-}
-
-struct fastbuf *
-bfdopen(int fd, uns buffer)
-{
- byte x[32];
-
- sprintf(x, "fd%d", fd);
- return __bfdopen(fd, buffer, x);
-}
void bclose(struct fastbuf *f)
{
- bflush(f);
- close(f->fd);
- free(f->name);
- free(f->buffer);
- free(f);
-}
-
-static int
-rdbuf(struct fastbuf *f)
-{
- int l = read(f->fd, f->buffer, f->buflen);
-
- if (l < 0)
- die("Error reading %s: %m", f->name);
- f->bptr = f->buffer;
- f->bstop = f->buffer + l;
- f->pos = f->fdpos;
- f->fdpos += l;
- return l;
-}
-
-static void
-wrbuf(struct fastbuf *f)
-{
- int l = f->bptr - f->buffer;
-
- if (l)
+ if (f)
{
- if (write(f->fd, f->buffer, l) != l)
- die("Error writing %s: %m", f->name);
- f->bptr = f->buffer;
- f->fdpos += l;
- f->pos = f->fdpos;
+ bflush(f);
+ f->close(f);
+ xfree(f);
}
}
f->pos = f->fdpos;
}
else /* Write data... */
- wrbuf(f);
+ f->spout(f);
}
}
-inline void bsetpos(struct fastbuf *f, uns pos)
+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);
else
{
bflush(f);
- if (f->fdpos != pos && lseek(f->fd, pos, SEEK_SET) < 0)
- die("lseek on %s: %m", f->name);
- f->fdpos = f->pos = pos;
+ f->seek(f, pos, SEEK_SET);
}
}
-void bseek(struct fastbuf *f, uns pos, int whence)
+void bseek(struct fastbuf *f, sh_off_t pos, int whence)
{
- int l;
-
switch (whence)
{
case SEEK_SET:
return bsetpos(f, btell(f) + pos);
case SEEK_END:
bflush(f);
- l = lseek(f->fd, pos, whence);
- if (l < 0)
- die("lseek on %s: %m", f->name);
- f->fdpos = f->pos = l;
+ f->seek(f, pos, SEEK_END);
break;
default:
die("bseek: invalid whence=%d", whence);
{
if (f->bptr < f->bstop)
return *f->bptr++;
- if (!rdbuf(f))
+ if (!f->refill(f))
return EOF;
return *f->bptr++;
}
{
if (f->bptr < f->bstop)
return *f->bptr;
- if (!rdbuf(f))
+ if (!f->refill(f))
return EOF;
return *f->bptr;
}
-void bputc_slow(struct fastbuf *f, byte c)
+void bputc_slow(struct fastbuf *f, uns c)
{
if (f->bptr >= f->bufend)
- wrbuf(f);
+ f->spout(f);
*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
}
-ulg bgetl_slow(struct fastbuf *f)
+u32 bgetl_slow(struct fastbuf *f)
{
- ulg l = bgetc_slow(f);
+ u32 l = bgetc_slow(f);
#ifdef CPU_BIG_ENDIAN
l = (l << 8) | bgetc_slow(f);
l = (l << 8) | bgetc_slow(f);
#endif
}
-void bputw_slow(struct fastbuf *f, word w)
+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);
#endif
}
-void bputl_slow(struct fastbuf *f, ulg l)
+void bputl_slow(struct fastbuf *f, u32 l)
{
#ifdef CPU_BIG_ENDIAN
bputc_slow(f, l >> 24);
#endif
}
-void bread_slow(struct fastbuf *f, void *b, uns l)
+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;
while (l)
{
uns k = f->bstop - f->bptr;
if (!k)
{
- rdbuf(f);
+ 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)
if (!k)
{
- wrbuf(f);
+ f->spout(f);
k = f->bufend - f->bptr;
}
if (k > l)
die("%s: Line too long", f->name);
}
-void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
+byte *
+bgets0(struct fastbuf *f, byte *b, uns l)
{
- uns rf = f->bstop - f->bptr;
+ byte *e = b + l - 1;
+ int k;
- if (!l)
- return;
- if (rf)
- {
- uns k = (rf <= l) ? rf : l;
- bwrite(t, f->bptr, k);
- f->bptr += k;
- l -= k;
- }
- while (l >= t->buflen)
- {
- wrbuf(t);
- if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen)
- die("bbcopy: %s exhausted", f->name);
- f->pos = f->fdpos;
- f->fdpos += t->buflen;
- f->bstop = f->bptr = f->buffer;
- t->bptr = t->bufend;
- l -= t->buflen;
- }
- while (l)
+ k = bgetc(f);
+ if (k == EOF)
+ return NULL;
+ while (b < e)
{
- uns k = t->bufend - t->bptr;
-
- if (!k)
+ if (!k || k == EOF)
{
- wrbuf(t);
- k = t->bufend - t->bptr;
+ *b = 0;
+ return b;
}
- if (k > l)
- k = l;
- bread(f, t->bptr, k);
- t->bptr += k;
- l -= k;
+ *b++ = k;
+ k = bgetc(f);
}
+ die("%s: Line too long", f->name);
}
-#ifdef TEST
-
-int main(int argc, char **argv)
+int
+bdirect_read(struct fastbuf *f, byte **buf)
{
- struct fastbuf *f, *t;
- int c;
+ int len;
- f = bopen("/etc/profile", O_RDONLY, 16);
- t = bfdopen(1, 13);
- bbcopy(f, t, 100);
- bclose(f);
- bclose(t);
+ if (f->bptr == f->bstop && !f->refill(f))
+ return EOF;
+ *buf = f->bptr;
+ len = f->bstop - f->bptr;
+ f->bptr += len;
+ return len;
}
-#endif
+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;
+}