diff options
| -rw-r--r-- | man/blogc-pagination.7.ronn | 12 | ||||
| -rw-r--r-- | src/blogc/loader.c | 120 | ||||
| -rw-r--r-- | tests/blogc/check_loader.c | 195 | 
3 files changed, 278 insertions, 49 deletions
| diff --git a/man/blogc-pagination.7.ronn b/man/blogc-pagination.7.ronn index 4b3b4b6..cca8aba 100644 --- a/man/blogc-pagination.7.ronn +++ b/man/blogc-pagination.7.ronn @@ -30,9 +30,15 @@ files passed as arguments to it:      provided tag, instead of filtering the whole file set.    * `FILTER_REVERSE`: -    Any string, if defined, blogc(1) will list files in reverse order. This -    is always the first filter applied to the files. All the other filters will -    get the files already in the reverse order, and won't care about this. +    Boolean (1/y/yes/true/on), if set, blogc(1) will list files in reverse order. +    This is always the first filter applied to the files. All the previous filters +    will get the files already in the reverse order, and won't care about this. + +  * `FILTER_SORT`: +    Boolean (1/y/yes/true/on), if set, blogc(1) will sort files using the `DATE` +    variable provided in the files, instead of respecting the order of the +    source files provided to blogc(1). All the previous filters will get the +    files already sorted, and won't care about this.  ## TEMPLATE VARIABLES diff --git a/src/blogc/loader.c b/src/blogc/loader.c index ea427de..029acbf 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -12,12 +12,14 @@  #include <stdio.h>  #include <stdlib.h>  #include <string.h> +#include "datetime-parser.h"  #include "source-parser.h"  #include "template-parser.h"  #include "loader.h"  #include "../common/error.h"  #include "../common/file.h"  #include "../common/utils.h" +#include "../common/sort.h"  char* @@ -97,26 +99,104 @@ blogc_source_parse_from_file(const char *f, bc_error_t **err)  } +static int +sort_source(const void *a, const void *b) +{ +    const char *ca = bc_trie_lookup((bc_trie_t*) a, "c"); +    const char *cb = bc_trie_lookup((bc_trie_t*) b, "c"); + +    if (ca == NULL || cb == NULL) { +        return 0;  // wat +    } + +    return strcmp(cb, ca); +} + + +static int +sort_source_reverse(const void *a, const void *b) +{ +    return sort_source(b, a); +} + +  bc_slist_t*  blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err)  {      if (err == NULL || *err != NULL)          return NULL; -    bool reverse = bc_trie_lookup(conf, "FILTER_REVERSE"); +    bool sort = bc_str_to_bool(bc_trie_lookup(conf, "FILTER_SORT")); +      bc_slist_t* sources = NULL; +    bc_error_t *tmp_err = NULL; +    size_t with_date = 0;      for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) { -        if (reverse) { -            sources = bc_slist_prepend(sources, tmp->data); +        char *f = tmp->data; +        bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); +        if (s == NULL) { +            *err = bc_error_new_printf(BLOGC_ERROR_LOADER, +                "An error occurred while parsing source file: %s\n\n%s", +                f, tmp_err->msg); +            bc_error_free(tmp_err); +            bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free); +            return NULL;          } -        else { -            sources = bc_slist_append(sources, tmp->data); + +        const char *date = bc_trie_lookup(s, "DATE"); +        if (date != NULL) { +            with_date++; +        } + +        if (sort) { +            if (date == NULL) { +                *err = bc_error_new_printf(BLOGC_ERROR_LOADER, +                    "'FILTER_SORT' requires that 'DATE' variable is set for " +                    "every source file: %s", f); +                bc_trie_free(s); +                bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free); +                return NULL; +            } + +            char *timestamp = blogc_convert_datetime(date, "%s", &tmp_err); +            if (timestamp == NULL) { +                *err = bc_error_new_printf(BLOGC_ERROR_LOADER, +                    "An error occurred while parsing 'DATE' variable: %s" +                    "\n\n%s", f, tmp_err->msg); +                bc_error_free(tmp_err); +                bc_trie_free(s); +                bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free); +                return NULL; +            } + +            bc_trie_insert(s, "c", timestamp);          } + +        sources = bc_slist_append(sources, s);      } -    bc_error_t *tmp_err = NULL; -    bc_slist_t *rv = NULL; -    size_t with_date = 0; +    if (with_date > 0 && with_date < bc_slist_length(l)) { +        *err = bc_error_new_printf(BLOGC_ERROR_LOADER, +            "'DATE' variable provided for at least one source file, but not " +            "for all source files. It must be provided for all files."); +        bc_slist_free_full(sources, (bc_free_func_t) bc_trie_free); +        return NULL; +    } + +    bool reverse = bc_str_to_bool(bc_trie_lookup(conf, "FILTER_REVERSE")); + +    if (sort) { +        sources = bc_slist_sort(sources, reverse ? sort_source_reverse : sort_source); +    } +    else if (reverse) { +        bc_slist_t *tmp_sources = NULL; +        for (bc_slist_t *tmp = sources; tmp != NULL; tmp = tmp->next) { +            tmp_sources = bc_slist_prepend(tmp_sources, tmp->data); +        } +        bc_slist_t *tmp = sources; +        sources = tmp_sources; +        bc_slist_free(tmp); +    }      const char *filter_tag = bc_trie_lookup(conf, "FILTER_TAG");      const char *filter_page = bc_trie_lookup(conf, "FILTER_PAGE"); @@ -146,19 +226,9 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err)      size_t end = start + per_page;      size_t counter = 0; +    bc_slist_t *rv = NULL;      for (bc_slist_t *tmp = sources; tmp != NULL; tmp = tmp->next) { -        char *f = tmp->data; -        bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); -        if (s == NULL) { -            *err = bc_error_new_printf(BLOGC_ERROR_LOADER, -                "An error occurred while parsing source file: %s\n\n%s", -                f, tmp_err->msg); -            bc_error_free(tmp_err); -            tmp_err = NULL; -            bc_slist_free_full(rv, (bc_free_func_t) bc_trie_free); -            rv = NULL; -            break; -        } +        bc_trie_t *s = tmp->data;          if (filter_tag != NULL) {              const char *tags_str = bc_trie_lookup(s, "TAGS");              // if user wants to filter by tag and no tag is provided, skip it @@ -188,21 +258,11 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err)              }              counter++;          } -        if (bc_trie_lookup(s, "DATE") != NULL) -            with_date++;          rv = bc_slist_append(rv, s);      }      bc_slist_free(sources); -    if (with_date > 0 && with_date < bc_slist_length(rv)) { -        *err = bc_error_new_printf(BLOGC_ERROR_LOADER, -            "'DATE' variable provided for at least one source file, but not " -            "for all source files. It must be provided for all files.\n"); -        bc_slist_free_full(rv, (bc_free_func_t) bc_trie_free); -        rv = NULL; -    } -      bool first = true;      for (bc_slist_t *tmp = rv; tmp != NULL; tmp = tmp->next) {          bc_trie_t *s = tmp->data; diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c index f7f2024..d65f418 100644 --- a/tests/blogc/check_loader.c +++ b/tests/blogc/check_loader.c @@ -169,21 +169,52 @@ test_source_parse_from_files(void **state)  static void -test_source_parse_from_files_filter_reverse(void **state) +test_source_parse_from_files_filter_sort(void **state)  { -    will_return(__wrap_bc_file_get_contents, "bola3.txt"); +    will_return(__wrap_bc_file_get_contents, "bola1.txt");      will_return(__wrap_bc_file_get_contents, bc_strdup( -        "ASD: 789\n" -        "DATE: 2003-02-03 04:05:06\n" +        "ASD: 123\n" +        "DATE: 2001-02-02 04:05:06\n"          "--------\n"          "bola"));      will_return(__wrap_bc_file_get_contents, "bola2.txt");      will_return(__wrap_bc_file_get_contents, bc_strdup(          "ASD: 456\n" -        "DATE: 2002-02-03 04:05:06\n" -        "TAGS: bola, chunda\n" +        "DATE: 2001-02-01 04:05:06\n"          "--------\n"          "bola")); +    will_return(__wrap_bc_file_get_contents, "bola3.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 789\n" +        "DATE: 2001-02-03 04:05:06\n" +        "--------\n" +        "bola")); +    bc_error_t *err = NULL; +    bc_slist_t *s = NULL; +    s = bc_slist_append(s, bc_strdup("bola1.txt")); +    s = bc_slist_append(s, bc_strdup("bola2.txt")); +    s = bc_slist_append(s, bc_strdup("bola3.txt")); +    bc_trie_t *c = bc_trie_new(free); +    bc_trie_insert(c, "FILTER_SORT", bc_strdup("1")); +    bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); +    assert_null(err); +    assert_non_null(t); +    assert_int_equal(bc_slist_length(t), 3);  // it is enough, no need to look at the items +    assert_int_equal(bc_trie_size(c), 5); +    assert_string_equal(bc_trie_lookup(c, "FILTER_SORT"), "1"); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_FIRST"), "bola3"); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola2"); +    assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2001-02-03 04:05:06"); +    assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2001-02-01 04:05:06"); +    bc_trie_free(c); +    bc_slist_free_full(s, free); +    bc_slist_free_full(t, (bc_free_func_t) bc_trie_free); +} + + +static void +test_source_parse_from_files_filter_reverse(void **state) +{      will_return(__wrap_bc_file_get_contents, "bola1.txt");      will_return(__wrap_bc_file_get_contents, bc_strdup(          "ASD: 123\n" @@ -191,13 +222,26 @@ test_source_parse_from_files_filter_reverse(void **state)          "TAGS: chunda\n"          "--------\n"          "bola")); +    will_return(__wrap_bc_file_get_contents, "bola2.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 456\n" +        "DATE: 2002-02-03 04:05:06\n" +        "TAGS: bola, chunda\n" +        "--------\n" +        "bola")); +    will_return(__wrap_bc_file_get_contents, "bola3.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 789\n" +        "DATE: 2003-02-03 04:05:06\n" +        "--------\n" +        "bola"));      bc_error_t *err = NULL;      bc_slist_t *s = NULL;      s = bc_slist_append(s, bc_strdup("bola1.txt"));      s = bc_slist_append(s, bc_strdup("bola2.txt"));      s = bc_slist_append(s, bc_strdup("bola3.txt"));      bc_trie_t *c = bc_trie_new(free); -    bc_trie_insert(c, "FILTER_REVERSE", bc_strdup("")); +    bc_trie_insert(c, "FILTER_REVERSE", bc_strdup("1"));      bc_slist_t *t = blogc_source_parse_from_files(c, s, &err);      assert_null(err);      assert_non_null(t); @@ -207,7 +251,53 @@ test_source_parse_from_files_filter_reverse(void **state)      assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola1");      assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2003-02-03 04:05:06");      assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2001-02-03 04:05:06"); -    assert_string_equal(bc_trie_lookup(c, "FILTER_REVERSE"), ""); +    assert_string_equal(bc_trie_lookup(c, "FILTER_REVERSE"), "1"); +    bc_trie_free(c); +    bc_slist_free_full(s, free); +    bc_slist_free_full(t, (bc_free_func_t) bc_trie_free); +} + + +static void +test_source_parse_from_files_filter_sort_reverse(void **state) +{ +    will_return(__wrap_bc_file_get_contents, "bola1.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 123\n" +        "DATE: 2001-02-02 04:05:06\n" +        "--------\n" +        "bola")); +    will_return(__wrap_bc_file_get_contents, "bola2.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 456\n" +        "DATE: 2001-02-01 04:05:06\n" +        "--------\n" +        "bola")); +    will_return(__wrap_bc_file_get_contents, "bola3.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 789\n" +        "DATE: 2001-02-03 04:05:06\n" +        "--------\n" +        "bola")); +    bc_error_t *err = NULL; +    bc_slist_t *s = NULL; +    s = bc_slist_append(s, bc_strdup("bola1.txt")); +    s = bc_slist_append(s, bc_strdup("bola2.txt")); +    s = bc_slist_append(s, bc_strdup("bola3.txt")); +    bc_trie_t *c = bc_trie_new(free); +    bc_trie_insert(c, "FILTER_SORT", bc_strdup("1")); +    bc_trie_insert(c, "FILTER_REVERSE", bc_strdup("1")); +    bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); +    assert_null(err); +    assert_non_null(t); +    assert_int_equal(bc_slist_length(t), 3);  // it is enough, no need to look at the items +    assert_int_equal(bc_trie_size(c), 6); +    assert_string_equal(bc_trie_lookup(c, "FILTER_SORT"), "1"); +    assert_string_equal(bc_trie_lookup(c, "FILTER_REVERSE"), "1"); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_FIRST"), "bola2"); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola3"); +    assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2001-02-01 04:05:06"); +    assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2001-02-03 04:05:06");      bc_trie_free(c);      bc_slist_free_full(s, free);      bc_slist_free_full(t, (bc_free_func_t) bc_trie_free); @@ -496,7 +586,7 @@ test_source_parse_from_files_filter_by_page3(void **state)  static void -test_source_parse_from_files_filter_by_page_and_tag(void **state) +test_source_parse_from_files_filter_sort_and_by_page_and_tag(void **state)  {      will_return(__wrap_bc_file_get_contents, "bola1.txt");      will_return(__wrap_bc_file_get_contents, bc_strdup( @@ -555,6 +645,7 @@ test_source_parse_from_files_filter_by_page_and_tag(void **state)      s = bc_slist_append(s, bc_strdup("bola6.txt"));      s = bc_slist_append(s, bc_strdup("bola7.txt"));      bc_trie_t *c = bc_trie_new(free); +    bc_trie_insert(c, "FILTER_SORT", bc_strdup("1"));      bc_trie_insert(c, "FILTER_TAG", bc_strdup("chunda"));      bc_trie_insert(c, "FILTER_PAGE", bc_strdup("2"));      bc_trie_insert(c, "FILTER_PER_PAGE", bc_strdup("2")); @@ -562,11 +653,12 @@ test_source_parse_from_files_filter_by_page_and_tag(void **state)      assert_null(err);      assert_non_null(t);      assert_int_equal(bc_slist_length(t), 2);  // it is enough, no need to look at the items -    assert_int_equal(bc_trie_size(c), 11); -    assert_string_equal(bc_trie_lookup(c, "FILENAME_FIRST"), "bola5"); -    assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola7"); -    assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2005-02-03 04:05:06"); -    assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2007-02-03 04:05:06"); +    assert_int_equal(bc_trie_size(c), 12); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_FIRST"), "bola3"); +    assert_string_equal(bc_trie_lookup(c, "FILENAME_LAST"), "bola2"); +    assert_string_equal(bc_trie_lookup(c, "DATE_FIRST"), "2003-02-03 04:05:06"); +    assert_string_equal(bc_trie_lookup(c, "DATE_LAST"), "2002-02-03 04:05:06"); +    assert_string_equal(bc_trie_lookup(c, "FILTER_SORT"), "1");      assert_string_equal(bc_trie_lookup(c, "FILTER_TAG"), "chunda");      assert_string_equal(bc_trie_lookup(c, "FILTER_PAGE"), "2");      assert_string_equal(bc_trie_lookup(c, "FILTER_PER_PAGE"), "2"); @@ -755,7 +847,7 @@ test_source_parse_from_files_without_all_dates(void **state)      assert_int_equal(err->type, BLOGC_ERROR_LOADER);      assert_string_equal(err->msg,          "'DATE' variable provided for at least one source file, but not for " -        "all source files. It must be provided for all files.\n"); +        "all source files. It must be provided for all files.");      bc_error_free(err);      assert_int_equal(bc_trie_size(c), 0);      bc_trie_free(c); @@ -764,6 +856,73 @@ test_source_parse_from_files_without_all_dates(void **state)  static void +test_source_parse_from_files_filter_sort_without_all_dates(void **state) +{ +    will_return(__wrap_bc_file_get_contents, "bola1.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 123\n" +        "DATE: 2002-02-03 04:05:06\n" +        "--------\n" +        "bola")); +    will_return(__wrap_bc_file_get_contents, "bola2.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 456\n" +        "--------\n" +        "bola")); +    bc_error_t *err = NULL; +    bc_slist_t *s = NULL; +    s = bc_slist_append(s, bc_strdup("bola1.txt")); +    s = bc_slist_append(s, bc_strdup("bola2.txt")); +    s = bc_slist_append(s, bc_strdup("bola3.txt")); +    bc_trie_t *c = bc_trie_new(free); +    bc_trie_insert(c, "FILTER_SORT", bc_strdup("1")); +    bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); +    assert_null(t); +    assert_non_null(err); +    assert_int_equal(err->type, BLOGC_ERROR_LOADER); +    assert_string_equal(err->msg, +        "'FILTER_SORT' requires that 'DATE' variable is set for every source " +        "file: bola2.txt"); +    bc_error_free(err); +    assert_int_equal(bc_trie_size(c), 1); +    assert_string_equal(bc_trie_lookup(c, "FILTER_SORT"), "1"); +    bc_trie_free(c); +    bc_slist_free_full(s, free); +} + + +static void +test_source_parse_from_files_filter_sort_with_wrong_date(void **state) +{ +    will_return(__wrap_bc_file_get_contents, "bola1.txt"); +    will_return(__wrap_bc_file_get_contents, bc_strdup( +        "ASD: 123\n" +        "DATE: 2002-02-03 04:05:ab\n" +        "--------\n" +        "bola")); +    bc_error_t *err = NULL; +    bc_slist_t *s = NULL; +    s = bc_slist_append(s, bc_strdup("bola1.txt")); +    s = bc_slist_append(s, bc_strdup("bola2.txt")); +    s = bc_slist_append(s, bc_strdup("bola3.txt")); +    bc_trie_t *c = bc_trie_new(free); +    bc_trie_insert(c, "FILTER_SORT", bc_strdup("1")); +    bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); +    assert_null(t); +    assert_non_null(err); +    assert_int_equal(err->type, BLOGC_ERROR_LOADER); +    assert_string_equal(err->msg, +        "An error occurred while parsing 'DATE' variable: bola1.txt\n\nInvalid " +        "first digit of seconds. Found 'a', must be integer >= 0 and <= 6."); +    bc_error_free(err); +    assert_int_equal(bc_trie_size(c), 1); +    assert_string_equal(bc_trie_lookup(c, "FILTER_SORT"), "1"); +    bc_trie_free(c); +    bc_slist_free_full(s, free); +} + + +static void  test_source_parse_from_files_null(void **state)  {      bc_error_t *err = NULL; @@ -790,15 +949,19 @@ 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_sort),          unit_test(test_source_parse_from_files_filter_reverse), +        unit_test(test_source_parse_from_files_filter_sort_reverse),          unit_test(test_source_parse_from_files_filter_by_tag),          unit_test(test_source_parse_from_files_filter_by_page),          unit_test(test_source_parse_from_files_filter_by_page2),          unit_test(test_source_parse_from_files_filter_by_page3), -        unit_test(test_source_parse_from_files_filter_by_page_and_tag), +        unit_test(test_source_parse_from_files_filter_sort_and_by_page_and_tag),          unit_test(test_source_parse_from_files_filter_by_page_invalid),          unit_test(test_source_parse_from_files_filter_by_page_invalid2),          unit_test(test_source_parse_from_files_without_all_dates), +        unit_test(test_source_parse_from_files_filter_sort_without_all_dates), +        unit_test(test_source_parse_from_files_filter_sort_with_wrong_date),          unit_test(test_source_parse_from_files_null),      };      return run_tests(tests); | 
