]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fastbuf.c
As usually, stuff in lib/* is LGPL'ed.
[libucw.git] / lib / fastbuf.c
index 1895e9bd5d892893135f348dd1312a8ca377279c..013c1b0848d3453672c38828c24bc91a4a8d09c4 100644 (file)
@@ -2,6 +2,9 @@
  *     Sherlock Library -- Fast Buffered I/O
  *
  *     (c) 1997--2000 Martin Mares <mj@ucw.cz>
  *     Sherlock Library -- Fast Buffered I/O
  *
  *     (c) 1997--2000 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/lib.h"
@@ -80,20 +83,26 @@ int bpeekc_slow(struct fastbuf *f)
   return *f->bptr;
 }
 
   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;
 }
 
 {
   if (f->bptr >= f->bufend)
     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
 #ifdef CPU_BIG_ENDIAN
-  return (w << 8) | bgetc_slow(f);
+  return (w1 << 8) | w2;
 #else
 #else
-  return w | (bgetc_slow(f) << 8);
+  return w1 | (w2 << 8);
 #endif
 }
 
 #endif
 }
 
@@ -137,7 +146,7 @@ u64 bget5_slow(struct fastbuf *f)
   return ((u64) h << 32) | l;
 }
 
   return ((u64) h << 32) | l;
 }
 
-void bputw_slow(struct fastbuf *f, word w)
+void bputw_slow(struct fastbuf *f, uns w)
 {
 #ifdef CPU_BIG_ENDIAN
   bputc_slow(f, w >> 8);
 {
 #ifdef CPU_BIG_ENDIAN
   bputc_slow(f, w >> 8);
@@ -187,7 +196,7 @@ void bput5_slow(struct fastbuf *f, u64 o)
 #endif
 }
 
 #endif
 }
 
-uns 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 total = 0;
   while (l)
@@ -209,6 +218,8 @@ uns bread_slow(struct fastbuf *f, void *b, uns l)
       l -= k;
       total += k;
     }
       l -= k;
       total += k;
     }
+  if (check && total && l)
+    die("breadb: short read");
   return total;
 }
 
   return total;
 }
 
@@ -253,3 +264,58 @@ bgets(struct fastbuf *f, byte *b, uns l)
     }
   die("%s: Line too long", f->name);
 }
     }
   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);
+}
+
+int
+bdirect_read_prepare(struct fastbuf *f, byte **buf)
+{
+  int len;
+
+  if (f->bptr == f->bstop && !f->refill(f))
+    return EOF;
+  *buf = f->bptr;
+  len = f->bstop - f->bptr;
+  return len;
+}
+
+void
+bdirect_read_commit(struct fastbuf *f, byte *pos)
+{
+  f->bptr = pos;
+}
+
+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;
+}