aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/loader.c8
-rw-r--r--src/loader.h2
-rw-r--r--src/main.c2
-rw-r--r--src/renderer.c6
-rw-r--r--src/source-parser.c29
-rw-r--r--src/source-parser.h8
-rw-r--r--src/utils/slist.c2
-rw-r--r--src/utils/trie.c2
-rw-r--r--src/utils/utils.h8
9 files changed, 23 insertions, 44 deletions
diff --git a/src/loader.c b/src/loader.c
index 35ecdcf..7eefa77 100644
--- a/src/loader.c
+++ b/src/loader.c
@@ -64,7 +64,7 @@ blogc_template_parse_from_file(const char *f, blogc_error_t **err)
}
-blogc_source_t*
+b_trie_t*
blogc_source_parse_from_file(const char *f, blogc_error_t **err)
{
if (err == NULL || *err != NULL)
@@ -73,7 +73,7 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err)
char *s = blogc_file_get_contents(f, &len, err);
if (s == NULL)
return NULL;
- blogc_source_t *rv = blogc_source_parse(s, len, err);
+ b_trie_t *rv = blogc_source_parse(s, len, err);
free(s);
return rv;
}
@@ -87,14 +87,14 @@ blogc_source_parse_from_files(b_slist_t *l, blogc_error_t **err)
for (b_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) {
char *f = tmp->data;
- blogc_source_t *s = blogc_source_parse_from_file(f, &tmp_err);
+ b_trie_t *s = blogc_source_parse_from_file(f, &tmp_err);
if (s == NULL) {
*err = blogc_error_new_printf(BLOGC_ERROR_LOADER,
"An error occurred while parsing source file: %s\n\n%s",
f, tmp_err->msg);
blogc_error_free(tmp_err);
tmp_err = NULL;
- b_slist_free_full(rv, blogc_source_free);
+ b_slist_free_full(rv, (b_free_func_t) b_trie_free);
rv = NULL;
break;
}
diff --git a/src/loader.h b/src/loader.h
index 42a0abf..f1e416e 100644
--- a/src/loader.h
+++ b/src/loader.h
@@ -16,7 +16,7 @@
char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err);
b_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err);
-blogc_source_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err);
+b_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err);
b_slist_t* blogc_source_parse_from_files(b_slist_t *l, blogc_error_t **err);
#endif /* _LOADER_H */
diff --git a/src/main.c b/src/main.c
index a2c63ed..3701cf5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -108,7 +108,7 @@ main(int argc, char **argv)
free(out);
cleanup3:
- b_slist_free_full(s, blogc_source_free);
+ b_slist_free_full(s, (b_free_func_t) b_trie_free);
cleanup2:
blogc_template_free_stmts(l);
blogc_error_free(err);
diff --git a/src/renderer.c b/src/renderer.c
index 7d20b39..ffd46c9 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -30,7 +30,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources)
b_string_t *str = b_string_new();
- blogc_source_t *tmp_source = NULL;
+ b_trie_t *tmp_source = NULL;
char *config_value = NULL;
unsigned int if_count = 0;
@@ -87,7 +87,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources)
case BLOGC_TEMPLATE_VARIABLE_STMT:
if (stmt->value != NULL && tmp_source != NULL) {
- config_value = b_trie_lookup(tmp_source->config, stmt->value);
+ config_value = b_trie_lookup(tmp_source, stmt->value);
if (config_value != NULL)
b_string_append(str, config_value);
break;
@@ -108,7 +108,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources)
case BLOGC_TEMPLATE_IF_STMT:
if (stmt->value != NULL && tmp_source != NULL) {
- if (b_trie_lookup(tmp_source->config, stmt->value) == NULL) {
+ if (b_trie_lookup(tmp_source, stmt->value) == NULL) {
if_skip = if_count;
// at this point we can just skip anything, counting the
diff --git a/src/source-parser.c b/src/source-parser.c
index 68cf9db..b6b7fd0 100644
--- a/src/source-parser.c
+++ b/src/source-parser.c
@@ -28,7 +28,7 @@ typedef enum {
} blogc_source_parser_state_t;
-blogc_source_t*
+b_trie_t*
blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
{
if (err == NULL || *err != NULL)
@@ -39,8 +39,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
char *key = NULL;
char *tmp = NULL;
- b_trie_t *config = b_trie_new(free);
- char *content = NULL;
+ b_trie_t *rv = b_trie_new(free);
blogc_source_parser_state_t state = SOURCE_START;
@@ -92,7 +91,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
case SOURCE_CONFIG_VALUE:
if (c == '\n' || c == '\r') {
tmp = b_strndup(src + start, current - start);
- b_trie_insert(config, key, b_strdup(b_str_strip(tmp)));
+ b_trie_insert(rv, key, b_strdup(b_str_strip(tmp)));
free(tmp);
free(key);
key = NULL;
@@ -119,7 +118,8 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
case SOURCE_CONTENT:
if (current == (src_len - 1))
- content = b_strndup(src + start, src_len - start);
+ b_trie_insert(rv, "CONTENT",
+ b_strndup(src + start, src_len - start));
break;
}
@@ -131,26 +131,9 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
if (*err != NULL) {
free(key);
- free(content);
- b_trie_free(config);
+ b_trie_free(rv);
return NULL;
}
- blogc_source_t *rv = b_malloc(sizeof(blogc_source_t));
- rv->config = config;
- rv->content = content;
-
return rv;
}
-
-
-void
-blogc_source_free(void *source)
-{
- if (source == NULL)
- return;
- blogc_source_t *s = source;
- free(s->content);
- b_trie_free(s->config);
- free(s);
-}
diff --git a/src/source-parser.h b/src/source-parser.h
index c816e5b..f89a390 100644
--- a/src/source-parser.h
+++ b/src/source-parser.h
@@ -13,13 +13,7 @@
#include "utils/utils.h"
#include "error.h"
-typedef struct {
- b_trie_t *config;
- char *content;
-} blogc_source_t;
-
-blogc_source_t* blogc_source_parse(const char *src, size_t src_len,
+b_trie_t* blogc_source_parse(const char *src, size_t src_len,
blogc_error_t **err);
-void blogc_source_free(void *source);
#endif /* _SOURCE_PARSER_H */
diff --git a/src/utils/slist.c b/src/utils/slist.c
index e0c1a44..a606977 100644
--- a/src/utils/slist.c
+++ b/src/utils/slist.c
@@ -32,7 +32,7 @@ b_slist_append(b_slist_t *l, void *data)
void
-b_slist_free_full(b_slist_t *l, void (*free_func)(void *ptr))
+b_slist_free_full(b_slist_t *l, b_free_func_t free_func)
{
while (l != NULL) {
b_slist_t *tmp = l->next;
diff --git a/src/utils/trie.c b/src/utils/trie.c
index d33c600..f1b77eb 100644
--- a/src/utils/trie.c
+++ b/src/utils/trie.c
@@ -13,7 +13,7 @@
b_trie_t*
-b_trie_new(void (*free_func)(void *ptr))
+b_trie_new(b_free_func_t free_func)
{
b_trie_t *trie = b_malloc(sizeof(b_trie_t));
trie->root = NULL;
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 7e61a67..03f1639 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -15,6 +15,8 @@
#define B_STRING_CHUNK_SIZE 128
+typedef void (*b_free_func_t) (void *ptr);
+
typedef struct _b_slist_t {
struct _b_slist_t *next;
void *data;
@@ -34,11 +36,11 @@ typedef struct _b_trie_node_t {
typedef struct _b_trie_t {
b_trie_node_t *root;
- void (*free_func)(void *ptr);
+ b_free_func_t free_func;
} b_trie_t;
b_slist_t* b_slist_append(b_slist_t *l, void *data);
-void b_slist_free_full(b_slist_t *l, void (*free_func)(void *ptr));
+void b_slist_free_full(b_slist_t *l, b_free_func_t free_func);
void b_slist_free(b_slist_t *l);
unsigned int b_slist_length(b_slist_t *l);
@@ -62,7 +64,7 @@ b_string_t* b_string_append(b_string_t *str, const char *suffix);
b_string_t* b_string_append_c(b_string_t *str, char c);
b_string_t* b_string_append_printf(b_string_t *str, const char *format, ...);
-b_trie_t* b_trie_new(void (*free_func)(void *ptr));
+b_trie_t* b_trie_new(b_free_func_t free_func);
void b_trie_free(b_trie_t *trie);
void b_trie_insert(b_trie_t *trie, const char *key, void *data);
void* b_trie_lookup(b_trie_t *trie, const char *key);