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
130
131
132
133
134
135
136
|
/*
* blogc: A blog compiler.
* Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file COPYING.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include "utils/utils.h"
#include "file.h"
#include "source-parser.h"
#include "template-parser.h"
#include "loader.h"
#include "error.h"
char*
blogc_get_filename(const char *f)
{
if (f == NULL)
return NULL;
if (strlen(f) == 0)
return NULL;
// keep a pointer to original string
char *filename = b_strdup(f);
char *tmp = filename;
bool removed_dot = false;
for (int i = strlen(tmp); i >= 0 ; i--) {
// remove last extension
if (!removed_dot && tmp[i] == '.') {
tmp[i] = '\0';
removed_dot = true;
continue;
}
if (tmp[i] == '/' || tmp[i] == '\\') {
tmp += i + 1;
break;
}
}
char *final_filename = b_strdup(tmp);
free(filename);
return final_filename;
}
b_slist_t*
blogc_template_parse_from_file(const char *f, blogc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
size_t len;
char *s = blogc_file_get_contents(f, &len, err);
if (s == NULL)
return NULL;
b_slist_t *rv = blogc_template_parse(s, len, err);
free(s);
return rv;
}
b_trie_t*
blogc_source_parse_from_file(const char *f, blogc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
size_t len;
char *s = blogc_file_get_contents(f, &len, err);
if (s == NULL)
return NULL;
b_trie_t *rv = blogc_source_parse(s, len, err);
// set FILENAME variable
if (rv != NULL) {
char *filename = blogc_get_filename(f);
if (filename != NULL)
b_trie_insert(rv, "FILENAME", filename);
}
free(s);
return rv;
}
b_slist_t*
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;
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);
if (s == NULL) {
*err = blogc_error_new_printf(BLOGC_ERROR_LOADER,
"An error occurred while parsing source file: %s\n\n%s",
f, tmp_err->msg);
blogc_error_free(tmp_err);
tmp_err = NULL;
b_slist_free_full(rv, (b_free_func_t) b_trie_free);
rv = NULL;
break;
}
if (first) {
const char *val = b_trie_lookup(s, "DATE");
if (val != NULL)
b_trie_insert(conf, "DATE_FIRST", b_strdup(val));
val = b_trie_lookup(s, "FILENAME");
if (val != NULL)
b_trie_insert(conf, "FILENAME_FIRST", b_strdup(val));
first = false;
}
if (tmp->next == NULL) { // last
const char *val = b_trie_lookup(s, "DATE");
if (val != NULL)
b_trie_insert(conf, "DATE_LAST", b_strdup(val));
val = b_trie_lookup(s, "FILENAME");
if (val != NULL)
b_trie_insert(conf, "FILENAME_LAST", b_strdup(val));
}
rv = b_slist_append(rv, s);
}
return rv;
}
|