aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-26 12:04:08 -0600
committerame <[email protected]>2024-02-26 12:04:08 -0600
commitd6eff6de364890153baa1c7d68d8b72c27df1b9b (patch)
tree2d857a0d8da93dec15d20bfc8b604a588edc33b2 /src/util.c
parentea535b120a4ff426b0d5a8458bde52bf2f049937 (diff)
content-disposition
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 3c2e279..4ba0476 100644
--- a/src/util.c
+++ b/src/util.c
@@ -3,6 +3,55 @@
#include <stdlib.h>
#include "lua.h"
+int gen_parse(char* inp, int len, parray_t** _table){
+ str* current = str_init(""), *last = NULL;
+ int state = 0;
+
+ parray_t* table = *_table;
+ for(int i = 0; i < len; i++){
+
+ if(state != 1 && inp[i] == ';'){
+ parray_set(table, last->c, (void*)current);
+ str_free(last);
+ last = NULL;
+ current = str_init("");
+ state = 0;
+ } else if(state != 1 && inp[i] == '='){
+ last = current;
+ current = str_init("");
+ if(inp[i+1] == '"'){
+ state = 1;
+ i++;
+ }
+ } else if(state == 1 && inp[i] == '"'){
+ state = 0;
+ } else if(current->c[0] != '\0' || inp[i] != ' ') str_pushl(current, inp + i, 1);
+ }
+ parray_set(table, last->c, (void*)current);
+ str_free(last);
+ *_table = table;
+ return 1;
+}
+
+char* strnstr(const char *s1, const char *s2, size_t n) {
+ // simplistic algorithm with O(n2) worst case, stolen from stack overflow
+ size_t i, len;
+ char c = *s2;
+
+ if(c == '\0')
+ return (char *)s1;
+
+ for(len = strlen(s2); len <= n; n--, s1++){
+ if(*s1 == c){
+ for(i = 1;; i++){
+ if(i == len) return (char *)s1;
+ if(s1[i] != s2[i]) break;
+ }
+ }
+ }
+ return NULL;
+}
+
void p_fatal(const char* m){
fprintf(stderr, "%s[ fatal ] %s %s\n",color_red, m, color_reset);
exit(EXIT_FAILURE);