]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - buffer_tree.c
para_write: Make check for wav header work with btr.
[paraslash.git] / buffer_tree.c
index 1046460d1d1c56dd424b0cc9a7a3e33c76857188..7b3355a28b98aa601431029c83063aac016d2d9d 100644 (file)
@@ -5,6 +5,7 @@
 #include "list.h"
 #include "string.h"
 #include "buffer_tree.h"
+#include "error.h"
 
 
 struct btr_buffer {
@@ -34,6 +35,8 @@ struct btr_node {
         * used by this btr node.
         */
        struct list_head input_queue;
+       btr_command_handler execute;
+       void *context;
 };
 
 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
@@ -44,12 +47,15 @@ struct btr_node {
 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
        list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
 
-struct btr_node *btr_new_node(char *name, struct btr_node *parent)
+struct btr_node *btr_new_node(char *name, struct btr_node *parent,
+               btr_command_handler handler, void *context)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
 
        btrn->name = para_strdup(name);
        btrn->parent = parent;
+       btrn->execute = handler;
+       btrn->context = context;
        if (parent)
                list_add_tail(&btrn->node, &parent->children);
        INIT_LIST_HEAD(&btrn->children);
@@ -79,6 +85,7 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
 {
        struct btr_buffer *btrb = br->btrb;
 
+       //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
        list_del(&br->node);
        free(br);
        btrb->refcount--;
@@ -88,14 +95,15 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
        }
 }
 
-static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
+static void add_btrb_to_children(struct btr_buffer *btrb,
+               struct btr_node *btrn, size_t consumed)
 {
        struct btr_node *ch;
 
        FOR_EACH_CHILD(ch, btrn) {
                struct btr_buffer_reference *br = para_malloc(sizeof(*br));
                br->btrb = btrb;
-               br->consumed = 0;
+               br->consumed = consumed;
                list_add_tail(&br->node, &ch->input_queue);
                btrb->refcount++;
        }
@@ -106,12 +114,12 @@ void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
        struct btr_buffer *btrb;
 
        btrb = new_btrb(buf, size);
-       add_btrb_to_children(btrb, btrn);
+       add_btrb_to_children(btrb, btrn, 0);
 }
 
 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
 {
-       add_btrb_to_children(br->btrb, btrn);
+       add_btrb_to_children(br->btrb, btrn, br->consumed);
        btr_drop_buffer_reference(br);
 }
 
@@ -124,7 +132,7 @@ void btr_pushdown(struct btr_node *btrn)
 }
 
 /* Return true if this node has no children. */
-bool btr_is_leaf_node(struct btr_node *btrn)
+bool btr_no_children(struct btr_node *btrn)
 {
        return list_empty(&btrn->children);
 }
@@ -204,11 +212,31 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        struct btr_buffer_reference *br;
        size_t size = 0;
 
-       FOR_EACH_BUFFER_REF(br, btrn)
+       FOR_EACH_BUFFER_REF(br, btrn) {
+               //PARA_CRIT_LOG("size: %zu\n", size);
                size += br_available_bytes(br);
+       }
        return size;
 }
 
+int btr_splice_out_node(struct btr_node *btrn)
+{
+       struct btr_node *ch;
+
+       if (!btrn)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (btr_get_input_queue_size(btrn) != 0)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
+       if (btrn->parent)
+               list_del(&btrn->node);
+       FOR_EACH_CHILD(ch, btrn)
+               ch->parent = btrn->parent;
+       free(btrn->name);
+       free(btrn);
+       return 1;
+}
+
 /**
  * Return the size of the largest input queue.
  *
@@ -225,3 +253,37 @@ size_t btr_bytes_pending(struct btr_node *btrn)
        }
        return max_size;
 }
+
+int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
+{
+       if (!btrn)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (!btrn->execute)
+               return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+       return btrn->execute(btrn, command, value_result);
+}
+
+int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
+{
+       int ret;
+
+       for (; btrn; btrn = btrn->parent) {
+               struct btr_node *parent = btrn->parent;
+               PARA_CRIT_LOG("parent: %p\n", parent);
+               if (!parent)
+                       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+               if (!parent->execute)
+                       continue;
+               ret = parent->execute(parent, command, value_result);
+               if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
+                       continue;
+               if (ret < 0)
+                       return ret;
+       }
+       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}
+
+void *btr_context(struct btr_node *btrn)
+{
+       return btrn->context;
+}