append captured tokens to match_entry

This commit is contained in:
c9s 2014-05-16 19:12:01 +08:00
parent ebe4c008fb
commit 2a0e120a91
4 changed files with 33 additions and 10 deletions

View file

@ -16,6 +16,10 @@
#include "token.h" #include "token.h"
#define node_edge_pattern(node,i) node->edges[i]->pattern
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len
struct _edge; struct _edge;
struct _node; struct _node;
typedef struct _edge edge; typedef struct _edge edge;
@ -48,9 +52,9 @@ struct _edge {
}; };
typedef struct { typedef struct {
char ** vars; str_array * vars;
int vars_len;
char * path; // dispatched path char * path; // dispatched path
int path_len;
void * route_ptr; // route ptr void * route_ptr; // route ptr
} match_entry; } match_entry;
@ -96,5 +100,8 @@ void r3_edge_free(edge * edge);
match_entry * match_entry_create(char * path, int path_len);
void match_entry_free(match_entry * entry);
#endif /* !NODE_H */ #endif /* !NODE_H */

View file

@ -18,7 +18,6 @@ typedef struct _str_array {
str_array * str_array_create(int cap); str_array * str_array_create(int cap);
bool str_array_is_full(str_array * l); bool str_array_is_full(str_array * l);
bool str_array_resize(str_array *l, int new_cap); bool str_array_resize(str_array *l, int new_cap);

View file

@ -85,7 +85,6 @@ void r3_tree_append_edge(node *n, edge *e) {
n->edges[ n->r3_edge_len++ ] = e; n->edges[ n->r3_edge_len++ ] = e;
} }
edge * r3_node_find_edge(node * n, char * pat) { edge * r3_node_find_edge(node * n, char * pat) {
edge * e; edge * e;
for (int i = 0 ; i < n->r3_edge_len ; i++ ) { for (int i = 0 ; i < n->r3_edge_len ; i++ ) {
@ -181,6 +180,21 @@ void r3_tree_compile_patterns(node * n) {
} }
match_entry * match_entry_create(char * path, int path_len) {
match_entry * entry = malloc(sizeof(match_entry));
if(!entry)
return NULL;
entry->vars = str_array_create(3);
entry->path = path;
entry->path_len = path_len;
entry->route_ptr = NULL;
return entry;
}
void match_entry_free(match_entry * entry) {
str_array_free(entry->vars);
free(entry);
}
node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) { node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
@ -234,7 +248,8 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
e = n->edges[i - 1]; e = n->edges[i - 1];
if (entry && e->has_slug) { if (entry && e->has_slug) {
// entry-> // append captured token to entry
str_array_append(entry->vars , strndup(substring_start, substring_length));
} }
if (restlen == 0) { if (restlen == 0) {
return e->child; return e->child;
@ -258,8 +273,6 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
return NULL; return NULL;
} }
#define node_edge_pattern(node,i) node->edges[i]->pattern
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len
inline edge * r3_node_find_edge_str(node * n, char * str, int str_len) { inline edge * r3_node_find_edge_str(node * n, char * str, int str_len) {
int i = 0; int i = 0;

View file

@ -80,7 +80,6 @@ START_TEST (test_compile)
n = r3_tree_create(10); n = r3_tree_create(10);
match_entry * entry;
node *m; node *m;
edge *e ; edge *e ;
@ -115,24 +114,29 @@ START_TEST (test_compile)
printf( "%s\n", n->combined_pattern ); printf( "%s\n", n->combined_pattern );
*/ */
entry = calloc( sizeof(entry) , 1 ); match_entry * entry;
entry = match_entry_create( "foo" , strlen("/foo") );
m = r3_tree_match( n , "/foo", strlen("/foo"), entry); m = r3_tree_match( n , "/foo", strlen("/foo"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
entry = match_entry_create( "/zoo" , strlen("/zoo") );
m = r3_tree_match( n , "/zoo", strlen("/zoo"), entry); m = r3_tree_match( n , "/zoo", strlen("/zoo"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
entry = match_entry_create( "/bar" , strlen("/bar") );
m = r3_tree_match( n , "/bar", strlen("/bar"), entry); m = r3_tree_match( n , "/bar", strlen("/bar"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
entry = match_entry_create( "/xxx" , strlen("/xxx") );
m = r3_tree_match( n , "/xxx", strlen("/xxx"), entry); m = r3_tree_match( n , "/xxx", strlen("/xxx"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
entry = match_entry_create( "/foo/xxx" , strlen("/foo/xxx") );
m = r3_tree_match( n , "/foo/xxx", strlen("/foo/xxx"), entry); m = r3_tree_match( n , "/foo/xxx", strlen("/foo/xxx"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
entry = match_entry_create( "/some_id" , strlen("/some_id") );
m = r3_tree_match( n , "/some_id", strlen("/some_id"), entry); m = r3_tree_match( n , "/some_id", strlen("/some_id"), entry);
fail_if( NULL == m ); fail_if( NULL == m );
ck_assert_int_gt( m->endpoint , 0 ); // should not be an endpoint ck_assert_int_gt( m->endpoint , 0 ); // should not be an endpoint