aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-08-08 00:10:48 -0400
committerfrosty <gabriel@bwaaa.monster>2026-08-08 00:10:48 -0400
commitcc04a6cada199649acb3c32fed9c99cb0134c57f (patch)
treed0875df33739143d96c3aa857a40047d8c7ac619
parent5348faebaab97373d758588bc89d13ea80c314f7 (diff)
downloadbeaker-cc04a6cada199649acb3c32fed9c99cb0134c57f.tar.gz
fix: centralise context cleanup
-rw-r--r--src/template.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/src/template.c b/src/template.c
index cf0e8ea..1c5f655 100644
--- a/src/template.c
+++ b/src/template.c
@@ -65,6 +65,23 @@ static void free_string_2d_array_var(ContextVar *var) {
var->value.string_2d_array_data.outer_count = 0;
}
+static void free_context_var(ContextVar *var) {
+ if (var == NULL) {
+ return;
+ }
+
+ switch (var->type) {
+ case CONTEXT_TYPE_STRING:
+ break;
+ case CONTEXT_TYPE_STRING_ARRAY:
+ free_string_array_var(var);
+ break;
+ case CONTEXT_TYPE_STRING_2D_ARRAY:
+ free_string_2d_array_var(var);
+ break;
+ }
+}
+
TemplateContext new_context() {
TemplateContext ctx;
ctx.count = 0;
@@ -80,12 +97,7 @@ void context_set(TemplateContext *ctx, const char *key, const char *value) {
ContextVar *var = find_context_var(ctx, key);
if (var != NULL) {
-
- if (var->type == CONTEXT_TYPE_STRING_ARRAY) {
- free_string_array_var(var);
- } else if (var->type == CONTEXT_TYPE_STRING_2D_ARRAY) {
- free_string_2d_array_var(var);
- }
+ free_context_var(var);
strncpy(var->value.string_val, value, MAX_VALUE_LEN - 1);
var->value.string_val[MAX_VALUE_LEN - 1] = '\0';
@@ -126,12 +138,7 @@ void context_set_string_array(TemplateContext *ctx, const char *key,
ContextVar *var = find_context_var(ctx, key);
if (var != NULL) {
-
- if (var->type == CONTEXT_TYPE_STRING_ARRAY) {
- free_string_array_var(var);
- } else if (var->type == CONTEXT_TYPE_STRING_2D_ARRAY) {
- free_string_2d_array_var(var);
- }
+ free_context_var(var);
} else {
if (ctx->count < MAX_CONTEXT_VARS) {
@@ -204,12 +211,7 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key,
ContextVar *var = find_context_var(ctx, key);
if (var != NULL) {
-
- if (var->type == CONTEXT_TYPE_STRING_ARRAY) {
- free_string_array_var(var);
- } else if (var->type == CONTEXT_TYPE_STRING_2D_ARRAY) {
- free_string_2d_array_var(var);
- }
+ free_context_var(var);
} else {
if (ctx->count < MAX_CONTEXT_VARS) {
@@ -343,11 +345,7 @@ void free_context(TemplateContext *ctx) {
return;
}
for (int i = 0; i < ctx->count; i++) {
- if (ctx->vars[i].type == CONTEXT_TYPE_STRING_ARRAY) {
- free_string_array_var(&ctx->vars[i]);
- } else if (ctx->vars[i].type == CONTEXT_TYPE_STRING_2D_ARRAY) {
- free_string_2d_array_var(&ctx->vars[i]);
- }
+ free_context_var(&ctx->vars[i]);
}
ctx->count = 0;
}
@@ -1501,4 +1499,4 @@ char *render_template(const char *template_file, TemplateContext *ctx) {
char *rendered_html = render_template_segment(template_content, ctx);
free(template_content);
return rendered_html;
-} \ No newline at end of file
+}