aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/utils.c
blob: f2bd7776edecd3a5352b646f044da05edf0ff481 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <squareball.h>

#include "utils.h"

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif


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] == '/') && !have_prefix;

    sb_string_t *rv = sb_string_new();

    if (dir != NULL && (have_prefix || have_fname || have_ext))
        sb_string_append(rv, dir);

    if ((have_prefix || have_fname || have_ext_noslash) && !is_index)
        sb_string_append_c(rv, '/');

    if (have_prefix)
        sb_string_append(rv, prefix);

    // with fname we have posts, pages and tags
    if (have_fname) {
        if (have_prefix && have_fname && fname[0] != '/')
            sb_string_append_c(rv, '/');
        if (!is_index)
            sb_string_append(rv, fname);
    }

    // no fname means index
    else if (have_ext_noslash) {
        if (have_fname)
            sb_string_append_c(rv, '/');
        if (!have_prefix)
            sb_string_append(rv, "index");
    }

    if (have_ext)
        sb_string_append(rv, ext);

    if (rv->len == 0) {
        sb_string_free(rv, true);
        return NULL;
    }

    return sb_string_free(rv, false);
}


char*
bm_generate_filename2(const char *dir, const char *prefix, const char *fname,
    const char *prefix2, const char *fname2, const char *ext)
{
    bool have_prefix = prefix != NULL && prefix[0] != '\0';
    bool have_fname = fname != NULL && fname[0] != '\0';
    bool have_prefix2 = prefix2 != NULL && prefix2[0] != '\0';

    sb_string_t *p = sb_string_new();

    if (have_prefix)
        sb_string_append(p, prefix);

    if (have_prefix && (have_fname || have_prefix2))
        sb_string_append_c(p, '/');

    if (have_fname)
        sb_string_append(p, fname);

    if (have_fname && have_prefix2)
        sb_string_append_c(p, '/');

    if (have_prefix2)
        sb_string_append(p, prefix2);

    char *rv = bm_generate_filename(dir, p->str, fname2, ext);
    sb_string_free(p, true);

    return rv;
}


char*
bm_abspath(const char *path, sb_error_t **err)
{
    if (err == NULL || *err != NULL)
        return NULL;

    if (path[0] == '/') {
        return sb_strdup(path);
    }

    char cwd[PATH_MAX];
    if (NULL == getcwd(cwd, sizeof(cwd))) {
        *err = sb_strerror_new_printf(
            "utils: Failed to detect absolute path (%s): %s", path, strerror(errno));
        return NULL;
    }

    if (cwd[0] != '/') {
        *err = sb_strerror_new_printf(
            "utils: Failed to get current working directory: %s", cwd);
        return NULL;
    }

    return sb_strdup_printf("%s/%s", cwd, path);
}