From 132bd203619657f1918e2f684d5e64882f421a94 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Sun, 25 May 2014 10:29:31 +0300 Subject: [PATCH] Added the r3_dump_tree_str function. --- README.md | 4 ++++ include/r3.h | 5 ++++- include/r3_str.h | 3 +++ src/node.c | 34 ++++++++++++++++++++++++++++++++++ src/str.c | 9 +++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04323bc..f24c265 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ r3_tree_compile(n); // dump the compiled tree r3_tree_dump(n, 0); +// dump the compiled tree to a string +sds tree_dump = sdsempty(); +tree_dump = r3_tree_dump_str(n, 0, tree_dump); + // match a route node *matched_node = r3_tree_match(n, "/foo/bar", strlen("/foo/bar"), NULL); if (matched_node) { diff --git a/include/r3.h b/include/r3.h index 1d8508d..fae4a0d 100644 --- a/include/r3.h +++ b/include/r3.h @@ -15,6 +15,7 @@ #include "r3_define.h" #include "str_array.h" +#include "sds.h" #define node_edge_pattern(node,i) node->edges[i]->pattern @@ -77,7 +78,7 @@ typedef struct { void * data; // route ptr - char * host; // the request host + char * host; // the request host int host_len; char * remote_addr; @@ -129,6 +130,8 @@ node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route void r3_tree_dump(node * n, int level); +sds r3_tree_dump_str(node *n, int level, sds output); + int r3_tree_render_file(node * tree, char * format, char * filename); int r3_tree_render_dot(node * tree); diff --git a/include/r3_str.h b/include/r3_str.h index f83bfcf..5ec8604 100644 --- a/include/r3_str.h +++ b/include/r3_str.h @@ -8,6 +8,7 @@ #define STR_H #include "r3.h" +#include "sds.h" #include "config.h" int slug_count(char * p, int len); @@ -28,6 +29,8 @@ void str_repeat(char *s, char *c, int len); void print_indent(int level); +sds concat_indent(sds s, int level); + #ifndef HAVE_STRDUP char *strdup(const char *s); #endif diff --git a/src/node.c b/src/node.c index 23dcddc..1d4631f 100644 --- a/src/node.c +++ b/src/node.c @@ -11,6 +11,7 @@ #include "r3.h" #include "r3_str.h" #include "str_array.h" +#include "sds.h" #include "zmalloc.h" // String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm @@ -650,6 +651,39 @@ void r3_tree_dump(node * n, int level) { } } +sds r3_tree_dump_str(node *n, int level, sds output) { + output = concat_indent(output, level); + + output = sdscatprintf(output, "(o)"); + + if ( n->combined_pattern ) { + output = sdscatprintf(output, " regexp:%s", n->combined_pattern); + } + + output = sdscatprintf(output, " endpoint:%d", n->endpoint); + + if (n->data) { + output = sdscatprintf(output, " data:%p", n->data); + } + output = sdscatprintf(output, "\n"); + + for ( int i = 0 ; i < n->edge_len ; i++ ) { + edge * e = n->edges[i]; + print_indent(level + 1); + output = sdscatprintf(output, "|-\"%s\"", e->pattern); + + if (e->opcode ) { + output = sdscatprintf(output, " opcode:%d", e->opcode); + } + + if ( e->child ) { + output = sdscatprintf(output, "\n"); + r3_tree_dump_str( e->child, level + 1, output); + } + output = sdscatprintf(output, "\n"); + } +} + /** * return 0 == equal diff --git a/src/str.c b/src/str.c index c794761..b2dd034 100644 --- a/src/str.c +++ b/src/str.c @@ -219,6 +219,15 @@ void print_indent(int level) { } } +sds concat_indent(sds s, int level) { + int len = level * 2; + while(len--) { + s = sdscatprintf(s, " "); + } + + return s; +} + #ifndef HAVE_STRDUP char *zstrdup(const char *s) { char *out;