feat(hectic C): json api

This commit is contained in:
2025-03-22 01:22:36 +00:00
parent aff923d470
commit bcaca5038d
11 changed files with 680 additions and 163 deletions

View File

@@ -10,16 +10,29 @@ stdenv.mkDerivation {
buildPhase = ''
mkdir -p target
echo "# Build library"
${gcc}/bin/cc -Wall -Wextra -g \
-pedantic -fsanitize=address hmpl.c \
-std=c99 \
-pedantic -fsanitize=address -c hmpl.c \
-lchectic -lcjson \
-o target/hmpl.o
${gcc}/bin/ar rcs target/libhmpl.a target/hmpl.o
echo "# Build app"
${gcc}/bin/cc -Wall -Wextra -g \
-pedantic -fsanitize=address main.c \
-Ltarget -lhmpl \
-lchectic -lcjson -o target/hmpl
'';
checkPhase = '' '';
installPhase = ''
mkdir -p $out/bin
mkdir -p $out/bin $out/lib $out/include
cp target/hmpl $out/bin/hmpl
cp target/libhmpl.a $out/lib/
cp hmpl.h $out/include/hmpl.h
'';
meta = {

View File

@@ -1,142 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "chectic.h"
#include "cjson/cJSON.h"
#include "hmpl.h"
static Arena arena;
Arena *cJSON_global_arena;
size_t last_size = 0; // tracked externally, unsafe but works for simple use
char *eval(cJSON *context, const char *key) {
if (!context || !key) return NULL;
char *key_copy = arena_strdup(&arena, key);
char *token, *rest = key_copy;
cJSON *res = context;
while ((token = strtok_r(rest, ".", &rest))) {
res = cJSON_GetObjectItemCaseSensitive(res, token);
if (!res)
return NULL;
}
if (cJSON_IsString(res) && res->valuestring)
return arena_strdup(&arena, res->valuestring);
else if (cJSON_IsNumber(res)) {
char buf[64];
snprintf(buf, sizeof(buf), "%g", res->valuedouble);
return arena_strdup(&arena, buf);
}
char *temp = cJSON_PrintUnformatted(res);
char *result = arena_strdup(&arena, temp);
free(temp);
return result;
void *arena_malloc(size_t size) {
void *ptr = arena_alloc(cJSON_global_arena, size);
last_size = size;
return ptr;
}
void substring(const char *src, char *dest, size_t start, size_t len) {
raise_debug("substring %s from %zu to %zu", src, start, len);
size_t srclen = strlen(src);
if (start >= srclen) {
dest[0] = '\0';
return;
}
if (start + len > srclen)
len = srclen - start;
strncpy(dest, src + start, len);
dest[len] = '\0';
void arena_free_stub(void *ptr) {
raise_debug("WARN: cJSON tried to free %p — ignored", ptr);
}
void init_cjson_with_arenas(Arena *arena) {
cJSON_global_arena = arena;
cJSON_InitHooks(&(cJSON_Hooks){
.malloc_fn = arena_malloc,
.free_fn = arena_free_stub,
});
}
char* replace_substring(const char* src, int start, int end, const char* replacement) {
raise_debug("replace_substring");
int src_len = strlen(src);
int rep_len = strlen(replacement);
int new_len = src_len - (end - start + 1) + rep_len;
char* new_str = arena_alloc(&arena, new_len + 1);
memcpy(new_str, src, start); // copy before
memcpy(new_str + start, replacement, rep_len); // insert replacement
strcpy(new_str + start + rep_len, src + end + 1); // copy after
return new_str;
char *eval(Arena *arena, const cJSON * const context, const char * const key) {
if (!context || !key) return NULL;
char *key_copy = arena_strdup(arena, key);
char *token, *rest = key_copy;
cJSON *res = context;
while ((token = strtok_r(rest, ".", &rest))) {
raise_debug("context: %s; token: %s", cJSON_Print(res), key);
res = cJSON_GetObjectItemCaseSensitive(res, token);
if (!res)
return NULL;
}
if (cJSON_IsString(res) && res->valuestring)
return arena_strdup(arena, res->valuestring);
else if (cJSON_IsNumber(res)) {
char buf[64];
snprintf(buf, sizeof(buf), "%g", res->valuedouble);
return arena_strdup(arena, buf);
}
char *temp = cJSON_PrintUnformatted(res);
char *result = arena_strdup(arena, temp);
free(temp);
return result;
}
/* Modified: text is passed by reference so we can update it and free old allocations */
void render_template_placeholders(char **text_ptr, cJSON *context, const char *prefix) {
raise_debug("render_template_placeholders");
char start_pattern[256];
snprintf(start_pattern, sizeof(start_pattern), "{{%s", prefix);
int start_pattern_length = strlen(start_pattern);
int offset = 0;
void render_template_placeholders(Arena *arena, char **text_ptr, cJSON *context, const char * const prefix) {
raise_debug("render_template_placeholders");
char start_pattern[256];
snprintf(start_pattern, sizeof(start_pattern), "{{%s", prefix);
int start_pattern_length = strlen(start_pattern);
int offset = 0;
while (1) {
char *current_text = *text_ptr;
char *placeholder_start = strstr(current_text + offset, start_pattern);
if (!placeholder_start)
break;
int start_index = placeholder_start - current_text;
int key_start = start_index + start_pattern_length;
raise_debug("start: %d", key_start);
while (1) {
char *current_text = *text_ptr;
char *placeholder_start = strstr(current_text + offset, start_pattern);
if (!placeholder_start)
break;
int start_index = placeholder_start - current_text;
int key_start = start_index + start_pattern_length;
raise_debug("start: %d", key_start);
char *placeholder_end = strstr(placeholder_start, "}}");
if (!placeholder_end)
raise_exception("Malformed template: missing closing braces for placeholder start");
int key_length = (placeholder_end - current_text) - key_start;
char *placeholder_key = arena_alloc(&arena, key_length + 1);
substring(current_text, placeholder_key, key_start, key_length);
raise_debug("key: %s", placeholder_key);
char *placeholder_end = strstr(placeholder_start, "}}");
if (!placeholder_end)
raise_exception("Malformed template: missing closing braces for placeholder start");
int key_length = (placeholder_end - current_text) - key_start;
char *placeholder_key = arena_alloc(arena, key_length + 1);
substr(current_text, placeholder_key, key_start, key_length);
raise_debug("key: %s", placeholder_key);
char *replacement = eval(context, placeholder_key);
raise_debug("%s = eval(context, %s)", replacement ? replacement : "NULL", placeholder_key);
if (!replacement) {
offset = (placeholder_end - current_text) + 2;
continue;
}
int placeholder_end_index = (placeholder_end - current_text) + 2;
char *new_text = replace_substring(current_text,
start_index,
placeholder_end_index - 1,
replacement);
char *replacement = eval(arena, context, placeholder_key);
raise_debug("%s = eval(context, %s)", replacement ? replacement : "NULL", placeholder_key);
if (!replacement) {
offset = (placeholder_end - current_text) + 2;
continue;
}
char *new_text = arena_repstr(arena, current_text,
start_index,
placeholder_end - placeholder_start + 1,
replacement);
*text_ptr = new_text;
offset = start_index;
}
*text_ptr = new_text;
offset = start_index;
}
}
void render_template(char **text, cJSON *context) {
render_template_placeholders(text, context, "");
void render_template_with_arena(Arena *arena, char **text, const cJSON * const context) {
if (!cJSON_IsObject(context)) {
raise_exception("Malformed context: context is not json");
exit(1);
}
render_template_placeholders(arena, text, context, "");
}
int main(int argc, char *argv[]) {
init_logger();
raise_info("start");
void render_template(char **text, const cJSON * const context) {
Arena arena = arena_init(1024 * 1024);
arena = arena_init(1024 * 1024);
raise_info("read the arguments");
char *text = NULL;
const char *json_input = (argc > 1 ? argv[1] : "{}");
cJSON *context = cJSON_Parse(json_input);
if (!context) {
fprintf(stderr, "Error parsing JSON\n");
return 1;
}
if (argc > 2) {
text = arena_strdup(&arena, argv[2]);
} else if (!isatty(fileno(stdin))) {
size_t size = 0;
char *heap_text = NULL;
ssize_t len = getdelim(&heap_text, &size, '\0', stdin);
if (len < 0) {
perror("read stdin");
cJSON_Delete(context);
return 1;
}
text = arena_strdup(&arena, heap_text);
free(heap_text); // free temporary heap allocation
} else {
text = arena_strdup(&arena, "");
}
render_template(&text, context);
printf("%s", text);
render_template_with_arena(&arena, text, context);
arena_free(&arena);
cJSON_Delete(context);
return 0;
}

22
package/c/hmpl/hmpl.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef EPRINTF_HMPL
#define EPRINTF_HMPL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "chectic.h"
#include "cjson/cJSON.h"
void init_cjson_with_arenas(Arena *arena);
char *eval(Arena *arena, const cJSON * const context, const char * const key);
/* Modified: text is passed by reference so we can update it and free old allocations */
void render_template_placeholders(Arena *arena, char **text_ptr, cJSON *context, const char * const prefix);
void render_template_with_arena(Arena *arena, char **text, const cJSON * const ccontext);
void render_template(char **text, const cJSON * const context);
#endif // EPRINTF_HMPL

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
#include "libhectic.h"
int main(void) {
raise_info("hello world");
return 0;
}

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
#include "libhectic.h"
int main(void) {
raise_info("hello world");
return 0;
}

51
package/c/hmpl/main.c Normal file
View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "chectic.h"
#include "cjson/cJSON.h"
#include "hmpl.h"
int main(int argc, char *argv[]) {
init_logger();
raise_info("start");
Arena arena = arena_init(1024 * 1024);
Arena arena_for_jsons = arena_init(1024 * 1024);
init_cjson_with_arenas(&arena_for_jsons);
raise_info("read the arguments");
char *text = NULL;
const char *json_input = (argc > 1 ? argv[1] : "{}");
cJSON *context = cJSON_Parse(json_input);
if (!context) {
fprintf(stderr, "Error parsing JSON\n");
return 1;
}
if (argc > 2) {
text = arena_strdup(&arena, argv[2]);
} else if (!isatty(fileno(stdin))) {
size_t size = 0;
char *heap_text = NULL;
ssize_t len = getdelim(&heap_text, &size, '\0', stdin);
if (len < 0) {
perror("read stdin");
cJSON_Delete(context);
return 1;
}
text = arena_strdup(&arena, heap_text);
free(heap_text); // free temporary heap allocation
} else {
text = arena_strdup(&arena, "");
}
render_template_with_arena(&arena, &text, context);
printf("%s", text);
arena_free(&arena);
cJSON_Delete(context);
return 0;
}