diff options
| author | ame <[email protected]> | 2024-02-22 10:17:40 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2024-02-22 10:17:40 -0600 |
| commit | 867a86a9ab31c0652a46759443fc98a49f91c8dd (patch) | |
| tree | f252e0c821a3c2cce56dd3180ea1c70fccd48d5a /src/types | |
| parent | 83f9dca38af329d214c7d6dd2819ec2c6621d679 (diff) | |
cleaning and switch to parray_t
Diffstat (limited to 'src/types')
| -rw-r--r-- | src/types/parray.c | 86 | ||||
| -rw-r--r-- | src/types/parray.h | 22 | ||||
| -rw-r--r-- | src/types/str.c | 47 | ||||
| -rw-r--r-- | src/types/str.h | 15 |
4 files changed, 170 insertions, 0 deletions
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 <stdio.h>
+#include "str.h"
+#include <stdlib.h>
+#include <string.h>
+
+#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 <string.h> +#include <stdio.h> +#include <stdlib.h> + +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*); |
