refactor: reorganize into subdirs (config/ util/ http/ notifs/ queue/ integrations/ static/)

This commit is contained in:
2026-06-09 00:56:27 +02:00
parent 4bcc34ec86
commit 32deda57e0
23 changed files with 33 additions and 18 deletions
+28
View File
@@ -0,0 +1,28 @@
#include "buf.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
void buf_init(Buf *b) {
b->data = malloc(4096); b->len = 0; b->cap = 4096; b->data[0] = 0;
}
void buf_free(Buf *b) {
free(b->data); b->data = NULL; b->len = 0; b->cap = 0;
}
void buf_append(Buf *b, const char *s, size_t n) {
while (b->len + n + 1 > b->cap) { b->cap *= 2; b->data = realloc(b->data, b->cap); }
memcpy(b->data + b->len, s, n);
b->len += n;
b->data[b->len] = 0;
}
void buf_str(Buf *b, const char *s) { buf_append(b, s, strlen(s)); }
void buf_fmt(Buf *b, const char *fmt, ...) {
char tmp[4096]; va_list ap; va_start(ap, fmt);
vsnprintf(tmp, sizeof(tmp), fmt, ap); va_end(ap);
buf_str(b, tmp);
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <stddef.h>
typedef struct { char *data; size_t len; size_t cap; } Buf;
void buf_init(Buf *b);
void buf_free(Buf *b);
void buf_append(Buf *b, const char *s, size_t n);
void buf_str(Buf *b, const char *s);
void buf_fmt(Buf *b, const char *fmt, ...) __attribute__((format(printf,2,3)));
+116
View File
@@ -0,0 +1,116 @@
#include "json.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *json_str(const char *json, const char *key) {
if (!json || !key) return NULL;
char needle[128]; snprintf(needle, sizeof(needle), "\"%s\"", key);
const char *p = strstr(json, needle);
if (!p) return NULL;
p += strlen(needle);
while (*p == ' ' || *p == ':') p++;
if (*p == '"') {
p++;
const char *end = p;
while (*end && !(*end == '"' && *(end-1) != '\\')) end++;
size_t len = end - p;
char *r = malloc(len+1); memcpy(r, p, len); r[len] = 0; return r;
}
const char *end = p;
while (*end && *end != ',' && *end != '}' && *end != ']') end++;
size_t len = end - p;
char *r = malloc(len+1); memcpy(r, p, len); r[len] = 0; return r;
}
char **json_array(const char *json, int *n) {
*n = 0;
if (!json) return NULL;
const char *p = json;
while (*p && *p != '[') p++;
if (!*p) return NULL;
p++;
int cap = 64;
char **arr = malloc(cap * sizeof(char*));
int depth = 0; const char *obj_start = NULL; int in_str = 0;
for (; *p; p++) {
if (*p == '"' && (p == json || *(p-1) != '\\')) { in_str = !in_str; continue; }
if (in_str) continue;
if (*p == '{' || *p == '[') { if (depth == 0) obj_start = p; depth++; }
else if (*p == '}' || *p == ']') {
depth--;
if (depth == 0 && obj_start) {
size_t len = p - obj_start + 1;
char *obj = malloc(len+1); memcpy(obj, obj_start, len); obj[len] = 0;
if (*n >= cap) { cap *= 2; arr = realloc(arr, cap*sizeof(char*)); }
arr[(*n)++] = obj;
obj_start = NULL;
if (*p == ']') break;
}
}
}
return arr;
}
void urldecode(char *s) {
char *w = s, *r = s;
while (*r) {
if (*r == '%' && r[1] && r[2]) {
char hex[3] = {r[1], r[2], 0}; *w++ = (char)strtol(hex, NULL, 16); r += 3;
} else if (*r == '+') { *w++ = ' '; r++; }
else { *w++ = *r++; }
}
*w = 0;
}
char *qparam(const char *qs, const char *key) {
if (!qs || !key) return NULL;
char needle[128]; snprintf(needle, sizeof(needle), "%s=", key);
const char *p = strstr(qs, needle);
if (!p) return NULL;
p += strlen(needle);
const char *end = strchr(p, '&');
size_t len = end ? (size_t)(end-p) : strlen(p);
char *r = malloc(len+1); memcpy(r, p, len); r[len] = 0;
urldecode(r);
return r;
}
void html_esc(Buf *b, const char *s) {
for (; *s; s++) {
if (*s == '<') buf_str(b, "&lt;");
else if (*s == '>') buf_str(b, "&gt;");
else if (*s == '&') buf_str(b, "&amp;");
else if (*s == '"') buf_str(b, "&quot;");
else buf_append(b, s, 1);
}
}
void js_esc(Buf *b, const char *s) {
for (; *s; s++) {
if (*s == '\'') buf_str(b, "\\'");
else if (*s == '\\') buf_str(b, "\\\\");
else if (*s == '\n' || *s == '\r') {}
else buf_append(b, s, 1);
}
}
void fwrite_json_str(FILE *f, const char *s) {
fputc('"', f);
for (; *s; s++) {
if (*s == '"' || *s == '\\') fputc('\\', f);
if ((unsigned char)*s >= 0x20) fputc(*s, f);
}
fputc('"', f);
}
int str_icontains(const char *hay, const char *needle) {
if (!hay || !needle || !*needle) return 1;
char h[2048], n[256];
strncpy(h, hay, sizeof(h)-1); h[sizeof(h)-1] = 0;
strncpy(n, needle, sizeof(n)-1); n[sizeof(n)-1] = 0;
for (char *p = h; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
for (char *p = n; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
return strstr(h, n) != NULL;
}
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <stdio.h>
#include "buf.h"
/* JSON micro-parser */
char *json_str(const char *json, const char *key);
char **json_array(const char *json, int *n);
/* URL helpers */
void urldecode(char *s);
char *qparam(const char *qs, const char *key);
/* Escaping */
void html_esc(Buf *b, const char *s);
void js_esc(Buf *b, const char *s);
void fwrite_json_str(FILE *f, const char *s);
/* String utils */
int str_icontains(const char *hay, const char *needle);