diff options
-rw-r--r-- | src/loader.c | 44 | ||||
-rw-r--r-- | tests/check_loader.c | 47 |
2 files changed, 82 insertions, 9 deletions
diff --git a/src/loader.c b/src/loader.c index 5a521e3..a48c9dc 100644 --- a/src/loader.c +++ b/src/loader.c @@ -99,9 +99,10 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) { blogc_error_t *tmp_err = NULL; b_slist_t *rv = NULL; - bool first = true; unsigned int with_date = 0; + const char *filter_tag = b_trie_lookup(conf, "FILTER_TAG"); + for (b_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) { char *f = tmp->data; b_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); @@ -115,8 +116,39 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) rv = NULL; break; } - if (b_trie_lookup(s, "DATE")) + if (filter_tag != NULL) { + const char *tags_str = b_trie_lookup(s, "TAGS"); + // if user wants to filter by tag and no tag is provided, skip it + if (tags_str == NULL) { + b_trie_free(s); + continue; + } + char **tags = b_str_split(tags_str, ',', 0); + bool found = false; + for (unsigned int i = 0; tags[i] != NULL; i++) + if (0 == strcmp(b_str_strip(tags[i]), filter_tag)) + found = true; + b_strv_free(tags); + if (!found) { + b_trie_free(s); + continue; + } + } + if (b_trie_lookup(s, "DATE") != NULL) with_date++; + rv = b_slist_append(rv, s); + } + + if (with_date > 0 && with_date < b_slist_length(rv)) + // fatal error, maybe? + blogc_fprintf(stderr, + "blogc: warning: 'DATE' variable provided for at least one source " + "file, but not for all source files. This means that you may get " + "wrong values for 'DATE_FIRST' and 'DATE_LAST' variables.\n"); + + bool first = true; + for (b_slist_t *tmp = rv; tmp != NULL; tmp = tmp->next) { + b_trie_t *s = tmp->data; if (first) { const char *val = b_trie_lookup(s, "DATE"); if (val != NULL) @@ -134,13 +166,7 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) if (val != NULL) b_trie_insert(conf, "FILENAME_LAST", b_strdup(val)); } - rv = b_slist_append(rv, s); } - if (with_date > 0 && with_date < b_slist_length(l)) - // fatal error, maybe? - blogc_fprintf(stderr, - "blogc: warning: 'DATE' variable provided for at least one source " - "file, but not for all source files. This means that you may get " - "wrong values for 'DATE_FIRST' and 'DATE_LAST' variables.\n"); + return rv; } diff --git a/tests/check_loader.c b/tests/check_loader.c index 506bf4f..f570297 100644 --- a/tests/check_loader.c +++ b/tests/check_loader.c @@ -177,6 +177,52 @@ test_source_parse_from_files(void **state) static void +test_source_parse_from_files_filter_by_tag(void **state) +{ + will_return(__wrap_blogc_file_get_contents, "bola1.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 123\n" + "DATE: 2001-02-03 04:05:06\n" + "TAGS: chunda\n" + "--------\n" + "bola")); + will_return(__wrap_blogc_file_get_contents, "bola2.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 456\n" + "DATE: 2002-02-03 04:05:06\n" + "TAGS: bola, chunda\n" + "--------\n" + "bola")); + will_return(__wrap_blogc_file_get_contents, "bola3.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 789\n" + "DATE: 2003-02-03 04:05:06\n" + "--------\n" + "bola")); + blogc_error_t *err = NULL; + b_slist_t *s = NULL; + s = b_slist_append(s, b_strdup("bola1.txt")); + s = b_slist_append(s, b_strdup("bola2.txt")); + s = b_slist_append(s, b_strdup("bola3.txt")); + b_trie_t *c = b_trie_new(free); + b_trie_insert(c, "FILTER_TAG", b_strdup("chunda")); + b_slist_t *t = blogc_source_parse_from_files(c, s, &err); + assert_null(err); + assert_non_null(t); + assert_int_equal(b_slist_length(t), 2); // it is enough, no need to look at the items + assert_int_equal(b_trie_size(c), 5); + assert_string_equal(b_trie_lookup(c, "FILENAME_FIRST"), "bola1"); + assert_string_equal(b_trie_lookup(c, "FILENAME_LAST"), "bola2"); + assert_string_equal(b_trie_lookup(c, "DATE_FIRST"), "2001-02-03 04:05:06"); + assert_string_equal(b_trie_lookup(c, "DATE_LAST"), "2002-02-03 04:05:06"); + assert_string_equal(b_trie_lookup(c, "FILTER_TAG"), "chunda"); + b_trie_free(c); + b_slist_free_full(s, free); + b_slist_free_full(t, (b_free_func_t) b_trie_free); +} + + +static void test_source_parse_from_files_without_all_dates(void **state) { will_return(__wrap_blogc_fprintf, stderr); @@ -248,6 +294,7 @@ main(void) unit_test(test_source_parse_from_file), unit_test(test_source_parse_from_file_null), unit_test(test_source_parse_from_files), + unit_test(test_source_parse_from_files_filter_by_tag), unit_test(test_source_parse_from_files_without_all_dates), unit_test(test_source_parse_from_files_null), }; |