]> git.ozlabs.org Git - ccan/blob - ccan/ungraph/_info
ungraph: new module
[ccan] / ccan / ungraph / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * ungraph - extract a graph from an ASCII diagram.
7  *
8  * This code takes an ASCII diagram and converts it to a graph.
9  * The following things are assumed:
10  * 1. The input consists of \n-terminated lines
11  * 2. /-\|+ are used for edges.
12  * 3. <^>v are used for arrowheads.
13  * 4. + can be used to cross-over.
14  * 5. No arrowheads or both-ended arrowheads are shortcuts for "both ways".
15  * 6. Edges can turn with or without a +, by up to 90 degrees.
16  * 7. Edges must go from one node name to another.
17  * 8. Any other text is an edge label which must be next to an edge or
18  *    another label.
19  *
20  * License: BSD-MIT
21  * Example:
22  * // Convert an ASCII graph to Graphviz dot format
23  * #include <ccan/err/err.h>
24  * #include <ccan/grab_file/grab_file.h>
25  * #include <ccan/ungraph/ungraph.h>
26  *
27  * // Just return the name as our node.
28  * static void *add_node(const tal_t *ctx,
29  *                       const char *name,
30  *                       const char **errstr,
31  *                       void *unused)
32  * {
33  *         return (void *)name;
34  * }
35  *
36  * static const char *add_edge(const tal_t *ctx,
37  *                             void *source_node,
38  *                             void *dest_node,
39  *                             bool bidir,
40  *                             const char **labels,
41  *                             void *arg)
42  * {
43  *         printf("%s -> %s;\n", 
44  *                (char *)source_node, (char *)dest_node);
45  *         if (bidir)
46  *                 printf("%s -> %s;\n", 
47  *                        (char *)dest_node, (char *)source_node);
48  *         return NULL;
49  * }
50  *
51  * int main(int argc, char *argv[])
52  * {
53  *         const char *graph = grab_file(NULL, argv[1], NULL), *errmsg;
54  *         printf("digraph %s {\n", argv[1] ? argv[1] : "stdin");
55  *         errmsg = ungraph(NULL, graph, add_node, add_edge, NULL);
56  *         if (errmsg)
57  *                 errx(1, "%s", errmsg);
58  *         printf("}");
59  * }
60  *
61  * Author: Rusty Russell <rusty@rustcorp.com.au>
62  */
63 int main(int argc, char *argv[])
64 {
65         /* Expect exactly one argument */
66         if (argc != 2)
67                 return 1;
68
69         if (strcmp(argv[1], "depends") == 0) {
70                 printf("ccan/tal\n");
71                 printf("ccan/tal/str\n");
72                 printf("ccan/typesafe_cb\n");
73                 return 0;
74         }
75
76         return 1;
77 }