r3/src/token.c

117 lines
2.8 KiB
C
Raw Normal View History

2014-05-14 23:52:45 -04:00
/*
* token.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include <stdlib.h>
2014-05-15 00:47:14 -04:00
#include <stdio.h>
#include <string.h>
#include <assert.h>
2014-05-16 08:22:25 -04:00
#include "str_array.h"
2014-05-14 23:52:45 -04:00
2014-05-16 06:03:52 -04:00
str_array * str_array_create(int cap) {
str_array * list = (str_array*) malloc( sizeof(str_array) );
2014-05-14 23:52:45 -04:00
list->len = 0;
list->cap = cap;
list->tokens = (char**) malloc( sizeof(char*) * cap);
return list;
}
2014-05-16 06:03:52 -04:00
void str_array_free(str_array *l) {
2014-05-15 01:39:50 -04:00
for ( int i = 0; i < l->len ; i++ ) {
char * t = l->tokens[ i ];
free(t);
}
free(l);
}
2014-05-16 06:03:52 -04:00
bool str_array_is_full(str_array * l) {
2014-05-14 23:52:45 -04:00
return l->len >= l->cap;
}
2014-05-16 06:03:52 -04:00
bool str_array_resize(str_array *l, int new_cap) {
2014-05-14 23:52:45 -04:00
l->tokens = realloc(l->tokens, sizeof(char**) * new_cap);
l->cap = new_cap;
return l->tokens != NULL;
}
2014-05-16 06:03:52 -04:00
bool str_array_append(str_array * l, char * token) {
if ( str_array_is_full(l) ) {
bool ret = str_array_resize(l, l->cap + 20);
2014-05-14 23:52:45 -04:00
if (ret == FALSE ) {
return FALSE;
}
}
2014-05-15 00:47:14 -04:00
l->tokens[ l->len++ ] = token;
2014-05-14 23:52:45 -04:00
return TRUE;
}
2014-05-16 06:03:52 -04:00
void str_array_dump(str_array *l) {
2014-05-15 00:47:14 -04:00
printf("[");
for ( int i = 0; i < l->len ; i++ ) {
printf("\"%s\"", l->tokens[i] );
if ( i + 1 != l->len ) {
printf(", ");
}
}
printf("]\n");
}
2014-05-14 23:52:45 -04:00
2014-05-15 00:47:14 -04:00
/**
* This function is used to split route path into a string array, not for performance.
* hence this function should be safe.
*
* Split "/path/foo/{id}" into [ "/path" , "/foo" , "/{id}" ]
* Split "/path/bar/{id}" into [ "/path" , "/foo" , "/{id}" ]
* Split "/blog/post/{id}" into [ "/blog" , "/post" , "/{id}" ]
* Split "/blog/{id}" into [ "/blog" , "/{id}" ]
* Split "/blog" into [ "/blog" ]
* Split "/b" into [ "/b" ]
* Split "/{id}" into [ "/{id}" ]
*
* @param char* pattern
* @param int pattern_len
*
* @return char**
*/
2014-05-16 06:03:52 -04:00
str_array * split_route_pattern(char *pattern, int pattern_len) {
2014-05-15 00:47:14 -04:00
char *s1, *p = pattern;
2014-05-16 06:03:52 -04:00
str_array * str_array = str_array_create( 20 );
2014-05-15 00:47:14 -04:00
s1 = p;
p++;
while (*p && (p - pattern) < pattern_len ) {
// a slug
if ( *p == '{' ) {
// find closing '}'
while (*p != '}') {
p++;
assert(p - pattern < pattern_len ); // throw exception
}
p++; // contains the '}'
// printf("==> %s\n", strndup(s1, p-s1) );
2014-05-16 06:03:52 -04:00
str_array_append(str_array, strndup(s1, p-s1) );
2014-05-15 00:47:14 -04:00
s1 = p;
continue;
}
else if ( *p == '/' ) {
// printf("==> %s\n", strndup(s1, p-s1) );
2014-05-16 06:03:52 -04:00
str_array_append(str_array, strndup(s1, p-s1) );
2014-05-15 00:47:14 -04:00
s1 = p;
}
p++;
}
if ( p-s1 > 0 ) {
2014-05-16 06:03:52 -04:00
str_array_append(str_array, strndup(s1, p-s1) );
2014-05-15 00:47:14 -04:00
}
2014-05-16 06:03:52 -04:00
return str_array;
2014-05-15 00:47:14 -04:00
}