]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/ff-string.c
Redblack: Added search_up
[libucw.git] / ucw / ff-string.c
index 5b4b8b9c61316ed4a449c7ee961edd43f397494c..d3aff87ce9ee7f1c792ce00e98d41af1e56d6e35 100644 (file)
@@ -8,23 +8,23 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/fastbuf.h"
-#include "ucw/mempool.h"
-#include "ucw/bbuf.h"
+#include <ucw/lib.h>
+#include <ucw/fastbuf.h>
+#include <ucw/mempool.h>
+#include <ucw/bbuf.h>
 
 char *                                 /* Non-standard */
-bgets(struct fastbuf *f, char *b, uns l)
+bgets(struct fastbuf *f, char *b, uint l)
 {
   ASSERT(l);
   byte *src;
-  uns src_len = bdirect_read_prepare(f, &src);
+  uint src_len = bdirect_read_prepare(f, &src);
   if (!src_len)
     return NULL;
   do
     {
-      uns cnt = MIN(l, src_len);
-      for (uns i = cnt; i--;)
+      uint cnt = MIN(l, src_len);
+      for (uint i = cnt; i--;)
         {
          byte v = *src++;
          if (v == '\n')
@@ -35,7 +35,7 @@ bgets(struct fastbuf *f, char *b, uns l)
          *b++ = v;
        }
       if (unlikely(cnt == l))
-        die("%s: Line too long", f->name);
+        bthrow(f, "toolong", "%s: Line too long", f->name);
       l -= cnt;
       bdirect_read_commit(f, src);
       src_len = bdirect_read_prepare(f, &src);
@@ -47,17 +47,17 @@ exit:
 }
 
 int
-bgets_nodie(struct fastbuf *f, char *b, uns l)
+bgets_nodie(struct fastbuf *f, char *b, uint l)
 {
   ASSERT(l);
   byte *src, *start = b;
-  uns src_len = bdirect_read_prepare(f, &src);
+  uint src_len = bdirect_read_prepare(f, &src);
   if (!src_len)
     return 0;
   do
     {
-      uns cnt = MIN(l, src_len);
-      for (uns i = cnt; i--;)
+      uint cnt = MIN(l, src_len);
+      for (uint i = cnt; i--;)
         {
          byte v = *src++;
          if (v == '\n')
@@ -79,21 +79,21 @@ exit:
   return b - (char *)start;
 }
 
-uns
-bgets_bb(struct fastbuf *f, struct bb_t *bb, uns limit)
+uint
+bgets_bb(struct fastbuf *f, struct bb_t *bb, uint limit)
 {
   ASSERT(limit);
   byte *src;
-  uns src_len = bdirect_read_prepare(f, &src);
+  uint src_len = bdirect_read_prepare(f, &src);
   if (!src_len)
     return 0;
   bb_grow(bb, 1);
   byte *buf = bb->ptr;
-  uns len = 0, buf_len = MIN(bb->len, limit);
+  uint len = 0, buf_len = MIN(bb->len, limit);
   do
     {
-      uns cnt = MIN(src_len, buf_len);
-      for (uns i = cnt; i--;)
+      uint cnt = MIN(src_len, buf_len);
+      for (uint i = cnt; i--;)
         {
          byte v = *src++;
          if (v == '\n')
@@ -114,7 +114,7 @@ bgets_bb(struct fastbuf *f, struct bb_t *bb, uns limit)
       if (cnt == buf_len)
         {
          if (unlikely(len == limit))
-            die("%s: Line too long", f->name);
+            bthrow(f, "toolong", "%s: Line too long", f->name);
          bb_do_grow(bb, len + 1);
          buf = bb->ptr + len;
          buf_len = MIN(bb->len, limit) - len;
@@ -132,7 +132,7 @@ char *
 bgets_mp(struct fastbuf *f, struct mempool *mp)
 {
   byte *src;
-  uns src_len = bdirect_read_prepare(f, &src);
+  uint src_len = bdirect_read_prepare(f, &src);
   if (!src_len)
     return NULL;
 #define BLOCK_SIZE (4096 - sizeof(void *))
@@ -140,13 +140,13 @@ bgets_mp(struct fastbuf *f, struct mempool *mp)
     struct block *prev;
     byte data[BLOCK_SIZE];
   } *blocks = NULL;
-  uns sum = 0, buf_len = BLOCK_SIZE, cnt;
+  uint sum = 0, buf_len = BLOCK_SIZE, cnt;
   struct block first_block, *new_block = &first_block;
   byte *buf = new_block->data;
   do
     {
       cnt = MIN(src_len, buf_len);
-      for (uns i = cnt; i--;)
+      for (uint i = cnt; i--;)
         {
          byte v = *src++;
          if (v == '\n')
@@ -176,7 +176,7 @@ bgets_mp(struct fastbuf *f, struct mempool *mp)
     }
   while (src_len);
 exit: ;
-  uns len = buf - new_block->data;
+  uint len = buf - new_block->data;
   byte *result = mp_alloc(mp, sum + len + 1) + sum;
   result[len] = 0;
   memcpy(result, new_block->data, len);
@@ -190,81 +190,18 @@ exit: ;
 #undef BLOCK_SIZE
 }
 
-void
-bgets_stk_init(struct bgets_stk_struct *s)
-{
-  s->src_len = bdirect_read_prepare(s->f, &s->src);
-  if (!s->src_len)
-    {
-      s->cur_buf = NULL;
-      s->cur_len = 0;
-    }
-  else
-    {
-      s->old_buf = NULL;
-      s->cur_len = 256;
-    }
-}
-
-void
-bgets_stk_step(struct bgets_stk_struct *s)
-{
-  byte *buf = s->cur_buf;
-  uns buf_len = s->cur_len;
-  if (s->old_buf)
-    {
-      memcpy( s->cur_buf, s->old_buf, s->old_len);
-      buf += s->old_len;
-      buf_len -= s->old_len;
-    }
-  do
-    {
-      uns cnt = MIN(s->src_len, buf_len);
-      for (uns i = cnt; i--;)
-        {
-         byte v = *s->src++;
-         if (v == '\n')
-           {
-              bdirect_read_commit(s->f, s->src);
-             goto exit;
-           }
-         *buf++ = v;
-       }
-      if (cnt == s->src_len)
-        {
-         bdirect_read_commit(s->f, s->src);
-         s->src_len = bdirect_read_prepare(s->f, &s->src);
-       }
-      else
-       s->src_len -= cnt;
-      if (cnt == buf_len)
-        {
-         s->old_len = s->cur_len;
-         s->old_buf = s->cur_buf;
-         s->cur_len *= 2;
-         return;
-       }
-      else
-       buf_len -= cnt;
-    }
-  while (s->src_len);
-exit:
-  *buf = 0;
-  s->cur_len = 0;
-}
-
 char *
-bgets0(struct fastbuf *f, char *b, uns l)
+bgets0(struct fastbuf *f, char *b, uint l)
 {
   ASSERT(l);
   byte *src;
-  uns src_len = bdirect_read_prepare(f, &src);
+  uint src_len = bdirect_read_prepare(f, &src);
   if (!src_len)
     return NULL;
   do
     {
-      uns cnt = MIN(l, src_len);
-      for (uns i = cnt; i--;)
+      uint cnt = MIN(l, src_len);
+      for (uint i = cnt; i--;)
         {
          *b = *src++;
          if (!*b)
@@ -275,7 +212,7 @@ bgets0(struct fastbuf *f, char *b, uns l)
          b++;
        }
       if (unlikely(cnt == l))
-        die("%s: Line too long", f->name);
+        bthrow(f, "toolong", "%s: Line too long", f->name);
       l -= cnt;
       bdirect_read_commit(f, src);
       src_len = bdirect_read_prepare(f, &src);