diff options
| author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2018-07-24 23:08:52 +0200 | 
|---|---|---|
| committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2018-07-25 22:28:41 +0200 | 
| commit | 1d6a1510c20794393cc45a71334724aee5bef473 (patch) | |
| tree | a39f1f9ab5b9a3600c05fdc1d1200b933ebc9805 /src/blogc-make/utils.c | |
| parent | c16c86bcc01dff2a984aeffd58e2402a876cc12d (diff) | |
| download | blogc-1d6a1510c20794393cc45a71334724aee5bef473.tar.gz blogc-1d6a1510c20794393cc45a71334724aee5bef473.tar.bz2 blogc-1d6a1510c20794393cc45a71334724aee5bef473.zip | |
make: added utilitary function to generate filenames
Diffstat (limited to 'src/blogc-make/utils.c')
| -rw-r--r-- | src/blogc-make/utils.c | 61 | 
1 files changed, 61 insertions, 0 deletions
| diff --git a/src/blogc-make/utils.c b/src/blogc-make/utils.c new file mode 100644 index 0000000..b1ff7e1 --- /dev/null +++ b/src/blogc-make/utils.c @@ -0,0 +1,61 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdbool.h> +#include <string.h> +#include "../common/utils.h" + + +char* +bm_generate_filename(const char *dir, const char *prefix, const char *fname, +    const char *ext) +{ +    bool have_prefix = prefix != NULL && prefix[0] != '\0'; +    bool have_fname = fname != NULL && fname[0] != '\0'; +    bool have_ext = ext != NULL && ext[0] != '\0'; +    bool have_ext_noslash = have_ext && ext[0] != '/'; +    bool is_index = have_fname && have_ext && ( +        (0 == strcmp(fname, "index")) && ext[0] == '/'); + +    bc_string_t *rv = bc_string_new(); + +    if (dir != NULL && (have_prefix || have_fname || have_ext)) +        bc_string_append(rv, dir); + +    if ((have_prefix || have_fname || have_ext_noslash) && !is_index) +        bc_string_append_c(rv, '/'); + +    if (have_prefix) +        bc_string_append(rv, prefix); + +    // with fname we have posts, pages and tags +    if (have_fname) { +        if (have_prefix && have_fname && fname[0] != '/') +            bc_string_append_c(rv, '/'); +        if (!is_index) +            bc_string_append(rv, fname); +    } + +    // no fname means index +    else if (have_ext_noslash) { +        if (have_fname) +            bc_string_append_c(rv, '/'); +        if (!have_prefix) +            bc_string_append(rv, "index"); +    } + +    if (have_ext) +        bc_string_append(rv, ext); + +    if (rv->len == 0) { +        bc_string_free(rv, true); +        return NULL; +    } + +    return bc_string_free(rv, false); +} | 
