diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/lexer.h | 3 | ||||
-rw-r--r-- | include/list.h | 4 | ||||
-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 |
7 files changed, 45 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 602ef5c..397f273 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(SRC src/engine.c src/filehandler.c src/lexer.c + src/list.c src/main.c src/template.c ) diff --git a/include/lexer.h b/include/lexer.h index 88ed4df..2a03cec 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -16,7 +16,8 @@ typedef struct { typedef struct { char *key; char *content; -} contentfor_operands_t; + unsigned int length; +} contentfor_operand_t; directive_t *find_directive(char *content, key_match_t *match); key_match_t *find_next_key(char *buffer); diff --git a/include/list.h b/include/list.h index e8df51c..4447884 100644 --- a/include/list.h +++ b/include/list.h @@ -4,8 +4,8 @@ #include <stddef.h> #include <stdint.h> -#define START_SIZE 100 -#define INCREMENT_BY 25 +#define START_SIZE 10 +#define INCREMENT_BY 5 typedef struct { size_t element_size; 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); } |