]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fastbuf.c
Implemented the URL regex replace mechanism.
[libucw.git] / lib / fastbuf.c
index 9756c978ce03592b1ba06a8e39f05690ca797248..4c0bf6db01530482dc40f2b4ff5e91e929ebfc31 100644 (file)
@@ -89,24 +89,20 @@ void bflush(struct fastbuf *f)
 {
   if (f->bptr != f->buffer)
     {                                  /* Have something to flush */
-      if (f->bstop > f->buffer)
-       {                               /* And it's read data */
+      if (f->bstop > f->buffer)                /* Read data? */
+       {
          f->bptr = f->bstop = f->buffer;
+         f->pos = f->fdpos;
        }
-      else
-       {                               /* Write data */
-         wrbuf(f);
-       }
+      else                             /* Write data... */
+       wrbuf(f);
     }
 }
 
 inline void bsetpos(struct fastbuf *f, uns 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);
-    }
+  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);
@@ -147,6 +143,15 @@ int bgetc_slow(struct fastbuf *f)
   return *f->bptr++;
 }
 
+int bpeekc_slow(struct fastbuf *f)
+{
+  if (f->bptr < f->bstop)
+    return *f->bptr;
+  if (!rdbuf(f))
+    return EOF;
+  return *f->bptr;
+}
+
 void bputc_slow(struct fastbuf *f, byte c)
 {
   if (f->bptr >= f->bufend)
@@ -246,6 +251,28 @@ void bwrite_slow(struct fastbuf *f, void *b, uns l)
     }
 }
 
+byte *                                 /* Non-standard */
+bgets(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 == '\n' || k == EOF)
+       {
+         *b = 0;
+         return b;
+       }
+      *b++ = k;
+      k = bgetc(f);
+    }
+  die("%s: Line too long", f->name);
+}
+
 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
 {
   uns rf = f->bstop - f->bptr;