From b81e0c2b1c70badf4131cf8a587a06e1c31df1f5 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 19 Apr 2015 14:36:19 -0300 Subject: loader: added compiler-defined variables --- .gitignore | 1 + Makefile.am | 18 ++++++++++++++++ src/loader.c | 44 +++++++++++++++++++++++++++++++++++++++ src/loader.h | 1 + src/source-parser.c | 9 ++++++++ tests/check_loader.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 tests/check_loader.c diff --git a/.gitignore b/.gitignore index 3ac375c..64014b2 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ Makefile.in /blogc # tests +/tests/check_loader /tests/check_source_parser /tests/check_template_parser /tests/check_utils diff --git a/Makefile.am b/Makefile.am index 5a4fb15..a6539a7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,11 +83,29 @@ blogc_LDADD = \ if USE_CMOCKA check_PROGRAMS += \ + tests/check_loader \ tests/check_source_parser \ tests/check_template_parser \ tests/check_utils \ $(NULL) +tests_check_loader_SOURCES = \ + tests/check_loader.c \ + $(NULL) + +tests_check_loader_CFLAGS = \ + $(CMOCKA_CFLAGS) \ + $(NULL) + +tests_check_loader_LDFLAGS = \ + -no-install \ + $(NULL) + +tests_check_loader_LDADD = \ + $(CMOCKA_LIBS) \ + libblogc.la \ + $(NULL) + tests_check_source_parser_SOURCES = \ tests/check_source_parser.c \ $(NULL) 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 +#include #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; } diff --git a/tests/check_loader.c b/tests/check_loader.c new file mode 100644 index 0000000..c9f1432 --- /dev/null +++ b/tests/check_loader.c @@ -0,0 +1,58 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file COPYING. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include +#include +#include "../src/loader.h" + + +static void +test_get_filename(void **state) +{ + char *f = blogc_get_filename("/home/foo/asd/bola.txt"); + assert_string_equal(f, "bola"); + free(f); + f = blogc_get_filename("/home/foo/asd/bola.guda.txt"); + assert_string_equal(f, "bola.guda"); + free(f); + f = blogc_get_filename("bola.txt"); + assert_string_equal(f, "bola"); + free(f); + f = blogc_get_filename("bola.guda.txt"); + assert_string_equal(f, "bola.guda"); + free(f); + f = blogc_get_filename("/home/foo/asd/bola"); + assert_string_equal(f, "bola"); + free(f); + f = blogc_get_filename("bola"); + assert_string_equal(f, "bola"); + free(f); + f = blogc_get_filename(""); + assert_null(f); + free(f); + f = blogc_get_filename(NULL); + assert_null(f); + free(f); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_get_filename), + }; + return run_tests(tests); +} -- cgit v1.2.3-18-g5258