summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..83ed688
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,125 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdarg.h>
+
+#include "pipeline.h"
+
+#define UNUSED(_v) ((void)(_v))
+
+PIPELINE_STAGE static int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz)
+{
+ return ptx_pipeline_ctx_next(ctx, data, sz);
+}
+
+PIPELINE_INIT static int debug_stage_init(const char *name, void **ppuser, va_list init_args)
+{
+ static const char *const messages[] = { "no continue :(", "continue :)" };
+
+ UNUSED(name);
+
+ int config = !!va_arg(init_args, int);
+ *ppuser = (void *)(uintptr_t)config;
+
+ printf("Debug stage %s (init): %s\n", name, messages[config]);
+
+ return 0;
+}
+
+PIPELINE_CLEANUP static void debug_stage_cleanup(const char *name, void *puser)
+{
+ printf("Debug stage %s (cleanup): %p\n", name, puser);
+}
+
+PIPELINE_STAGE static int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz)
+{
+ bool cont = !!(uintptr_t)(*ptx_pipeline_ctx_get_user(ctx));
+ printf("Debug stage %s:\n Data: %p\n Size: %zu\n", ptx_pipeline_ctx_get_name(ctx), data, sz);
+ return cont ? ptx_pipeline_ctx_next(ctx, data, sz) : 0;
+}
+
+static const struct ptx_pipeline_stage_funcs identity_stage = {
+ .init = NULL,
+ .handler = &identity_stage_handle,
+ .cleanup = NULL
+};
+
+static const struct ptx_pipeline_stage_funcs debug_stage = {
+ .init = &debug_stage_init,
+ .handler = &debug_stage_handle,
+ .cleanup = &debug_stage_cleanup
+};
+
+/* mockup:
+ * Pipeline signature: D -> D*1
+ * int simple_1to1_stage(struct pipeline_ctx *ctx, void *data, size_t len) {
+ * void *out = ... transform data ...;
+ * size_t out_len = ^^^;
+ * return ptx_pipeline_next(ctx, out, out_len);
+ * }
+ *
+ * Pipeline signature: D -> D*(0,n)
+ * int complex_1tomany_stage(struct pipeline_ctx *ctx, void *data, size_t len) {
+ * int err;
+ * for (void *out; has_data(data, len); out = next_data(data, &len)) {
+ * if (!out) {
+ * return -1; ... next_data failed somehow ...
+ * }
+ *
+ * if ((err = ptx_pipeline_next(ctx, out, out_len)) < 0) {
+ * return err;
+ * }
+ * }
+ * return 0;
+ * }
+ *
+ * Pipeline signature: D -> D*1
+ * int stateful_stage(struct pipeline_ctx *ctx, void *data, size_t len) {
+ * my_stage_t *stg = ctx->me;
+ * stg->stat_in += len;
+ * return ptx_pipeline_next(ctx, data, len);
+ * }
+ *
+ * Pipeline signature: D -> void (i.e., never calls next)
+ * int terminal_stage(struct pipeline_ctx *ctx, void *data, size_t len) {
+ * my_handler_t *handler = ctx->me;
+ * handler->do_handle(data, len);
+ * return 0; ... return values should indicate pipeline (i.e., parsing) errors, not application errors ...
+ * }
+ */
+
+int main(void) {
+ ptx_pipeline_t *pl = ptx_pipeline_new(1);
+ if (!pl) {
+ fputs("Allocation failure: pipeline\n", stderr);
+ return 1;
+ }
+
+ if (ptx_pipeline_add_before(pl, &identity_stage, "identity1", NULL) < 0) {
+ fputs("Failed to add identity1 to pipeline\n", stderr);
+ return 1;
+ }
+
+ if (ptx_pipeline_add_after(pl, &debug_stage, "debug1", "identity1", true) < 0) {
+ fputs("Failed to add debug1 to pipeline\n", stderr);
+ return 1;
+ }
+
+ if (ptx_pipeline_add_after(pl, &debug_stage, "debug2", "debug1", false) < 0) {
+ fputs("Failed to add debug2 to pipeline\n", stderr);
+ return 1;
+ }
+
+ printf("Handle returned: %d\n", ptx_pipeline_handle(pl, "sussy", 10));
+
+ if (ptx_pipeline_remove_stage(pl, "debug1") < 1) {
+ fputs("Failed to remove debug1 from pipeline\n", stderr);
+ return 1;
+ }
+
+ printf("Handle returned: %d\n", ptx_pipeline_handle(pl, "sussy2", 20));
+
+ ptx_pipeline_free(pl);
+
+ return 0;
+}