diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-19 14:36:19 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-19 14:36:19 -0300 |
commit | b81e0c2b1c70badf4131cf8a587a06e1c31df1f5 (patch) | |
tree | 1b6099808b8d762120fb5742c4edff6e5adfc198 /src | |
parent | d301477f1aba66d5f0996da7a610e703d34837c1 (diff) | |
download | blogc-b81e0c2b1c70badf4131cf8a587a06e1c31df1f5.tar.gz blogc-b81e0c2b1c70badf4131cf8a587a06e1c31df1f5.tar.bz2 blogc-b81e0c2b1c70badf4131cf8a587a06e1c31df1f5.zip |
loader: added compiler-defined variables
Diffstat (limited to 'src')
-rw-r--r-- | src/loader.c | 44 | ||||
-rw-r--r-- | src/loader.h | 1 | ||||
-rw-r--r-- | src/source-parser.c | 9 |
3 files changed, 54 insertions, 0 deletions
diff --git a/src/loader.c b/src/loader.c index 7eefa77..485fb0d 100644 --- a/src/loader.c +++ b/src/loader.c @@ -49,6 +49,42 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) } +char* +blogc_get_filename(const char *f) +{ + if (f == NULL) + return NULL; + + if (strlen(f) == 0) + return NULL; + + // keep a pointer to original string + char *filename = b_strdup(f); + char *tmp = filename; + + bool removed_dot = false; + for (int i = strlen(tmp); i >= 0 ; i--) { + + // remove last extension + if (!removed_dot && tmp[i] == '.') { + tmp[i] = '\0'; + removed_dot = true; + continue; + } + + if (tmp[i] == '/' || tmp[i] == '\\') { + tmp += i + 1; + break; + } + } + + char *final_filename = b_strdup(tmp); + free(filename); + + return final_filename; +} + + b_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err) { @@ -74,6 +110,14 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err) if (s == NULL) return NULL; b_trie_t *rv = blogc_source_parse(s, len, err); + + // set FILENAME variable + if (rv != NULL) { + char *filename = blogc_get_filename(f); + if (filename != NULL) + b_trie_insert(rv, "FILENAME", filename); + } + free(s); return rv; } diff --git a/src/loader.h b/src/loader.h index f1e416e..6407d0d 100644 --- a/src/loader.h +++ b/src/loader.h @@ -15,6 +15,7 @@ #define BLOGC_FILE_CHUNK_SIZE 1024 char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err); +char* blogc_get_filename(const char *f); b_slist_t* blogc_template_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); diff --git a/src/source-parser.c b/src/source-parser.c index b6b7fd0..322906f 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -11,6 +11,7 @@ #endif /* HAVE_CONFIG_H */ #include <stdbool.h> +#include <string.h> #include "utils/utils.h" #include "source-parser.h" @@ -70,6 +71,14 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) break; if (c == ':') { key = b_strndup(src + start, current - start); + if ((0 == strncmp("FILENAME", src + start, current - start)) || + (0 == strncmp("CONTENT", src + start, current - start))) + { + *err = blogc_error_new_printf(BLOGC_ERROR_SOURCE_PARSER, + "'%s' variable is forbidden in source files. It will " + "be set for you by the compiler.", key); + break; + } state = SOURCE_CONFIG_VALUE_START; break; } |