aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: be9133238493f403e907d9c38753656cf9c94f00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>

#include "pipeline.h"

#define UNUSED(_v) ((void)(_v))

PIPELINE_STAGE int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, void *data, size_t sz)
{
  return ptx_pipeline_ctx_next(ctx, data, sz);
}

PIPELINE_INIT 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 void debug_stage_cleanup(const char *name, void *puser)
{
  printf("Debug stage %s (cleanup): %p\n", name, puser);
}

PIPELINE_STAGE int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, 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));
  ptx_pipeline_free(pl);

  return 0;
}