]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - buffer_tree.c
Add btr support for the amp filter.
[paraslash.git] / buffer_tree.c
index 1bc9529884dc16b779ee232022e6bb37ccee0e2d..1a6b6e60d3f977d2d99f600d7a872a1b95250312 100644 (file)
@@ -137,6 +137,17 @@ void btr_pushdown(struct btr_node *btrn)
                btr_pushdown_br(br, btrn);
 }
 
+int btr_pushdown_one(struct btr_node *btrn)
+{
+       struct btr_buffer_reference *br;
+
+       if (list_empty(&btrn->input_queue))
+               return 0;
+       br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
+       btr_pushdown_br(br, btrn);
+       return 1;
+}
+
 /* Return true if this node has no children. */
 bool btr_no_children(struct btr_node *btrn)
 {
@@ -166,6 +177,9 @@ size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
        return br_available_bytes(br);
 }
 
+/**
+ * \return zero if the input buffer queue is empty.
+ */
 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
 {
        struct btr_buffer_reference *br;
@@ -225,15 +239,13 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        return size;
 }
 
-int btr_splice_out_node(struct btr_node *btrn)
+void btr_splice_out_node(struct btr_node *btrn)
 {
        struct btr_node *ch, *tmp;
 
-       if (!btrn)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
-       if (btr_get_input_queue_size(btrn) != 0)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       assert(btrn);
        PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
+       btr_pushdown(btrn);
        if (btrn->parent)
                list_del(&btrn->node);
        FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
@@ -246,7 +258,6 @@ int btr_splice_out_node(struct btr_node *btrn)
        assert(list_empty(&btrn->children));
        free(btrn->name);
        free(btrn);
-       return 1;
 }
 
 /**
@@ -309,9 +320,9 @@ void *btr_context(struct btr_node *btrn)
  *
  * This is a quite expensive operation.
  *
- * \return The number of buffers that have been merged (zero, one or two).
+ * \return The number of buffers that have been available (zero, one or two).
  */
-int btr_merge(struct btr_node *btrn)
+static int merge_input(struct btr_node *btrn)
 {
        struct btr_buffer_reference *brs[2], *br;
        char *bufs[2], *buf;
@@ -351,3 +362,40 @@ int btr_merge(struct btr_node *btrn)
        para_list_add(&br->node, &btrn->input_queue);
        return 2;
 }
+
+void btr_merge(struct btr_node *btrn, size_t dest_size)
+{
+       for (;;) {
+               char *buf;
+               size_t len = btr_next_buffer(btrn, &buf);
+               if (len >= dest_size)
+                       return;
+               if (merge_input(btrn) < 2)
+                       return;
+       }
+}
+
+bool btr_eof(struct btr_node *btrn)
+{
+       char *buf;
+       size_t len = btr_next_buffer(btrn, &buf);
+
+       return (len == 0 && btr_no_parent(btrn));
+}
+
+void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
+{
+       struct btr_node *ch;
+       const char spaces[] = "                 ", *space = spaces + 16 - depth;
+
+       if (depth > 16)
+               return;
+       para_log(loglevel, "%s%s\n", space, btrn->name);
+       FOR_EACH_CHILD(ch, btrn)
+               log_tree_recursively(ch, loglevel, depth + 1);
+}
+
+void btr_log_tree(struct btr_node *btrn, int loglevel)
+{
+       return log_tree_recursively(btrn, loglevel, 0);
+}