aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/utils.c
blob: b1ff7e1e614ab248e7016cbc7cf2018d66098445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
}