diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-26 18:25:04 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-26 18:25:53 +0530 |
commit | 9376c06ded47c4b77b94cb4d1e628537d9d69fce (patch) | |
tree | 25297ed59e75133ff6ee410716606a103f88644b | |
parent | a3b18ea4019f44185a495afd4be66350f217c144 (diff) |
config,main,list: get resources from config.cfg instead of a compiled config.h
-rw-r--r-- | config.cfg | 6 | ||||
-rw-r--r-- | config.h | 8 | ||||
-rw-r--r-- | include/copy.h | 3 | ||||
-rw-r--r-- | include/engine.h | 2 | ||||
-rw-r--r-- | include/list.h | 7 | ||||
-rw-r--r-- | include/template.h | 2 | ||||
-rw-r--r-- | src/config.c | 31 | ||||
-rw-r--r-- | src/engine.c | 1 | ||||
-rw-r--r-- | src/list.c | 15 | ||||
-rw-r--r-- | src/main.c | 35 | ||||
-rw-r--r-- | src/template.c | 1 |
11 files changed, 83 insertions, 28 deletions
diff --git a/config.cfg b/config.cfg new file mode 100644 index 0000000..c785a93 --- /dev/null +++ b/config.cfg @@ -0,0 +1,6 @@ +resources = { + index.html, + projects.html, + posts/a.html, + posts/b.md +} diff --git a/config.h b/config.h deleted file mode 100644 index f77fbed..0000000 --- a/config.h +++ /dev/null @@ -1,8 +0,0 @@ -#define DIRECTORY "compromyse.xyz" -#define PARTIALS "partials" -#define ASSETS "assets" -#define OUTPUT "dist" -#define BASE_TEMPLATE "base.html" - -static const char *html_resources[] = { "index", "projects", "posts/a", NULL }; -static const char *md_resources[] = { "posts/b", NULL }; diff --git a/include/copy.h b/include/copy.h index b334b12..80fc5fb 100644 --- a/include/copy.h +++ b/include/copy.h @@ -4,6 +4,9 @@ #include <ftw.h> #include <sys/stat.h> +#define OUTPUT "dist" +#define DIRECTORY "compromyse.xyz" + typedef struct FTW FTW; int copy_recursively(const char *fpath, diff --git a/include/engine.h b/include/engine.h index d65c771..7719528 100644 --- a/include/engine.h +++ b/include/engine.h @@ -4,6 +4,8 @@ #include <lexer.h> #include <list.h> +#define PARTIALS "partials" + list_t *ingest(char **buffer); void handle_include(char **buffer, key_match_t *match, directive_t *directive); void handle_contentfor(char **buffer, diff --git a/include/list.h b/include/list.h index 4447884..b6342d1 100644 --- a/include/list.h +++ b/include/list.h @@ -14,9 +14,16 @@ typedef struct { uint8_t *elements; } list_t; +typedef struct { + void *ptr; +} ptr_wrapper_t; + list_t *list_create(size_t element_size); void list_add(list_t *list, void *element); void *list_get(list_t *list, size_t i); void list_delete(list_t *list); +void *list_find_corresponding_value_from_ptr_wrapper(list_t *keys, + list_t *values, + char *key); #endif diff --git a/include/template.h b/include/template.h index dbeb897..f0fdac6 100644 --- a/include/template.h +++ b/include/template.h @@ -5,6 +5,8 @@ #include <stdbool.h> #include <stdio.h> +#define BASE_TEMPLATE "base.html" + typedef struct { list_t *components; } template_t; diff --git a/src/config.c b/src/config.c index c06f70b..ce2c7b0 100644 --- a/src/config.c +++ b/src/config.c @@ -10,10 +10,12 @@ config_t * config_parse(char *content) { - list_t *keys = list_create(sizeof(char *)); - list_t *values = list_create(sizeof(char *)); + list_t *keys = list_create(sizeof(ptr_wrapper_t)); + list_t *values = list_create(sizeof(ptr_wrapper_t)); list_t *array_values = list_create(sizeof(list_t)); + ptr_wrapper_t *wrapper; + char *buffer = strdup(content); /* For free() */ char *x = buffer; @@ -23,26 +25,39 @@ config_parse(char *content) while (buffer != NULL) { buffer = ltrim(buffer); - list_add(keys, key); + wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = strdup(key); + list_add(keys, wrapper); if (*buffer == '{') { buffer++; - list_t *l = list_create(sizeof(char *)); + list_t *l = list_create(sizeof(ptr_wrapper_t)); char *raw_array = strsep(&buffer, "}"); char *value = strsep(&raw_array, DELIM_ARRAY); while (value != NULL) { - list_add(l, trim(value)); + wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = strdup(trim(value)); + list_add(l, wrapper); + value = strsep(&raw_array, DELIM_ARRAY); } list_add(array_values, l); - list_add(values, "\0"); + + wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = NULL; + list_add(values, wrapper); } else { char *value = trim(strsep(&buffer, "\n")); - list_add(array_values, &((list_t) { 0 })); - list_add(values, value); + wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = NULL; + list_add(array_values, wrapper); + + wrapper = malloc(sizeof(ptr_wrapper_t)); + wrapper->ptr = strdup(value); + list_add(values, wrapper); } key = trim(strsep(&buffer, DELIM)); diff --git a/src/engine.c b/src/engine.c index f47369a..61f20ff 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,5 +1,6 @@ #define _GNU_SOURCE +#include <copy.h> #include <engine.h> #include <filehandler.h> #include <lexer.h> @@ -55,3 +55,18 @@ list_delete(list_t *list) free(list->elements); free(list); } + +void * +list_find_corresponding_value_from_ptr_wrapper(list_t *keys, + list_t *values, + char *key) +{ + for (size_t i = 0; i < keys->size; i++) { + ptr_wrapper_t *wrapper = list_get(keys, i); + if (strcmp(wrapper->ptr, key) == 0) { + return list_get(values, i); + } + } + + return NULL; +} @@ -1,5 +1,6 @@ #define _GNU_SOURCE +#include <config.h> #include <copy.h> #include <engine.h> #include <errno.h> @@ -17,6 +18,8 @@ #include "../config.h" +#define ASSETS "assets" + template_t *base_template; void @@ -93,6 +96,14 @@ main(int argc, char **argv) (void) argc; (void) argv; + FILE *f = fopen("config.cfg", "r"); + size_t s = fsize(f); + char *content = fcontent(f, s); + fclose(f); + + config_t *config = config_parse(content); + free(content); + struct stat sb; if (stat(DIRECTORY, &sb) != 0 || !S_ISDIR(sb.st_mode)) { printf("%s does not exist.\n", DIRECTORY); @@ -110,22 +121,22 @@ main(int argc, char **argv) nftw( DIRECTORY "/" ASSETS, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL); - char **x; - char *filepath; + list_t *resources = list_find_corresponding_value_from_ptr_wrapper( + config->keys, config->array_values, "resources"); - for (x = (char **) html_resources; *x != NULL; x++) { - asprintf(&filepath, "%s.html", *x); - printf("HANDLING: %s\n", filepath); - handle_file(filepath); - free(filepath); + if (resources == NULL) { + printf("Could not find resources in config.cfg\n"); + return EXIT_FAILURE; } - for (x = (char **) md_resources; *x != NULL; x++) { - asprintf(&filepath, "%s.md", *x); - printf("HANDLING: %s\n", filepath); - handle_file(filepath); - free(filepath); + for (size_t i = 0; i < resources->size; i++) { + ptr_wrapper_t *value = list_get(resources, i); + char *path = value->ptr; + printf("HANDLING: %s\n", path); + handle_file(path); } + free(config); + return EXIT_SUCCESS; } diff --git a/src/template.c b/src/template.c index 9083463..7a43218 100644 --- a/src/template.c +++ b/src/template.c @@ -1,5 +1,6 @@ #define _GNU_SOURCE +#include <copy.h> #include <engine.h> #include <filehandler.h> #include <lexer.h> |