diff options
| author | ame <[email protected]> | 2024-04-15 08:23:28 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-04-15 08:23:28 -0500 |
| commit | 89c9c2e56c8b547ee0b0e104798c609d8d307f5e (patch) | |
| tree | e864d5bb71d06320ede5a5f15158938dcc4c1912 /src/util.c | |
| parent | 7018bae05d9c04c799e715294b5c58317f21a5ac (diff) | |
msys2 + fix logging
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 138 |
1 files changed, 69 insertions, 69 deletions
@@ -1,69 +1,69 @@ -#include "util.h" -#include <stdio.h> -#include <stdlib.h> -#include <pthread.h> -#include "lua.h" -#include "net.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); - } - - if(last != NULL){ - 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, int line, const char* file, const char* function){ - fprintf(stderr, "%s[fatal] %s \n" - "\tthread: %zu/%zu\n" - "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), threads, file, function, line, color_reset); - exit(EXIT_FAILURE); -} - -void p_error(const char* m){ - fprintf(stderr, "%s[error]%s %s\n",color_red, color_reset, m); -} +#include "util.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include "lua.h"
+#include "net.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);
+ }
+
+ if(last != NULL){
+ 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, int line, const char* file, const char* function){
+ fprintf(stderr, "%s[fatal] %s \n"
+ "\tthread: %zu/%zu\n"
+ "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), threads, file, function, line, color_reset);
+ exit(EXIT_FAILURE);
+}
+
+void p_error(const char* m){
+ fprintf(stderr, "%s[error]%s %s\n",color_red, color_reset, m);
+}
|
