Formatted the project using AStyle.

This commit is contained in:
Omer Katz 2014-05-19 13:54:13 +03:00
parent 6ea4405ddf
commit 8fc6333de5
13 changed files with 645 additions and 597 deletions

4
main.c
View file

@ -1,4 +1,3 @@
#include <stdlib.h> #include <stdlib.h>
#include <pcre.h> #include <pcre.h>
#include "main.h" #include "main.h"
@ -12,6 +11,7 @@ typedef struct _Node {
void compile() { void compile()
{
} }

View file

@ -24,7 +24,8 @@
#include "r3.h" #include "r3.h"
#include "str_array.h" #include "str_array.h"
edge * r3_edge_create(char * pattern, int pattern_len, node * child) { edge * r3_edge_create(char * pattern, int pattern_len, node * child)
{
edge * e = (edge*) malloc( sizeof(edge) ); edge * e = (edge*) malloc( sizeof(edge) );
e->pattern = pattern; e->pattern = pattern;
e->pattern_len = pattern_len; e->pattern_len = pattern_len;
@ -43,7 +44,8 @@ edge * r3_edge_create(char * pattern, int pattern_len, node * child) {
* A -> [prefix] -> B -> [suffix] -> New Child (Copy Data, Edges from B) * A -> [prefix] -> B -> [suffix] -> New Child (Copy Data, Edges from B)
* *
*/ */
node * r3_edge_branch(edge *e, int dl) { node * r3_edge_branch(edge *e, int dl)
{
node *new_child; node *new_child;
edge *e1; edge *e1;
char * s1 = e->pattern + dl; char * s1 = e->pattern + dl;
@ -81,7 +83,8 @@ node * r3_edge_branch(edge *e, int dl) {
return new_child; return new_child;
} }
void r3_edge_free(edge * e) { void r3_edge_free(edge * e)
{
if (e->pattern) { if (e->pattern) {
free(e->pattern); free(e->pattern);
} }

View file

@ -12,13 +12,15 @@
#include "r3_gvc.h" #include "r3_gvc.h"
char * node_id_str(int id) { char * node_id_str(int id)
{
char * name = malloc(sizeof(char) * 20); char * name = malloc(sizeof(char) * 20);
sprintf(name, "#%d", id); sprintf(name, "#%d", id);
return name; return name;
} }
void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, node * n, int node_cnt) { void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, node * n, int node_cnt)
{
edge * e; edge * e;
Agnode_t *agn_child; Agnode_t *agn_child;
Agedge_t *agn_edge; Agedge_t *agn_edge;

View file

@ -58,8 +58,7 @@ list_add_element(l, ptr)
if (l->tail == NULL) { if (l->tail == NULL) {
l->head = l->tail = li; l->head = l->tail = li;
} } else {
else {
l->tail = li; l->tail = li;
} }
l->count++; l->count++;
@ -83,15 +82,13 @@ list_remove_element(l, ptr)
if (li->value == ptr) { if (li->value == ptr) {
if (li->prev == NULL) { if (li->prev == NULL) {
l->head = li->next; l->head = li->next;
} } else {
else {
li->prev->next = li->next; li->prev->next = li->next;
} }
if (li->next == NULL) { if (li->next == NULL) {
l->tail = li->prev; l->tail = li->prev;
} } else {
else {
li->next->prev = li->prev; li->next->prev = li->prev;
} }
l->count--; l->count--;

View file

@ -24,7 +24,8 @@
/** /**
* Create a node object * Create a node object
*/ */
node * r3_tree_create(int cap) { node * r3_tree_create(int cap)
{
node * n = (node*) malloc( sizeof(node) ); node * n = (node*) malloc( sizeof(node) );
n->edges = (edge**) malloc( sizeof(edge*) * 10 ); n->edges = (edge**) malloc( sizeof(edge*) * 10 );
@ -39,7 +40,8 @@ node * r3_tree_create(int cap) {
return n; return n;
} }
void r3_tree_free(node * tree) { void r3_tree_free(node * tree)
{
for (int i = 0 ; i < tree->edge_len ; i++ ) { for (int i = 0 ; i < tree->edge_len ; i++ ) {
if (tree->edges[i]) { if (tree->edges[i]) {
r3_edge_free(tree->edges[ i ]); r3_edge_free(tree->edges[ i ]);
@ -63,7 +65,8 @@ void r3_tree_free(node * tree) {
/* parent node, edge pattern, child */ /* parent node, edge pattern, child */
edge * r3_node_add_child(node * n, char * pat , node *child) { edge * r3_node_add_child(node * n, char * pat , node *child)
{
// find the same sub-pattern, if it does not exist, create one // find the same sub-pattern, if it does not exist, create one
edge * e; edge * e;
@ -82,7 +85,8 @@ edge * r3_node_add_child(node * n, char * pat , node *child) {
void r3_node_append_edge(node *n, edge *e) { void r3_node_append_edge(node *n, edge *e)
{
if (!n->edges) { if (!n->edges) {
n->edge_cap = 3; n->edge_cap = 3;
n->edges = malloc(sizeof(edge) * n->edge_cap); n->edges = malloc(sizeof(edge) * n->edge_cap);
@ -94,7 +98,8 @@ void r3_node_append_edge(node *n, edge *e) {
n->edges[ n->edge_len++ ] = e; n->edges[ n->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->edge_len ; i++ ) { for (int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
@ -125,7 +130,8 @@ void r3_tree_compile(node *n)
* This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)} * This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)}
* *
*/ */
void r3_tree_compile_patterns(node * n) { void r3_tree_compile_patterns(node * n)
{
char * cpat; char * cpat;
char * p; char * p;
@ -196,7 +202,8 @@ void r3_tree_compile_patterns(node * n) {
} }
match_entry * match_entry_createl(char * path, int path_len) { match_entry * match_entry_createl(char * path, int path_len)
{
match_entry * entry = malloc(sizeof(match_entry)); match_entry * entry = malloc(sizeof(match_entry));
if(!entry) if(!entry)
return NULL; return NULL;
@ -207,7 +214,8 @@ match_entry * match_entry_createl(char * path, int path_len) {
return entry; return entry;
} }
void match_entry_free(match_entry * entry) { void match_entry_free(match_entry * entry)
{
str_array_free(entry->vars); str_array_free(entry->vars);
free(entry); free(entry);
} }
@ -224,7 +232,8 @@ void match_entry_free(match_entry * entry) {
* @param int path_len the length of the URL path. * @param int path_len the length of the URL path.
* @param match_entry* entry match_entry is used for saving the captured dynamic strings from pcre result. * @param match_entry* entry match_entry is used for saving the captured dynamic strings from pcre result.
*/ */
node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry) { node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry)
{
info("try matching: %s\n", path); info("try matching: %s\n", path);
edge *e; edge *e;
@ -250,21 +259,23 @@ node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry)
// info("rc: %d\n", rc ); // info("rc: %d\n", rc );
if (rc < 0) { if (rc < 0) {
switch(rc) switch(rc) {
{ case PCRE_ERROR_NOMATCH:
case PCRE_ERROR_NOMATCH: printf("No match\n"); break; printf("No match\n");
break;
/* /*
Handle other special cases if you like Handle other special cases if you like
*/ */
default: printf("Matching error %d\n", rc); break; default:
printf("Matching error %d\n", rc);
break;
} }
// does not match all edges, return NULL; // does not match all edges, return NULL;
return NULL; return NULL;
} }
for (i = 1; i < rc; i++) for (i = 1; i < rc; i++) {
{
char *substring_start = path + n->ov[2*i]; char *substring_start = path + n->ov[2*i];
int substring_length = n->ov[2*i+1] - n->ov[2*i]; int substring_length = n->ov[2*i+1] - n->ov[2*i];
// info("%2d: %.*s\n", i, substring_length, substring_start); // info("%2d: %.*s\n", i, substring_length, substring_start);
@ -300,7 +311,8 @@ node * r3_tree_matchl(node * n, char * path, int path_len, match_entry * entry)
return NULL; return NULL;
} }
route * r3_tree_match_route(node *tree, match_entry * entry) { route * r3_tree_match_route(node *tree, match_entry * entry)
{
node *n; node *n;
n = r3_tree_match_entry(tree, entry); n = r3_tree_match_entry(tree, entry);
if (n->routes && n->route_len > 0) { if (n->routes && n->route_len > 0) {
@ -314,7 +326,8 @@ route * r3_tree_match_route(node *tree, match_entry * entry) {
return NULL; return NULL;
} }
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;
int matched_idx = 0; int matched_idx = 0;
@ -334,7 +347,8 @@ inline edge * r3_node_find_edge_str(node * n, char * str, int str_len) {
node * r3_node_create() { node * r3_node_create()
{
node * n = (node*) malloc( sizeof(node) ); node * n = (node*) malloc( sizeof(node) );
n->edges = NULL; n->edges = NULL;
n->edge_len = 0; n->edge_len = 0;
@ -351,15 +365,18 @@ node * r3_node_create() {
} }
route * r3_route_create(char * path) { route * r3_route_create(char * path)
{
return r3_route_createl(path, strlen(path)); return r3_route_createl(path, strlen(path));
} }
void r3_route_free(route * route) { void r3_route_free(route * route)
{
free(route); free(route);
} }
route * r3_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;
@ -487,7 +504,8 @@ node * _r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route
return n; return n;
} }
bool r3_node_has_slug_edges(node *n) { bool r3_node_has_slug_edges(node *n)
{
bool found = FALSE; bool found = FALSE;
edge *e; edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
@ -501,7 +519,8 @@ bool r3_node_has_slug_edges(node *n) {
void r3_tree_dump(node * n, int level) { void r3_tree_dump(node * n, int level)
{
print_indent(level); print_indent(level);
if ( n->combined_pattern ) { if ( n->combined_pattern ) {
printf(" regexp:%s", n->combined_pattern); printf(" regexp:%s", n->combined_pattern);
@ -533,7 +552,8 @@ void r3_tree_dump(node * n, int level) {
* *
* -1 == different route * -1 == different route
*/ */
int r3_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;
@ -570,7 +590,8 @@ 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)
{
if (!n->routes) { if (!n->routes) {
n->route_cap = 3; n->route_cap = 3;
n->routes = malloc(sizeof(route) * n->route_cap); n->routes = malloc(sizeof(route) * n->route_cap);

View file

@ -12,7 +12,8 @@
#include "str_array.h" #include "str_array.h"
#include "r3_define.h" #include "r3_define.h"
int strndiff(char * d1, char * d2, unsigned int n) { int strndiff(char * d1, char * d2, unsigned int n)
{
char * o = d1; char * o = d1;
while ( *d1 == *d2 && n-- > 0 ) { while ( *d1 == *d2 && n-- > 0 ) {
d1++; d1++;
@ -22,7 +23,8 @@ int strndiff(char * d1, char * d2, unsigned int n) {
} }
int strdiff(char * d1, char * d2) { int strdiff(char * d1, char * d2)
{
char * o = d1; char * o = d1;
while( *d1 == *d2 ) { while( *d1 == *d2 ) {
d1++; d1++;
@ -35,7 +37,8 @@ int strdiff(char * d1, char * d2) {
/** /**
* provide a quick way to count slugs, simply search for '{' * provide a quick way to count slugs, simply search for '{'
*/ */
int count_slug(char * p, int len) { int count_slug(char * p, int len)
{
int s = 0; int s = 0;
int lev = 0; int lev = 0;
while( len-- ) { while( len-- ) {
@ -52,11 +55,13 @@ int count_slug(char * p, int len) {
return s; return s;
} }
bool contains_slug(char * str) { bool contains_slug(char * str)
{
return strchr(str, '{') != NULL ? TRUE : FALSE; return strchr(str, '{') != NULL ? TRUE : FALSE;
} }
char * inside_slug(char * needle, int needle_len, char *offset) { char * inside_slug(char * needle, int needle_len, char *offset)
{
char * s1 = offset; char * s1 = offset;
char * s2 = offset; char * s2 = offset;
@ -81,7 +86,8 @@ char * inside_slug(char * needle, int needle_len, char *offset) {
return NULL; return NULL;
} }
char * find_slug_placeholder(char *s1, int *len) { char * find_slug_placeholder(char *s1, int *len)
{
char *c; char *c;
char *s2; char *s2;
int cnt = 0; int cnt = 0;
@ -113,7 +119,8 @@ char * find_slug_placeholder(char *s1, int *len) {
/** /**
* given a slug string, duplicate the pattern string of the slug * given a slug string, duplicate the pattern string of the slug
*/ */
char * find_slug_pattern(char *s1, int *len) { char * find_slug_pattern(char *s1, int *len)
{
char *c; char *c;
char *s2; char *s2;
int cnt = 1; int cnt = 1;
@ -205,10 +212,8 @@ char** str_split(char* a_str, const char a_delim)
delim[1] = 0; delim[1] = 0;
/* Count how many elements will be extracted. */ /* Count how many elements will be extracted. */
while (*tmp) while (*tmp) {
{ if (a_delim == *tmp) {
if (a_delim == *tmp)
{
count++; count++;
last_comma = tmp; last_comma = tmp;
} }
@ -224,13 +229,11 @@ char** str_split(char* a_str, const char a_delim)
result = malloc(sizeof(char*) * count); result = malloc(sizeof(char*) * count);
if (result) if (result) {
{
size_t idx = 0; size_t idx = 0;
char* token = strtok(a_str, delim); char* token = strtok(a_str, delim);
while (token) while (token) {
{
assert(idx < count); assert(idx < count);
*(result + idx++) = strdup(token); *(result + idx++) = strdup(token);
token = strtok(0, delim); token = strtok(0, delim);
@ -242,13 +245,15 @@ char** str_split(char* a_str, const char a_delim)
return result; return result;
} }
void str_repeat(char *s, char *c, int len) { void str_repeat(char *s, char *c, int len)
{
while(len--) { while(len--) {
s[len - 1] = *c; s[len - 1] = *c;
} }
} }
void print_indent(int level) { void print_indent(int level)
{
int len = level * 2; int len = level * 2;
while(len--) { while(len--) {
printf(" "); printf(" ");
@ -256,7 +261,8 @@ void print_indent(int level) {
} }
#ifndef HAVE_STRDUP #ifndef HAVE_STRDUP
char *strdup(const char *s) { char *strdup(const char *s)
{
char *out; char *out;
int count = 0; int count = 0;
while( s[count] ) while( s[count] )
@ -271,7 +277,8 @@ char *strdup(const char *s) {
#endif #endif
#ifndef HAVE_STRNDUP #ifndef HAVE_STRNDUP
char *strndup(const char *s, int n) { char *strndup(const char *s, int n)
{
char *out; char *out;
int count = 0; int count = 0;
while( count < n && s[count] ) while( count < n && s[count] )

View file

@ -12,7 +12,8 @@
#include "r3_str.h" #include "r3_str.h"
str_array * str_array_create(int cap) { str_array * str_array_create(int cap)
{
str_array * list = (str_array*) malloc( sizeof(str_array) ); str_array * list = (str_array*) malloc( sizeof(str_array) );
list->len = 0; list->len = 0;
list->cap = cap; list->cap = cap;
@ -20,7 +21,8 @@ str_array * str_array_create(int cap) {
return list; return list;
} }
void str_array_free(str_array *l) { void str_array_free(str_array *l)
{
for ( int i = 0; i < l->len ; i++ ) { for ( int i = 0; i < l->len ; i++ ) {
char * t = l->tokens[ i ]; char * t = l->tokens[ i ];
free(t); free(t);
@ -28,17 +30,20 @@ void str_array_free(str_array *l) {
free(l); free(l);
} }
bool str_array_is_full(str_array * l) { bool str_array_is_full(str_array * l)
{
return l->len >= l->cap; return l->len >= l->cap;
} }
bool str_array_resize(str_array *l, int new_cap) { bool str_array_resize(str_array *l, int new_cap)
{
l->tokens = realloc(l->tokens, sizeof(char**) * new_cap); l->tokens = realloc(l->tokens, sizeof(char**) * new_cap);
l->cap = new_cap; l->cap = new_cap;
return l->tokens != NULL; return l->tokens != NULL;
} }
bool str_array_append(str_array * l, char * token) { bool str_array_append(str_array * l, char * token)
{
if ( str_array_is_full(l) ) { if ( str_array_is_full(l) ) {
bool ret = str_array_resize(l, l->cap + 20); bool ret = str_array_resize(l, l->cap + 20);
if (ret == FALSE ) { if (ret == FALSE ) {
@ -49,7 +54,8 @@ bool str_array_append(str_array * l, char * token) {
return TRUE; return TRUE;
} }
void str_array_dump(str_array *l) { void str_array_dump(str_array *l)
{
printf("["); printf("[");
for ( int i = 0; i < l->len ; i++ ) { for ( int i = 0; i < l->len ; i++ ) {
printf("\"%s\"", l->tokens[i] ); printf("\"%s\"", l->tokens[i] );

View file

@ -10,7 +10,8 @@
#include <stdio.h> #include <stdio.h>
#include <sys/time.h> #include <sys/time.h>
long unixtime() { long unixtime()
{
struct timeval tp; struct timeval tp;
long sec = 0L; long sec = 0L;
if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) { if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
@ -19,7 +20,8 @@ long unixtime() {
return 0; return 0;
} }
double microtime() { double microtime()
{
struct timeval tp; struct timeval tp;
long sec = 0L; long sec = 0L;
double msec = 0.0; double msec = 0.0;
@ -36,19 +38,23 @@ double microtime() {
} }
void bench_start(bench *b) { void bench_start(bench *b)
{
b->start = microtime(); b->start = microtime();
} }
void bench_stop(bench *b) { void bench_stop(bench *b)
{
b->end = microtime(); b->end = microtime();
} }
double bench_iteration_speed(bench *b) { double bench_iteration_speed(bench *b)
{
return b->N / (b->end - b->start); return b->N / (b->end - b->start);
} }
void bench_print_summary(bench *b) { void bench_print_summary(bench *b)
{
printf("%ld runs, ", b->R); printf("%ld runs, ", b->R);
printf("%ld iterations each run, ", b->N); printf("%ld iterations each run, ", b->N);
printf("finished in %lf seconds\n", b->end - b->start ); printf("finished in %lf seconds\n", b->end - b->start );

View file

@ -56,7 +56,8 @@ START_TEST (test_gvc_render_file)
END_TEST END_TEST
Suite* r3_suite (void) { Suite* r3_suite (void)
{
Suite *suite = suite_create("gvc test"); Suite *suite = suite_create("gvc test");
TCase *tcase = tcase_create("test_gvc"); TCase *tcase = tcase_create("test_gvc");
tcase_add_test(tcase, test_gvc_render_file); tcase_add_test(tcase, test_gvc_render_file);
@ -65,7 +66,8 @@ Suite* r3_suite (void) {
return suite; return suite;
} }
int main (int argc, char *argv[]) { int main (int argc, char *argv[])
{
int number_failed; int number_failed;
Suite *suite = r3_suite(); Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite); SRunner *runner = srunner_create(suite);

View file

@ -78,7 +78,8 @@ START_TEST (test_find_slug_placeholder_with_broken_slug)
END_TEST END_TEST
Suite* r3_suite (void) { Suite* r3_suite (void)
{
Suite *suite = suite_create("slug test"); Suite *suite = suite_create("slug test");
TCase *tcase = tcase_create("test_slug"); TCase *tcase = tcase_create("test_slug");
tcase_add_test(tcase, test_contains_slug); tcase_add_test(tcase, test_contains_slug);
@ -93,7 +94,8 @@ Suite* r3_suite (void) {
return suite; return suite;
} }
int main (int argc, char *argv[]) { int main (int argc, char *argv[])
{
int number_failed; int number_failed;
Suite *suite = r3_suite(); Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite); SRunner *runner = srunner_create(suite);

View file

@ -735,7 +735,8 @@ r3_tree_insert_path(n, "/garply/grault/corge", NULL);
END_TEST END_TEST
Suite* r3_suite (void) { Suite* r3_suite (void)
{
Suite *suite = suite_create("blah"); Suite *suite = suite_create("blah");
TCase *tcase = tcase_create("testcase"); TCase *tcase = tcase_create("testcase");
@ -758,7 +759,8 @@ Suite* r3_suite (void) {
return suite; return suite;
} }
int main (int argc, char *argv[]) { int main (int argc, char *argv[])
{
int number_failed; int number_failed;
Suite *suite = r3_suite(); Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite); SRunner *runner = srunner_create(suite);