diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-22 12:55:58 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-22 12:55:58 +0530 |
commit | 18d0d7f3f62216898c76a7404f16743f4086726d (patch) | |
tree | 0299c1dbf530b163b855703ad5cacc43e6316fa2 /src | |
parent | 751068c825d4eef93aab6f8c708fe0f64cf27323 (diff) |
(engine): maintain a list of content headers
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.c | 41 | ||||
-rw-r--r-- | src/lexer.c | 4 | ||||
-rw-r--r-- | src/list.c | 2 | ||||
-rw-r--r-- | src/main.c | 1 |
4 files changed, 40 insertions, 8 deletions
diff --git a/src/engine.c b/src/engine.c index 1cb3ae7..3d46373 100644 --- a/src/engine.c +++ b/src/engine.c @@ -3,6 +3,7 @@ #include <engine.h> #include <filehandler.h> #include <lexer.h> +#include <list.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -13,6 +14,11 @@ void ingest(char **buffer) { key_match_t *match; + list_t *content_headers = list_create(sizeof(contentfor_operand_t)); + if (content_headers == NULL) { + printf("Could not create content_headers\n"); + return; + } while (true) { match = find_next_key(*buffer); @@ -29,7 +35,7 @@ ingest(char **buffer) switch (directive->type) { case INCLUDE: { - char *operand = (char *) directive->operands; + char *operand = directive->operands; char *partial_path; asprintf(&partial_path, "%s/%s/%s", DIRECTORY, PARTIALS, operand); @@ -57,12 +63,27 @@ ingest(char **buffer) break; } case CONTENTFOR: { - contentfor_operands_t *operand - = (contentfor_operands_t *) directive->operands; - printf("CONTENTFOR: %s\n", operand->key); - printf("CONTENT: %s\n", operand->content); + contentfor_operand_t *operand = directive->operands; + list_add(content_headers, operand); + + /* printf("CONTENTFOR: %s\n", operand->key); */ + /* printf("CONTENT: %s\n", operand->content); */ + + /* printf("CONTENT: %.*s\n", operand->length, *buffer + match->offset); + */ + + char *temp_buffer; + asprintf(&temp_buffer, "%s", *buffer); - return; + free(*buffer); + asprintf(buffer, + "%.*s%s", + match->offset, + temp_buffer, + temp_buffer + operand->length); + + free(temp_buffer); + /* free(operand); */ break; } @@ -77,4 +98,12 @@ ingest(char **buffer) if (match != NULL) free(match); } + + for (size_t i = 0; i < content_headers->size; i++) { + contentfor_operand_t *op = list_get(content_headers, i); + free(op->content); + free(op->key); + } + + list_delete(content_headers); } diff --git a/src/lexer.c b/src/lexer.c index 8a04df1..1b8cb04 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -82,7 +82,7 @@ found_start: directive->operands = NULL; } else if (strncmp(buffer + n, "contentfor", strlen("contentfor")) == 0) { directive->type = CONTENTFOR; - contentfor_operands_t *operands = malloc(sizeof(contentfor_operands_t)); + contentfor_operand_t *operands = malloc(sizeof(contentfor_operand_t)); for (size_t i = n + strlen("contentfor"); i < match->length; i++) if (isalnum(buffer[i])) { @@ -125,6 +125,8 @@ found_start: } asprintf(&operands->content, "%.*s", new_match->offset, buffer); + operands->length = match->offset + match->length + new_match->offset + + new_match->length; free(new_directive); free(new_match); @@ -15,7 +15,7 @@ list_create(size_t element_size) list->element_size = element_size; list->size = 0; list->max = START_SIZE; - list->elements = (uint8_t *) calloc(list->max, element_size); + list->elements = (uint8_t *) calloc(list->max, list->element_size); return list; } @@ -117,6 +117,7 @@ main(int argc, char **argv) for (x = (char **) html_resources; *x != NULL; x++) { asprintf(&filepath, "%s.html", *x); + printf("HANDLING: %s\n", filepath); handle_file(filepath); free(filepath); } |