Add r3_ prefix to route struct related functions

This commit is contained in:
c9s 2014-05-19 10:39:03 +08:00
parent 2ac6f87422
commit 401443f7ac
6 changed files with 31 additions and 20 deletions

View file

@ -26,3 +26,6 @@ This reduce the interface complexity, e.g.,
6. A path that is inserted by `r3_tree_insert_route` can only be matched by `r3_tree_match_route`. 6. A path that is inserted by `r3_tree_insert_route` can only be matched by `r3_tree_match_route`.
7. Add `r3_` prefix to `route` related methods.

View file

@ -67,6 +67,9 @@ the catched variables will be pushed into the match entry structure:
```c ```c
match_entry * entry = match_entry_create("/foo/bar"); match_entry * entry = match_entry_create("/foo/bar");
// free the match entry
match_entry_free(entry);
``` ```
And you can even specify the request method restriction: And you can even specify the request method restriction:
@ -105,6 +108,10 @@ r3_tree_compile(n);
route *matched_route = r3_tree_match_route(n, entry); route *matched_route = r3_tree_match_route(n, entry);
matched_route->data; // get the data from matched route matched_route->data; // get the data from matched route
// free the objects at the end
r3_route_free(r1);
r3_tree_free(n);
``` ```

View file

@ -155,15 +155,15 @@ match_entry * match_entry_createl(char * path, int path_len);
void match_entry_free(match_entry * entry); void match_entry_free(match_entry * entry);
route * route_create(char * path); route * r3_route_create(char * path);
route * route_createl(char * path, int path_len); route * r3_route_createl(char * path, int path_len);
int route_cmp(route *r1, match_entry *r2); int r3_route_cmp(route *r1, match_entry *r2);
void r3_node_append_route(node * n, route * route); void r3_node_append_route(node * n, route * route);
void route_free(route * route); void r3_route_free(route * route);
route * r3_tree_match_route(node *n, match_entry * entry); route * r3_tree_match_route(node *n, match_entry * entry);

View file

@ -306,7 +306,7 @@ route * r3_tree_match_route(node *tree, match_entry * entry) {
if (n->routes && n->route_len > 0) { if (n->routes && n->route_len > 0) {
int i; int i;
for (i = 0; i < n->route_len ; i++ ) { for (i = 0; i < n->route_len ; i++ ) {
if ( route_cmp(n->routes[i], entry) == 0 ) { if ( r3_route_cmp(n->routes[i], entry) == 0 ) {
return n->routes[i]; return n->routes[i];
} }
} }
@ -351,15 +351,15 @@ node * r3_node_create() {
} }
route * route_create(char * path) { route * r3_route_create(char * path) {
return route_createl(path, strlen(path)); return r3_route_createl(path, strlen(path));
} }
void route_free(route * route) { void r3_route_free(route * route) {
free(route); free(route);
} }
route * route_createl(char * path, int path_len) { route * r3_route_createl(char * path, int path_len) {
route * info = malloc(sizeof(route)); route * info = malloc(sizeof(route));
info->path = path; info->path = path;
info->path_len = path_len; info->path_len = path_len;
@ -527,7 +527,7 @@ void r3_tree_dump(node * n, int level) {
* *
* -1 == different route * -1 == different route
*/ */
int route_cmp(route *r1, match_entry *r2) { int r3_route_cmp(route *r1, match_entry *r2) {
if (r1->request_method != 0) { if (r1->request_method != 0) {
if (0 == (r1->request_method & r2->request_method) ) { if (0 == (r1->request_method & r2->request_method) ) {
return -1; return -1;

View file

@ -327,3 +327,4 @@
1400466198,13359933.76 1400466198,13359933.76
1400466833,13166545.46 1400466833,13166545.46
1400466875,13515485.94 1400466875,13515485.94
1400467139,13047096.38

1 1400242718 5649455.80
327 1400466198 13359933.76
328 1400466833 13166545.46
329 1400466875 13515485.94
330 1400467139 13047096.38

View file

@ -245,24 +245,24 @@ END_TEST
START_TEST(test_route_cmp) START_TEST(test_route_cmp)
{ {
route *r1 = route_create("/blog/post"); route *r1 = r3_route_create("/blog/post");
match_entry * m = match_entry_create("/blog/post"); match_entry * m = match_entry_create("/blog/post");
fail_if( route_cmp(r1, m) == -1, "should match"); fail_if( r3_route_cmp(r1, m) == -1, "should match");
r1->request_method = METHOD_GET; r1->request_method = METHOD_GET;
m->request_method = METHOD_GET; m->request_method = METHOD_GET;
fail_if( route_cmp(r1, m) == -1, "should match"); fail_if( r3_route_cmp(r1, m) == -1, "should match");
r1->request_method = METHOD_GET; r1->request_method = METHOD_GET;
m->request_method = METHOD_POST; m->request_method = METHOD_POST;
fail_if( route_cmp(r1, m) == 0, "should be different"); fail_if( r3_route_cmp(r1, m) == 0, "should be different");
r1->request_method = METHOD_GET; r1->request_method = METHOD_GET;
m->request_method = METHOD_POST | METHOD_GET; m->request_method = METHOD_POST | METHOD_GET;
fail_if( route_cmp(r1, m) == -1, "should match"); fail_if( r3_route_cmp(r1, m) == -1, "should match");
route_free(r1); r3_route_free(r1);
match_entry_free(m); match_entry_free(m);
} }
END_TEST END_TEST
@ -340,8 +340,8 @@ START_TEST(test_insert_route)
{ {
int var1 = 22; int var1 = 22;
int var2 = 33; int var2 = 33;
route *r1 = route_create("/blog/post"); route *r1 = r3_route_create("/blog/post");
route *r2 = route_create("/blog/post"); route *r2 = r3_route_create("/blog/post");
r1->request_method = METHOD_GET; r1->request_method = METHOD_GET;
r2->request_method = METHOD_POST; r2->request_method = METHOD_POST;
@ -356,8 +356,8 @@ START_TEST(test_insert_route)
fail_if(c == NULL); fail_if(c == NULL);
match_entry_free(entry); match_entry_free(entry);
route_free(r1); r3_route_free(r1);
route_free(r2); r3_route_free(r2);
} }
END_TEST END_TEST