Add const

This commit is contained in:
c9s 2014-05-27 01:07:33 +08:00
parent 533eb6b9c3
commit 54397987bd
5 changed files with 99 additions and 106 deletions

View file

@ -24,7 +24,7 @@ typedef struct _str_array {
str_array * str_array_create(int cap);
bool str_array_is_full(str_array * l);
bool str_array_is_full(const str_array * l);
bool str_array_resize(str_array *l, int new_cap);
@ -32,7 +32,7 @@ bool str_array_append(str_array * list, char * token);
void str_array_free(str_array *l);
void str_array_dump(str_array *l);
void str_array_dump(const str_array *l);
str_array * split_route_pattern(char *pattern, int pattern_len);
@ -136,7 +136,7 @@ edge * r3_node_connectl(node * n, char * pat, int len, int strdup, node *child);
#define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child)
edge * r3_node_find_edge(node * n, char * pat);
edge * r3_node_find_edge(const node * n, char * pat);
void r3_node_append_edge(node *n, edge *child);
@ -151,7 +151,7 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, void * data);
*/
node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route, void * data);
void r3_tree_dump(node * n, int level);
void r3_tree_dump(const node * n, int level);
int r3_tree_render_file(node * tree, char * format, char * filename);
@ -171,9 +171,9 @@ node * r3_tree_matchl(const node * n, char * path, int path_len, match_entry * e
// node * r3_tree_match_entry(node * n, match_entry * entry);
#define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry)
bool r3_node_has_slug_edges(node *n);
bool r3_node_has_slug_edges(const node *n);
edge * r3_edge_create(char * pattern, int pattern_len, node * child);
edge * r3_edge_create(const char * pattern, int pattern_len, node * child);
node * r3_edge_branch(edge *e, int dl);
@ -188,9 +188,9 @@ match_entry * match_entry_createl(char * path, int path_len);
void match_entry_free(match_entry * entry);
route * r3_route_create(char * path);
route * r3_route_create(const char * path);
route * r3_route_createl(char * path, int path_len);
route * r3_route_createl(const char * path, int path_len);
int r3_route_cmp(route *r1, match_entry *r2);

View file

@ -23,7 +23,7 @@
#include "r3_str.h"
#include "zmalloc.h"
edge * r3_edge_create(char * pattern, int pattern_len, node * child) {
edge * r3_edge_create(const char * pattern, int pattern_len, node * child) {
edge * e = (edge*) zmalloc( sizeof(edge) );
e->pattern = pattern;
e->pattern_len = pattern_len;

View file

@ -1,7 +1,6 @@
/*
* list.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* list.c Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include <stdlib.h>
@ -10,121 +9,114 @@
/* Naive linked list implementation */
list *
list *
list_create()
{
list *l = (list *) zmalloc(sizeof(list));
l->count = 0;
l->head = NULL;
l->tail = NULL;
pthread_mutex_init(&(l->mutex), NULL);
return l;
list *l = (list *) zmalloc(sizeof(list));
l->count = 0;
l->head = NULL;
l->tail = NULL;
pthread_mutex_init(&(l->mutex), NULL);
return l;
}
void
list_free(l)
list *l;
list *l;
{
if (l) {
list_item *li, *tmp;
if (l) {
list_item *li, *tmp;
pthread_mutex_lock(&(l->mutex));
if (l != NULL) {
li = l->head;
while (li != NULL) {
tmp = li->next;
li = tmp;
}
}
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_destroy(&(l->mutex));
zfree(l);
}
}
list_item * list_add_element(list * l, void * ptr)
{
list_item *li;
pthread_mutex_lock(&(l->mutex));
if (l != NULL) {
li = l->head;
while (li != NULL) {
tmp = li->next;
li = tmp;
}
li = (list_item *) zmalloc(sizeof(list_item));
li->value = ptr;
li->next = NULL;
li->prev = l->tail;
if (l->tail == NULL) {
l->head = l->tail = li;
} else {
l->tail = li;
}
l->count++;
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_destroy(&(l->mutex));
zfree(l);
}
}
list_item *
list_add_element(l, ptr)
list *l;
void *ptr;
{
list_item *li;
pthread_mutex_lock(&(l->mutex));
li = (list_item *) zmalloc(sizeof(list_item));
li->value = ptr;
li->next = NULL;
li->prev = l->tail;
if (l->tail == NULL) {
l->head = l->tail = li;
}
else {
l->tail = li;
}
l->count++;
pthread_mutex_unlock(&(l->mutex));
return li;
return li;
}
int
list_remove_element(l, ptr)
list *l;
void *ptr;
list *l;
void *ptr;
{
int result = 0;
list_item *li = l->head;
int result = 0;
list_item *li = l->head;
pthread_mutex_lock(&(l->mutex));
pthread_mutex_lock(&(l->mutex));
while (li != NULL) {
if (li->value == ptr) {
if (li->prev == NULL) {
l->head = li->next;
}
else {
li->prev->next = li->next;
}
while (li != NULL) {
if (li->value == ptr) {
if (li->prev == NULL) {
l->head = li->next;
} else {
li->prev->next = li->next;
}
if (li->next == NULL) {
l->tail = li->prev;
}
else {
li->next->prev = li->prev;
}
l->count--;
zfree(li);
result = 1;
break;
if (li->next == NULL) {
l->tail = li->prev;
} else {
li->next->prev = li->prev;
}
l->count--;
zfree(li);
result = 1;
break;
}
li = li->next;
}
li = li->next;
}
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_unlock(&(l->mutex));
return result;
return result;
}
void
list_each_element(l, func)
list *l;
int (*func)(list_item *);
list *l;
int (*func) (list_item *);
{
list_item *li;
list_item *li;
pthread_mutex_lock(&(l->mutex));
pthread_mutex_lock(&(l->mutex));
li = l->head;
while (li != NULL) {
if (func(li) == 1) {
break;
li = l->head;
while (li != NULL) {
if (func(li) == 1) {
break;
}
li = li->next;
}
li = li->next;
}
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_unlock(&(l->mutex));
}

View file

@ -109,7 +109,7 @@ void r3_node_append_edge(node *n, edge *e) {
n->edges[ n->edge_len++ ] = e;
}
edge * r3_node_find_edge(node * n, char * pat) {
edge * r3_node_find_edge(const node * n, char * pat) {
edge * e;
for (int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i];
@ -420,7 +420,7 @@ node * r3_node_create() {
}
route * r3_route_create(char * path) {
route * r3_route_create(const char * path) {
return r3_route_createl(path, strlen(path));
}
@ -428,9 +428,9 @@ void r3_route_free(route * route) {
zfree(route);
}
route * r3_route_createl(char * path, int path_len) {
route * r3_route_createl(const char * path, int path_len) {
route * info = zmalloc(sizeof(route));
info->path = path;
info->path = (char*) path;
info->path_len = path_len;
info->request_method = 0; // can be (GET || POST)
@ -602,7 +602,7 @@ node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route
return n;
}
bool r3_node_has_slug_edges(node *n) {
bool r3_node_has_slug_edges(const node *n) {
bool found = FALSE;
edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) {
@ -616,7 +616,7 @@ bool r3_node_has_slug_edges(node *n) {
void r3_tree_dump(node * n, int level) {
void r3_tree_dump(const node * n, int level) {
print_indent(level);
printf("(o)");

View file

@ -22,17 +22,18 @@ str_array * str_array_create(int cap) {
void str_array_free(str_array *l) {
for ( int i = 0; i < l->len ; i++ ) {
char * t = l->tokens[ i ];
zfree(t);
if (l->tokens[ i ]) {
zfree(l->tokens[i]);
}
}
zfree(l);
}
bool str_array_is_full(str_array * l) {
bool str_array_is_full(const str_array * l) {
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 = zrealloc(l->tokens, sizeof(char**) * new_cap);
l->cap = new_cap;
return l->tokens != NULL;
@ -49,7 +50,7 @@ bool str_array_append(str_array * l, char * token) {
return TRUE;
}
void str_array_dump(str_array *l) {
void str_array_dump(const str_array *l) {
printf("[");
for ( int i = 0; i < l->len ; i++ ) {
printf("\"%s\"", l->tokens[i] );