]> git.ozlabs.org Git - ccan/blob - ccan/ungraph/ungraph.h
ungraph: new module
[ccan] / ccan / ungraph / ungraph.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_UNGRAPH_H
3 #define CCAN_UNGRAPH_H
4 #include <ccan/tal/tal.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
6
7 /**
8  * ungraph: extract a graph from an ASCII graph.
9  * @ctx: context for callbacks, and/or returned errstr.
10  * @str: a string containing a graph.
11  * @add_node: callback for a new node, returns node.
12  * @add_edge: callback for a new edge, with tal_count(labels).
13  * @arg: callback argument.
14  *
15  * On success, returns NULL.  On failure, returns some error message
16  * (allocated off @ctx, or returned from callbacks).
17  *
18  * If @add_node returns NULL, it must set @errstr. @add_edge
19  * returns the error message directly.
20  *
21  * @add_node and @add_edge can tal_steal the name/labels if they want,
22  * otherwise they will be freed.
23  */
24 const char *ungraph_(const tal_t *ctx,
25                      const char *str,
26                      void *(*add_node)(const tal_t *ctx,
27                                        const char *name,
28                                        const char **errstr,
29                                        void *arg),
30                      const char *(*add_edge)(const tal_t *ctx,
31                                              void *source_node,
32                                              void *dest_node,
33                                              bool bidir,
34                                              const char **labels,
35                                              void *arg),
36                      void *arg);
37
38 #define ungraph(ctx, str, add_node, add_edge, arg)                      \
39         ungraph_((ctx), (str),                                          \
40                  typesafe_cb_preargs(void *, void *,                    \
41                                      (add_node), (arg),                 \
42                                      const tal_t *,                     \
43                                      const char *,                      \
44                                      const char **errstr),              \
45                  typesafe_cb_preargs(const char *, void *,              \
46                                      (add_edge), (arg),                 \
47                                      const tal_t *,                     \
48                                      void *,                            \
49                                      void *,                            \
50                                      bool,                              \
51                                      const char **),                    \
52                  arg)
53 #endif /* CCAN_UNGRAPH_H */