From 80d7ca8421d758a1a7a2988391f1ee5ae7075ab7 Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 2 Feb 2024 08:37:52 -0600 Subject: wildcard --- src/net.c | 31 +++++++++++++++++++++++++++---- src/parray.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/parray.h | 6 +++++- 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/net.c b/src/net.c index 8dcde19..ad48011 100644 --- a/src/net.c +++ b/src/net.c @@ -294,7 +294,7 @@ void* handle_client(void *_arg){ str* aa = str_init(portc); str_push(aa, table[k]->c); - void* v = parray_get(paths, aa->c); + void* v = parray_find(paths, aa->c); if(v == NULL){ str* resp; @@ -339,9 +339,31 @@ void* handle_client(void *_arg){ lua_settable(L, res_idx); //the function(s) - struct sarray_t* awa = (struct sarray_t*)v; + //get all function that kinda match + parray_t* owo = (parray_t*)v; + for(int i = 0; i != owo->len; i++){ + //though these are arrays of arrays we have to iterate *again* + struct sarray_t* awa = (struct sarray_t*)owo->P[i].value; + + for(int z = 0; z != awa->len; z++){ + struct lchar* wowa = awa->cs[z]; + + luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c); + + int func = lua_gettop(L); + + lua_pushvalue(L, func); // push function call + lua_pushvalue(L, res_idx); //push methods related to dealing with the request + lua_pushvalue(L, req_idx); //push info about the request + + //call the function + lua_call(L, 2, 0); + } + } + /* for(int i = 0; i != awa->len; i++){ - struct lchar* wowa = awa->cs[i]; + //struct lchar* wowa = awa->cs[i]; + luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c); int func = lua_gettop(L); @@ -352,7 +374,8 @@ void* handle_client(void *_arg){ //call the function lua_call(L, 2, 0); - } + }*/ + } } diff --git a/src/parray.c b/src/parray.c index 077a67f..637bc0b 100644 --- a/src/parray.c +++ b/src/parray.c @@ -35,11 +35,51 @@ void* parray_get(parray_t* p, char* key){ return NULL; } +void parray_lclear(parray_t* p){ + free(p->P); + free(p); +} + void parray_clear(parray_t* p, int clear_val){ for(int i = 0; i != p->len; i++){ str_free(p[i].P->key); if(clear_val) free(p[i].P->value); } - free(p->P); - free(p); + parray_lclear(p); +} + +int fmatch(char* s, char* p) { + int slen = strlen(s); + int plen = strlen(p); + int sidx = 0, pidx = 0, lastWildcardIdx = -1, sBacktrackIdx = -1, nextToWildcardIdx = -1; + for(;sidx < slen;) { + if (pidx < plen && (p[pidx] == '?' || p[pidx] == s[sidx])) { + sidx++; + pidx++; + } else if (pidx < plen && p[pidx] == '*') { + lastWildcardIdx = pidx; + nextToWildcardIdx = ++pidx; + sBacktrackIdx = sidx; + } else if (lastWildcardIdx == -1) { + return 0; + } else { + pidx = nextToWildcardIdx; + sidx = sBacktrackIdx++; + } + } + for(int i = pidx; i < plen; i++) if(p[i] != '*') return 0; + return 1; } + +parray_t* parray_find(parray_t* p, char* match){ + parray_t* ret = malloc(sizeof * ret); + ret->P = malloc(sizeof * ret->P * p->len); + ret->len = 0; + for(int i = 0; i != p->len; i++){ + if(fmatch(match, p->P[i].key->c)){ + ret->P[ret->len] = p->P[i]; + ret->len++; + } + } + return ret; +} \ No newline at end of file diff --git a/src/parray.h b/src/parray.h index 5821922..3f33865 100644 --- a/src/parray.h +++ b/src/parray.h @@ -15,4 +15,8 @@ void parray_set(parray_t*, char*, void*); void* parray_get(parray_t* , char*); -void parray_clear(parray_t*, int); \ No newline at end of file +void parray_clear(parray_t*, int); + +void parray_lclear(parray_t*); + +parray_t* parray_find(parray_t*, char*); \ No newline at end of file -- cgit v1.2.3