Consolidate decoder code by introducing prepare_filter_node().
[paraslash.git] / filter_common.c
index 7c71ff39243a7a63f7026c11726418377790c87e..bc5539d0b842a7f81a735a53918c565d4c785d02 100644 (file)
@@ -9,12 +9,14 @@
 #include <regex.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <stdbool.h>
 
 #include "para.h"
 #include "list.h"
 #include "sched.h"
 #include "fd.h"
 #include "ggo.h"
+#include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
 #include "string.h"
@@ -273,3 +275,23 @@ void print_filter_helps(int detailed)
        }
 
 }
+
+/** 640K ought to be enough for everybody ;) */
+#define FILTER_MAX_PENDING (640 * 1024)
+
+int prepare_filter_node(struct btr_node *btrn, size_t min_len)
+{
+       size_t iqs;
+
+       if (btr_eof(btrn))
+               return -E_FC_EOF;
+       if (btr_bytes_pending(btrn) > FILTER_MAX_PENDING)
+               return 0;
+       iqs = btr_get_input_queue_size(btrn);
+       if (iqs < min_len && !btr_no_parent(btrn))
+               return 0;
+       assert(iqs != 0);
+       /* avoid "buffer too small" errors from the decoder */
+       btr_merge_to(btrn, min_len);
+       return 1;
+}