]> mj.ucw.cz Git - libucw.git/commitdiff
Added another version of bgets() which doesn't die on too long lines and
authorMartin Mares <mj@ucw.cz>
Mon, 27 Jan 2003 13:49:10 +0000 (13:49 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 27 Jan 2003 13:49:10 +0000 (13:49 +0000)
reports an error instead.

I wrote it originally for new http.c, but the required http.c changes were
going to be too extensive, so I postponed the changes and this function
is currently unused, but probably worth saving for the future.

Also optimized the existing bgets functions a bit.

lib/fastbuf.c
lib/fastbuf.h

index b7be624b5645d5ac34e1e52ca83b1ce2a951be62..50b869a88ff39b086f5e09a952703f7ddb494a48 100644 (file)
@@ -244,11 +244,11 @@ bgets(struct fastbuf *f, byte *b, uns l)
   int k;
 
   k = bgetc(f);
-  if (k == EOF)
+  if (k < 0)
     return NULL;
   while (b < e)
     {
-      if (k == '\n' || k == EOF)
+      if (k == '\n' || k < 0)
        {
          *b = 0;
          return b;
@@ -259,6 +259,29 @@ bgets(struct fastbuf *f, byte *b, uns l)
   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)
 {
@@ -266,11 +289,11 @@ bgets0(struct fastbuf *f, byte *b, uns l)
   int k;
 
   k = bgetc(f);
-  if (k == EOF)
+  if (k < 0)
     return NULL;
   while (b < e)
     {
-      if (!k || k == EOF)
+      if (k <= 0)
        {
          *b = 0;
          return b;
index a370c45be8995155190b5eb61079812a685178e4..e08e16a100724173d5fd256fbe256b5ce1409c28 100644 (file)
@@ -268,6 +268,7 @@ static inline void bwrite(struct fastbuf *f, void *b, uns l)
 }
 
 byte *bgets(struct fastbuf *f, byte *b, uns l);        /* Non-std */
+int bgets_nodie(struct fastbuf *f, byte *b, uns l);
 byte *bgets0(struct fastbuf *f, byte *b, uns l);
 
 static inline void