From 149d594012b32158cd5e29f484cf0274dde6af44 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 22 Feb 2024 10:17:40 -0600 Subject: cleaning and switch to parray_t --- src/crypto.h | 2 +- src/hash/buzhash.c | 2 +- src/hash/djb2.c | 2 +- src/hash/farmhash.c | 2 +- src/hash/fasthash.c | 2 +- src/hash/fnv.c | 2 +- src/hash/jenkins.c | 2 +- src/hash/loselose.c | 2 +- src/hash/metrohash.c | 2 +- src/hash/murmur.c | 2 +- src/hash/pearson.c | 2 +- src/hash/pjw.c | 2 +- src/hash/sdbm.c | 2 +- src/hash/sha2-256.c | 2 +- src/hash/spookyhash.c | 2 +- src/i_common.c | 0 src/i_common.h | 2 - src/i_str.c | 47 ------------------ src/i_str.h | 15 ------ src/i_util.c | 13 ----- src/i_util.h | 20 -------- src/io.c | 4 +- src/lua.c | 4 +- src/net.c | 129 +++++++++++++++++++++++--------------------------- src/parray.c | 86 --------------------------------- src/parray.h | 22 --------- src/sort.h | 3 +- src/table.h | 3 +- src/types/parray.c | 86 +++++++++++++++++++++++++++++++++ src/types/parray.h | 22 +++++++++ src/types/str.c | 47 ++++++++++++++++++ src/types/str.h | 15 ++++++ src/util.c | 13 +++++ src/util.h | 22 +++++++++ 34 files changed, 286 insertions(+), 297 deletions(-) delete mode 100644 src/i_common.c delete mode 100644 src/i_common.h delete mode 100644 src/i_str.c delete mode 100644 src/i_str.h delete mode 100644 src/i_util.c delete mode 100644 src/i_util.h delete mode 100644 src/parray.c delete mode 100644 src/parray.h create mode 100644 src/types/parray.c create mode 100644 src/types/parray.h create mode 100644 src/types/str.c create mode 100644 src/types/str.h create mode 100644 src/util.c create mode 100644 src/util.h (limited to 'src') diff --git a/src/crypto.h b/src/crypto.h index 564ea58..e0a29b0 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -1,5 +1,5 @@ #include "lua.h" -#include "i_util.h" +#include "util.h" #include "hash/md5.h" #include "hash/sha01.h" #include "hash/sha2xx.h" diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c index 36b39f3..9e1b560 100644 --- a/src/hash/buzhash.c +++ b/src/hash/buzhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/djb2.c b/src/hash/djb2.c index 7ec39de..19075d2 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/farmhash.c b/src/hash/farmhash.c index 93e194b..6533eb4 100644 --- a/src/hash/farmhash.c +++ b/src/hash/farmhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" //include city hash too #include diff --git a/src/hash/fasthash.c b/src/hash/fasthash.c index 3ad0ba2..0ac2cc0 100644 --- a/src/hash/fasthash.c +++ b/src/hash/fasthash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/fnv.c b/src/hash/fnv.c index 1fc4d0a..ac1f88b 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/jenkins.c b/src/hash/jenkins.c index b11de8e..a08f526 100644 --- a/src/hash/jenkins.c +++ b/src/hash/jenkins.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/loselose.c b/src/hash/loselose.c index a421079..f3350f5 100644 --- a/src/hash/loselose.c +++ b/src/hash/loselose.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/metrohash.c b/src/hash/metrohash.c index 0b6ca42..9ce08b3 100644 --- a/src/hash/metrohash.c +++ b/src/hash/metrohash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/murmur.c b/src/hash/murmur.c index 9d913db..18f2887 100644 --- a/src/hash/murmur.c +++ b/src/hash/murmur.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/pearson.c b/src/hash/pearson.c index 9372931..4e1ea0a 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/pjw.c b/src/hash/pjw.c index 8494ba2..50593b9 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c index bec251e..d913d01 100644 --- a/src/hash/sdbm.c +++ b/src/hash/sdbm.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 34afe63..e343825 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/hash/spookyhash.c b/src/hash/spookyhash.c index 7e27d26..5281452 100644 --- a/src/hash/spookyhash.c +++ b/src/hash/spookyhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include #include diff --git a/src/i_common.c b/src/i_common.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/i_common.h b/src/i_common.h deleted file mode 100644 index 66dc56a..0000000 --- a/src/i_common.h +++ /dev/null @@ -1,2 +0,0 @@ -#define i_swap(A,B) double temp = A; A = B; B = temp; - diff --git a/src/i_str.c b/src/i_str.c deleted file mode 100644 index d77067b..0000000 --- a/src/i_str.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "i_str.h" -#include "lua.h" - -str* str_init(char* init){ - if(init == NULL){ - char cc = '\0'; - init = &cc; - } - - size_t len = strlen(init); - str* s = malloc(sizeof * s); - s->c = malloc(len + 1); - s->len = len ; - - memcpy(s->c, init, len + 1); - return s; -} - -void str_free(str* s){ - free(s->c); - return free(s); -} - -void str_push(str* s, char* insert){ - s->len += strlen(insert); - s->c = realloc(s->c, s->len + 5); - strcat(s->c, insert); -} - -void str_pushl(str* s, char* insert, size_t l){ - - s->c = realloc(s->c, s->len + l + 5); - //strcat(s->c, insert); - for(int i = 0; i != l; i++){ - s->c[i + s->len] = insert[i]; - } - s->len += l; - s->c[s->len] = '\0'; -} - -void str_clear(str* s){ - memset(s->c, 0, s->len); - - s->len = 0; -} - - diff --git a/src/i_str.h b/src/i_str.h deleted file mode 100644 index 60b0cb9..0000000 --- a/src/i_str.h +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -typedef struct { - size_t len; - size_t _bytes; //may be used in the future - char* c; -} str; - -str* str_init(char*); -void str_free(str*); -void str_push(str*, char*); -void str_pushl(str*, char*, size_t); -void str_clear(str*); diff --git a/src/i_util.c b/src/i_util.c deleted file mode 100644 index 48749d7..0000000 --- a/src/i_util.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "i_util.h" -#include -#include -#include "lua.h" - -void p_fatal(const char* m){ - fprintf(stderr, "%s[ fatal ] %s %s\n",color_red, m, color_reset); - exit(EXIT_FAILURE); -} - -void p_error(const char* m){ - fprintf(stderr, "%s[ error ]%s %s\n",color_red, color_reset, m); -} diff --git a/src/i_util.h b/src/i_util.h deleted file mode 100644 index b4b609a..0000000 --- a/src/i_util.h +++ /dev/null @@ -1,20 +0,0 @@ -#define color_black "\e[30m" -#define color_red "\e[31m" -#define color_green "\e[32m" -#define color_yellow "\e[33m" -#define color_blue "\e[34m" -#define color_magenta "\e[35m" -#define color_cyan "\e[36m" -#define color_lgray "\e[37m" -#define color_gray "\e[90m" -#define color_lred "\e[91m" -#define color_lgreen "\e[92m" -#define color_lyellow "\e[93m" -#define color_lblue "\e[94m" -#define color_lmagenta "\e[95m" -#define color_lcyan "\e[96m" -#define color_white "\e[97m" -#define color_reset "\e[0m" - -void p_fatal(const char*); -void p_error(const char*); diff --git a/src/io.c b/src/io.c index cc33a71..852d175 100644 --- a/src/io.c +++ b/src/io.c @@ -1,5 +1,5 @@ #include -#include "i_str.h" +#include "types/str.h" #include "io.h" #include "stdlib.h" #include "stdio.h" @@ -30,7 +30,7 @@ int l_readfile(lua_State* L){ char* out = calloc(sz + 1, sizeof * out); fread(out, sizeof * out, sz, fp); - + lua_pushlstring(L, out, sz); fclose(fp); diff --git a/src/lua.c b/src/lua.c index 5c60fe8..00d6205 100644 --- a/src/lua.c +++ b/src/lua.c @@ -3,8 +3,8 @@ #include "io.h" #include #include -#include "i_str.h" -#include "parray.h" +#include "types/str.h" +#include "types/parray.h" static int ii = 0; static int malloc_count = 0; diff --git a/src/net.c b/src/net.c index 4be6016..f0f9c8e 100644 --- a/src/net.c +++ b/src/net.c @@ -22,8 +22,8 @@ #include "io.h" #include "table.h" -#include "i_str.h" -#include "parray.h" +#include "types/str.h" +#include "types/parray.h" #define max_con 200 #define BUFFER_SIZE 2048 @@ -101,66 +101,52 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ return len + BUFFER_SIZE; } -int parse_header(char* buffer, int header_eof, str*** _table, int* _len){ +int parse_header(char* buffer, int header_eof, parray_t** _table){ if(header_eof == -1) return -1; char add[] = {0,0}; int lines = 3; for(int i = 0; i != header_eof; i++) lines += buffer[i] == '\n'; - str** table = malloc(sizeof ** table * (lines * 2 + 2)); - table[0] = str_init("Request");// table[1] = str_init("Post|Get"); - table[2] = str_init("Path");// table[3] = str_init("/"); - table[4] = str_init("Version");// table[5] = str_init("HTTP/1.1"); + parray_t* table = parray_init(); str* current = str_init(""); - int ins = 1; + int oi = 0; + int item = 0; for(; oi != header_eof; oi++){ - add[0] = buffer[oi]; - if(buffer[oi] == '\n') break; - if(buffer[oi] == ' '){ - table[ins] = str_init(current->c); - ins += 2; + if(buffer[oi] == ' ' || buffer[oi] == '\n'){ + if(buffer[oi] == '\n') current->c[current->len - 1] = 0; + parray_set(table, item == 0 ? "Request" : + item == 1 ? "Path" : "Version", (void*)str_init(current->c)); str_clear(current); - } else str_push(current, add); + item++; + if(buffer[oi] == '\n') break; + } else str_pushl(current, buffer + oi, 1); } - current->c[current->len - 1] = 0; - table[ins] = str_init(current->c); - str_clear(current); - int tlen = 6; int key = 1; + str* sw = NULL; for(int i = oi + 1; i != header_eof; i++){ - if(key && buffer[i]==':' || !key && buffer[i]=='\n') { - if(!key) current->c[current->len - 1] = 0; - table[tlen] = str_init(current->c); - str_clear(current); - tlen++; - i+=key; - key = !key; + if(buffer[i] == ' ' && strcmp(current->c, "") == 0) continue; + if(key && buffer[i] == ':' || !key && buffer[i] == '\n'){ + if(key){ + sw = current; + current = str_init(""); + key = 0; + } else { + if(buffer[oi] == '\n') current->c[current->len - 1] = 0; + parray_set(table, sw->c, (void*)str_init(current->c)); + str_clear(current); + sw = NULL; + key = 1; + } continue; - } - add[0] = buffer[i]; - str_push(current, add); + } else str_pushl(current, buffer + i, 1); } - table[tlen] = str_init(current->c); - tlen++; - table[tlen] = str_init("Body"); - tlen++; - table[tlen] = str_init(buffer + header_eof + 4); - tlen++; + parray_set(table, sw->c, (void*)str_init(current->c)); + parray_set(table, "Body", (void*)str_init(buffer + header_eof + 4)); str_free(current); - *_len = tlen / 2; - *_table = table; + *_table = table; return 0; -} - -int stable_key(str** table, char* target, int flen){ - for(int i = 0; i != flen * 2; i+=2){ - if(strcmp(table[i]->c,target) == 0){ - return i + 1; - } - } - return -1; -} +}s void http_build(str** _dest, int code, char* code_det, char* header_vs, char* content, size_t len){ /**dest = str_init( @@ -422,8 +408,8 @@ int l_serve(lua_State* L){ return 0; } -int file_parse(lua_State* L, char* buffer, str* content_type){ - str* boundary = str_init(""); +int file_parse(lua_State* L, char* buffer, str* content_type, char** end_buffer){ + str* boundary = str_init(""); //usually add + 2 to the length when using int state = 0; for(int i = 0; content_type->c[i] != '\0'; i++){ if(state == 2) str_pushl(boundary, content_type->c + i, 1); @@ -450,7 +436,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ int key = -1; for(char* s = ind + boundary->len + 2; s != header_eof; s++){ - if(*s == ':'){ //collapse Content-Disposition + if(*s == ':'){ //todo: collapse Content-Disposition lua_pushlstring(L, current->c, current->len); key = lua_gettop(L); str_clear(current); @@ -472,7 +458,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ lua_pushstring(L, "content"); lua_pushlstring(L, file->c, file->len); lua_settable(L, file_T); - + buffer = ending_ind; //printf("'%s'\n", buffer); str_free(file); @@ -484,6 +470,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ } str_free(boundary); + *end_buffer = buffer + boundary->len + 2; lua_pushvalue(L, base_T); return base_T; } @@ -517,21 +504,25 @@ void* handle_client(void *_arg){ int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof); //ignore if header is just fucked if(bytes_received >= -1){ - str** table; - int len = 0; + //str** table; + parray_t* table; //checks for a valid header - if(parse_header(buffer, header_eof, &table, &len) != -1){ + if(parse_header(buffer, header_eof, &table) != -1){ - int k = stable_key(table, "Path", len); - int R = stable_key(table, "Request", len); - int T = stable_key(table, "Content-Type", len); - + //int k = stable_key(table, "Path", len); + //int R = stable_key(table, "Request", len); + //int T = stable_key(table, "Content-Type", len); + + str* sk = (str*)parray_get(table, "Path"); + str* sR = (str*)parray_get(table, "Request"); + str* sT = (str*)parray_get(table, "Content-Type"); + //printf("%s\n", sk->c); char portc[10] = {0}; sprintf(portc, "%i", args->port); str* aa = str_init(portc); //if(table[k]->c[table[k]->len - 1] != '/') str_push(table[k], "/"); - str_push(aa, table[k]->c); + str_push(aa, sk->c); //if(aa->c[aa->len - 1] != '/') str_push(aa, "/"); void* v = parray_find(paths, aa->c); @@ -549,9 +540,14 @@ void* handle_client(void *_arg){ lua_newtable(L); int res_idx = lua_gettop(L); - for(int i = 0; i != len * 2; i+=2){ + char* new_cont; + int pf = file_parse(L, buffer + header_eof, sT, &new_cont); + + luaI_tsetv(L, req_idx, "files", pf); + + for(int i = 0; i != table->len; i+=1){ //printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c); - luaI_tsets(L, req_idx, table[i]->c, table[i+1]->c); + luaI_tsets(L, req_idx, table->P[i].key->c, ((str*)table->P[i].value)->c); } luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr)); @@ -563,8 +559,6 @@ void* handle_client(void *_arg){ luaI_tsetb(L, req_idx, "partial", bytes_received == -1); luaI_tseti(L, req_idx, "_bytes", bytes_received); //printf("%s\n",table[T]->c); - int pf = file_parse(L, buffer + header_eof, table[T]); - luaI_tsetv(L, req_idx, "files", pf); //functions luaI_tsetcf(L, res_idx, "send", l_send); @@ -574,7 +568,7 @@ void* handle_client(void *_arg){ //values luaI_tseti(L, res_idx, "client_fd", client_fd); - luaI_tsets(L, res_idx, "_request", table[R]->c); + luaI_tsets(L, res_idx, "_request", sR->c); //header table lua_newtable(L); @@ -595,8 +589,8 @@ void* handle_client(void *_arg){ for(int z = 0; z != awa->len; z++){ char* path; struct lchar* wowa = awa->cs[z]; - if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, table[R]->c) == 0 || - (strcmp(table[R]->c, "HEAD") && strcmp(wowa->req, "GET"))){ + if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, sR->c) == 0 || + (strcmp(sR->c, "HEAD") && strcmp(wowa->req, "GET"))){ luaI_tseti(L, res_idx, "passes", passes); passes++; @@ -624,10 +618,7 @@ void* handle_client(void *_arg){ } - for(int i = 0; i != len * 2; i++){ - str_free(table[i]); - } - free(table); + parray_clear(table, 1); } if(client_fd > 0) closesocket(client_fd); diff --git a/src/parray.c b/src/parray.c deleted file mode 100644 index 77c9771..0000000 --- a/src/parray.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include "i_str.h" -#include -#include - -#include "lua.h" -#include "parray.h" - -parray_t* parray_init(){ - parray_t* awa = malloc(sizeof * awa); - awa->P = malloc(sizeof * awa->P); - awa->len = 0; - return awa; -} - -void parray_set(parray_t* p, char* key, void* value){ - for(int i = 0; i != p->len; i++){ - if(strcmp(p->P[i].key->c, key) == 0){ - p->P[p->len - 1].value = value; - return; - } - } - - p->len++; - p->P = realloc(p->P, sizeof * p->P * (p->len + 1)); - p->P[p->len - 1].key = str_init(key); - p->P[p->len - 1].value = value; -} - -void* parray_get(parray_t* p, char* key){ - for(int i = 0; i != p->len; i++){ - if(strcmp(p->P[i].key->c, key) == 0){ - return p->P[i].value; - } - } - 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->P[i].key); - if(clear_val) free(p->P[i].value); - } - 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 deleted file mode 100644 index 3f33865..0000000 --- a/src/parray.h +++ /dev/null @@ -1,22 +0,0 @@ - -typedef struct { - void* value; - str* key; -} pelem_t; - -typedef struct { - pelem_t* P; - int len; -} parray_t; - -parray_t* parray_init(); - -void parray_set(parray_t*, char*, void*); - -void* parray_get(parray_t* , char*); - -void parray_clear(parray_t*, int); - -void parray_lclear(parray_t*); - -parray_t* parray_find(parray_t*, char*); \ No newline at end of file diff --git a/src/sort.h b/src/sort.h index 1b54164..555c812 100644 --- a/src/sort.h +++ b/src/sort.h @@ -1,6 +1,5 @@ #include "lua.h" -#include "i_util.h" -#include "i_common.h" +#include "util.h" //comparison sorts int l_quicksort(lua_State*); //[double+int] -> arr[N] (greatest -> least) diff --git a/src/table.h b/src/table.h index 5b85e9a..704c917 100644 --- a/src/table.h +++ b/src/table.h @@ -1,6 +1,5 @@ #include "lua.h" -#include "i_util.h" -#include "i_common.h" +#include "util.h" #include "sort.h" #include diff --git a/src/types/parray.c b/src/types/parray.c new file mode 100644 index 0000000..3dcc57f --- /dev/null +++ b/src/types/parray.c @@ -0,0 +1,86 @@ +#include +#include "str.h" +#include +#include + +#include "lua.h" +#include "parray.h" + +parray_t* parray_init(){ + parray_t* awa = malloc(sizeof * awa); + awa->P = malloc(sizeof * awa->P); + awa->len = 0; + return awa; +} + +void parray_set(parray_t* p, char* key, void* value){ + for(int i = 0; i != p->len; i++){ + if(strcmp(p->P[i].key->c, key) == 0){ + p->P[p->len - 1].value = value; + return; + } + } + + p->len++; + p->P = realloc(p->P, sizeof * p->P * (p->len + 1)); + p->P[p->len - 1].key = str_init(key); + p->P[p->len - 1].value = value; +} + +void* parray_get(parray_t* p, char* key){ + for(int i = 0; i != p->len; i++){ + if(strcmp(p->P[i].key->c, key) == 0){ + return p->P[i].value; + } + } + 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->P[i].key); + if(clear_val) free(p->P[i].value); + } + 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/types/parray.h b/src/types/parray.h new file mode 100644 index 0000000..3f33865 --- /dev/null +++ b/src/types/parray.h @@ -0,0 +1,22 @@ + +typedef struct { + void* value; + str* key; +} pelem_t; + +typedef struct { + pelem_t* P; + int len; +} parray_t; + +parray_t* parray_init(); + +void parray_set(parray_t*, char*, void*); + +void* parray_get(parray_t* , char*); + +void parray_clear(parray_t*, int); + +void parray_lclear(parray_t*); + +parray_t* parray_find(parray_t*, char*); \ No newline at end of file diff --git a/src/types/str.c b/src/types/str.c new file mode 100644 index 0000000..279567f --- /dev/null +++ b/src/types/str.c @@ -0,0 +1,47 @@ +#include "str.h" +#include "lua.h" + +str* str_init(char* init){ + if(init == NULL){ + char cc = '\0'; + init = &cc; + } + + size_t len = strlen(init); + str* s = malloc(sizeof * s); + s->c = malloc(len + 1); + s->len = len ; + + memcpy(s->c, init, len + 1); + return s; +} + +void str_free(str* s){ + free(s->c); + return free(s); +} + +void str_push(str* s, char* insert){ + s->len += strlen(insert); + s->c = realloc(s->c, s->len + 5); + strcat(s->c, insert); +} + +void str_pushl(str* s, char* insert, size_t l){ + + s->c = realloc(s->c, s->len + l + 5); + //strcat(s->c, insert); + for(int i = 0; i != l; i++){ + s->c[i + s->len] = insert[i]; + } + s->len += l; + s->c[s->len] = '\0'; +} + +void str_clear(str* s){ + memset(s->c, 0, s->len); + + s->len = 0; +} + + diff --git a/src/types/str.h b/src/types/str.h new file mode 100644 index 0000000..60b0cb9 --- /dev/null +++ b/src/types/str.h @@ -0,0 +1,15 @@ +#include +#include +#include + +typedef struct { + size_t len; + size_t _bytes; //may be used in the future + char* c; +} str; + +str* str_init(char*); +void str_free(str*); +void str_push(str*, char*); +void str_pushl(str*, char*, size_t); +void str_clear(str*); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..3c2e279 --- /dev/null +++ b/src/util.c @@ -0,0 +1,13 @@ +#include "util.h" +#include +#include +#include "lua.h" + +void p_fatal(const char* m){ + fprintf(stderr, "%s[ fatal ] %s %s\n",color_red, m, color_reset); + exit(EXIT_FAILURE); +} + +void p_error(const char* m){ + fprintf(stderr, "%s[ error ]%s %s\n",color_red, color_reset, m); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..9a5b295 --- /dev/null +++ b/src/util.h @@ -0,0 +1,22 @@ +#define color_black "\e[30m" +#define color_red "\e[31m" +#define color_green "\e[32m" +#define color_yellow "\e[33m" +#define color_blue "\e[34m" +#define color_magenta "\e[35m" +#define color_cyan "\e[36m" +#define color_lgray "\e[37m" +#define color_gray "\e[90m" +#define color_lred "\e[91m" +#define color_lgreen "\e[92m" +#define color_lyellow "\e[93m" +#define color_lblue "\e[94m" +#define color_lmagenta "\e[95m" +#define color_lcyan "\e[96m" +#define color_white "\e[97m" +#define color_reset "\e[0m" + +#define i_swap(A,B) double temp = A; A = B; B = temp; + +void p_fatal(const char*); +void p_error(const char*); -- cgit v1.2.3