]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fastbuf.c
Defined a GET_TAGGED_CHAR macro to read our internal representation
[libucw.git] / lib / fastbuf.c
index d2e6255e1426af0c1f00989365e85c89b99749d3..00fa51bcccd902538514cbbb069598008094b06f 100644 (file)
@@ -1,87 +1,22 @@
 /*
 /*
- *     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 <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)
 {
 
 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);
     }
 }
 
     }
 }
 
@@ -95,27 +30,23 @@ void bflush(struct fastbuf *f)
          f->pos = f->fdpos;
        }
       else                             /* Write data... */
          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 (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:
   switch (whence)
     {
     case SEEK_SET:
@@ -124,10 +55,7 @@ void bseek(struct fastbuf *f, uns pos, int whence)
       return bsetpos(f, btell(f) + pos);
     case SEEK_END:
       bflush(f);
       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);
       break;
     default:
       die("bseek: invalid whence=%d", whence);
@@ -138,7 +66,7 @@ int bgetc_slow(struct fastbuf *f)
 {
   if (f->bptr < f->bstop)
     return *f->bptr++;
 {
   if (f->bptr < f->bstop)
     return *f->bptr++;
-  if (!rdbuf(f))
+  if (!f->refill(f))
     return EOF;
   return *f->bptr++;
 }
     return EOF;
   return *f->bptr++;
 }
@@ -147,7 +75,7 @@ int bpeekc_slow(struct fastbuf *f)
 {
   if (f->bptr < f->bstop)
     return *f->bptr;
 {
   if (f->bptr < f->bstop)
     return *f->bptr;
-  if (!rdbuf(f))
+  if (!f->refill(f))
     return EOF;
   return *f->bptr;
 }
     return EOF;
   return *f->bptr;
 }
@@ -155,7 +83,7 @@ int bpeekc_slow(struct fastbuf *f)
 void bputc_slow(struct fastbuf *f, byte c)
 {
   if (f->bptr >= f->bufend)
 void bputc_slow(struct fastbuf *f, byte c)
 {
   if (f->bptr >= f->bufend)
-    wrbuf(f);
+    f->spout(f);
   *f->bptr++ = c;
 }
 
   *f->bptr++ = c;
 }
 
@@ -169,9 +97,9 @@ word bgetw_slow(struct fastbuf *f)
 #endif
 }
 
 #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);
 #ifdef CPU_BIG_ENDIAN
   l = (l << 8) | bgetc_slow(f);
   l = (l << 8) | bgetc_slow(f);
@@ -183,6 +111,32 @@ ulg bgetl_slow(struct fastbuf *f)
 #endif
 }
 
 #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
 void bputw_slow(struct fastbuf *f, word w)
 {
 #ifdef CPU_BIG_ENDIAN
@@ -194,7 +148,7 @@ void bputw_slow(struct fastbuf *f, word w)
 #endif
 }
 
 #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);
 {
 #ifdef CPU_BIG_ENDIAN
   bputc_slow(f, l >> 24);
@@ -209,18 +163,43 @@ void bputl_slow(struct fastbuf *f, ulg l)
 #endif
 }
 
 #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)
        {
   while (l)
     {
       uns k = f->bstop - f->bptr;
 
       if (!k)
        {
-         rdbuf(f);
+         f->refill(f);
          k = f->bstop - f->bptr;
          if (!k)
          k = f->bstop - f->bptr;
          if (!k)
-           die("bread on %s: file exhausted", f->name);
+           break;
        }
       if (k > l)
        k = l;
        }
       if (k > l)
        k = l;
@@ -228,7 +207,11 @@ void bread_slow(struct fastbuf *f, void *b, uns l)
       f->bptr += k;
       b = (byte *)b + k;
       l -= k;
       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)
 }
 
 void bwrite_slow(struct fastbuf *f, void *b, uns l)
@@ -239,7 +222,7 @@ void bwrite_slow(struct fastbuf *f, void *b, uns l)
 
       if (!k)
        {
 
       if (!k)
        {
-         wrbuf(f);
+         f->spout(f);
          k = f->bufend - f->bptr;
        }
       if (k > l)
          k = f->bufend - f->bptr;
        }
       if (k > l)
@@ -273,58 +256,52 @@ bgets(struct fastbuf *f, byte *b, uns l)
   die("%s: Line too long", f->name);
 }
 
   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->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;
+}