use HAVE_STR*DUP config to check function definitions

This commit is contained in:
c9s 2014-05-17 07:26:47 +08:00
parent 952ec60e99
commit 0c6afd03b7
6 changed files with 27 additions and 17 deletions

View file

@ -8,6 +8,7 @@
#define STR_H #define STR_H
#include "r3_define.h" #include "r3_define.h"
#include "config.h"
int strndiff(char * d1, char * d2, unsigned int n); int strndiff(char * d1, char * d2, unsigned int n);
@ -28,9 +29,13 @@ void str_repeat(char *s, char *c, int len);
void print_indent(int level); void print_indent(int level);
char *my_strdup(const char *s); #ifndef HAVE_STRDUP
char *strdup(const char *s);
#endif
char *my_strndup(const char *s, int n); #ifndef HAVE_STRNDUP
char *strndup(const char *s, int n);
#endif
#endif /* !STR_H */ #endif /* !STR_H */

View file

@ -42,7 +42,7 @@ void r3_edge_branch(edge *e, int dl) {
// the suffix edge of the leaf // the suffix edge of the leaf
c1 = r3_tree_create(3); c1 = r3_tree_create(3);
s1_len = e->pattern_len - dl; s1_len = e->pattern_len - dl;
e1 = r3_edge_create(my_strndup(s1, s1_len), s1_len, c1); e1 = r3_edge_create(strndup(s1, s1_len), s1_len, c1);
// printf("edge left: %s\n", e1->pattern); // printf("edge left: %s\n", e1->pattern);
// Migrate the child edges to the new edge we just created. // Migrate the child edges to the new edge we just created.

View file

@ -275,7 +275,7 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
if (entry && e->has_slug) { if (entry && e->has_slug) {
// append captured token to entry // append captured token to entry
str_array_append(entry->vars , my_strndup(substring_start, substring_length)); str_array_append(entry->vars , strndup(substring_start, substring_length));
} }
if (restlen == 0) { if (restlen == 0) {
return e->child; return e->child;
@ -379,7 +379,7 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
if ( dl == 0 ) { if ( dl == 0 ) {
// not found, we should just insert a whole new edge // not found, we should just insert a whole new edge
node * child = r3_tree_create(3); node * child = r3_tree_create(3);
r3_tree_add_child(n, my_strndup(route, route_len) , child); r3_tree_add_child(n, strndup(route, route_len) , child);
info("edge not found, insert one: %s\n", route); info("edge not found, insert one: %s\n", route);
child->route_ptr = route_ptr; child->route_ptr = route_ptr;
child->endpoint++; child->endpoint++;
@ -416,13 +416,13 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
// here is the new edge from. // here is the new edge from.
c2 = r3_tree_create(3); c2 = r3_tree_create(3);
s2_len = route_len - dl; s2_len = route_len - dl;
e2 = r3_edge_create(my_strndup(s2, s2_len), s2_len, c2); e2 = r3_edge_create(strndup(s2, s2_len), s2_len, c2);
// printf("edge right: %s\n", e2->pattern); // printf("edge right: %s\n", e2->pattern);
r3_tree_append_edge(e->child, e2); r3_tree_append_edge(e->child, e2);
// truncate the original edge pattern // truncate the original edge pattern
free(e->pattern); free(e->pattern);
e->pattern = my_strndup(e->pattern, dl); e->pattern = strndup(e->pattern, dl);
e->pattern_len = dl; e->pattern_len = dl;
// move n->edges to c1 // move n->edges to c1

View file

@ -62,7 +62,7 @@ char * compile_slug(char * str, int len)
s1 = strchr(str, '{'); s1 = strchr(str, '{');
if ( s1 == NULL ) { if ( s1 == NULL ) {
return my_strdup(str); return strdup(str);
} }
if ( (s1 - str) > 0 ) { if ( (s1 - str) > 0 ) {
@ -117,7 +117,7 @@ char * ltrim_slash(char* str)
{ {
char * p = str; char * p = str;
while (*p == '/') p++; while (*p == '/') p++;
return my_strdup(p); return strdup(p);
} }
char** str_split(char* a_str, const char a_delim) char** str_split(char* a_str, const char a_delim)
@ -158,7 +158,7 @@ char** str_split(char* a_str, const char a_delim)
while (token) while (token)
{ {
assert(idx < count); assert(idx < count);
*(result + idx++) = my_strdup(token); *(result + idx++) = strdup(token);
token = strtok(0, delim); token = strtok(0, delim);
} }
assert(idx == count - 1); assert(idx == count - 1);
@ -181,7 +181,8 @@ void print_indent(int level) {
} }
} }
char *my_strdup(const char *s) { #ifndef HAVE_STRDUP
char *strdup(const char *s) {
char *out; char *out;
int count = 0; int count = 0;
while( s[count] ) while( s[count] )
@ -193,8 +194,10 @@ char *my_strdup(const char *s) {
out[count] = s[count]; out[count] = s[count];
return out; return out;
} }
#endif
char *my_strndup(const char *s, int n) { #ifndef HAVE_STRNDUP
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] )
@ -206,3 +209,4 @@ char *my_strndup(const char *s, int n) {
out[count] = s[count]; out[count] = s[count];
return out; return out;
} }
#endif

View file

@ -97,21 +97,21 @@ str_array * split_route_pattern(char *pattern, int pattern_len) {
assert(p - pattern < pattern_len ); // throw exception assert(p - pattern < pattern_len ); // throw exception
} }
p++; // contains the '}' p++; // contains the '}'
// printf("==> %s\n", my_strndup(s1, p-s1) ); // printf("==> %s\n", strndup(s1, p-s1) );
str_array_append(str_array, my_strndup(s1, p-s1) ); str_array_append(str_array, strndup(s1, p-s1) );
s1 = p; s1 = p;
continue; continue;
} }
else if ( *p == '/' ) { else if ( *p == '/' ) {
// printf("==> %s\n", my_strndup(s1, p-s1) ); // printf("==> %s\n", strndup(s1, p-s1) );
str_array_append(str_array, my_strndup(s1, p-s1) ); str_array_append(str_array, strndup(s1, p-s1) );
s1 = p; s1 = p;
} }
p++; p++;
} }
if ( p-s1 > 0 ) { if ( p-s1 > 0 ) {
str_array_append(str_array, my_strndup(s1, p-s1) ); str_array_append(str_array, strndup(s1, p-s1) );
} }
return str_array; return str_array;
} }

View file

@ -49,3 +49,4 @@
1400278620,5733465.40 1400278620,5733465.40
1400279249,6306920.57 1400279249,6306920.57
1400282602,11775983.90 1400282602,11775983.90
1400282802,11627473.22

1 1400242718 5649455.80
49 1400278620 5733465.40
50 1400279249 6306920.57
51 1400282602 11775983.90
52 1400282802 11627473.22