diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/content-parser.c | 276 | ||||
-rw-r--r-- | src/datetime-parser.c | 4 | ||||
-rw-r--r-- | src/error.c | 12 | ||||
-rw-r--r-- | src/file.c | 8 | ||||
-rw-r--r-- | src/loader.c | 84 | ||||
-rw-r--r-- | src/loader.h | 8 | ||||
-rw-r--r-- | src/main.c | 52 | ||||
-rw-r--r-- | src/renderer.c | 68 | ||||
-rw-r--r-- | src/renderer.h | 16 | ||||
-rw-r--r-- | src/source-parser.c | 26 | ||||
-rw-r--r-- | src/source-parser.h | 4 | ||||
-rw-r--r-- | src/template-parser.c | 42 | ||||
-rw-r--r-- | src/template-parser.h | 6 | ||||
-rw-r--r-- | src/utils/mem.c | 42 | ||||
-rw-r--r-- | src/utils/slist.c | 68 | ||||
-rw-r--r-- | src/utils/strings.c | 314 | ||||
-rw-r--r-- | src/utils/trie.c | 199 | ||||
-rw-r--r-- | src/utils/utils.h | 79 |
18 files changed, 303 insertions, 1005 deletions
diff --git a/src/content-parser.c b/src/content-parser.c index 9d4adf6..7e2310f 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -13,7 +13,7 @@ #include <stdbool.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "content-parser.h" // this is a half ass implementation of a markdown-like syntax. bugs are @@ -25,7 +25,7 @@ blogc_slugify(const char *str) { if (str == NULL) return NULL; - char *new_str = b_strdup(str); + char *new_str = sb_strdup(str); int diff = 'a' - 'A'; // just to avoid magic numbers for (size_t i = 0; new_str[i] != '\0'; i++) { if (new_str[i] >= 'a' && new_str[i] <= 'z') @@ -46,32 +46,32 @@ blogc_htmlentities(const char *str) { if (str == NULL) return NULL; - b_string_t *rv = b_string_new(); + sb_string_t *rv = sb_string_new(); for (size_t i = 0; str[i] != '\0'; i++) { switch (str[i]) { case '&': - b_string_append(rv, "&"); + sb_string_append(rv, "&"); break; case '<': - b_string_append(rv, "<"); + sb_string_append(rv, "<"); break; case '>': - b_string_append(rv, ">"); + sb_string_append(rv, ">"); break; case '"': - b_string_append(rv, """); + sb_string_append(rv, """); break; case '\'': - b_string_append(rv, "'"); + sb_string_append(rv, "'"); break; case '/': - b_string_append(rv, "/"); + sb_string_append(rv, "/"); break; default: - b_string_append_c(rv, str[i]); + sb_string_append_c(rv, str[i]); } } - return b_string_free(rv, false); + return sb_string_free(rv, false); } @@ -126,7 +126,7 @@ blogc_content_parse_inline(const char *src) size_t start_state = 0; size_t end = 0; - b_string_t *rv = b_string_new(); + sb_string_t *rv = sb_string_new(); bool open_em_ast = false; bool open_strong_ast = false; @@ -152,7 +152,7 @@ blogc_content_parse_inline(const char *src) if (escape) { if (state == LINK_CLOSED) - b_string_append_c(rv, c); + sb_string_append_c(rv, c); current++; escape = false; continue; @@ -164,9 +164,9 @@ blogc_content_parse_inline(const char *src) if (state == LINK_TEXT_CLOSE && c != ' ' && c != '\n' && c != '\r' && c != '(') { - b_string_append_c(rv, src[start_state]); + sb_string_append_c(rv, src[start_state]); tmp = blogc_content_parse_inline(src + start_state + 1); - b_string_append(rv, tmp); + sb_string_append(rv, tmp); // no need to free here, we will exit the loop! break; } @@ -175,7 +175,7 @@ blogc_content_parse_inline(const char *src) case '\\': if (state == LINK_CLOSED && (open_code || open_code_double)) { - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; } if (!escape) @@ -185,7 +185,7 @@ blogc_content_parse_inline(const char *src) case '*': case '_': if (state == LINK_CLOSED && (open_code || open_code_double)) { - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; } if (!is_last && src[current + 1] == c) { @@ -194,7 +194,7 @@ blogc_content_parse_inline(const char *src) (c == '_' && open_strong_und)) { if (state == LINK_CLOSED) - b_string_append(rv, "</strong>"); + sb_string_append(rv, "</strong>"); if (c == '*') open_strong_ast = false; else @@ -202,7 +202,7 @@ blogc_content_parse_inline(const char *src) break; } if (state == LINK_CLOSED) - b_string_append(rv, "<strong>"); + sb_string_append(rv, "<strong>"); if (c == '*') open_strong_ast = true; else @@ -211,7 +211,7 @@ blogc_content_parse_inline(const char *src) } if ((c == '*' && open_em_ast) || (c == '_' && open_em_und)) { if (state == LINK_CLOSED) - b_string_append(rv, "</em>"); + sb_string_append(rv, "</em>"); if (c == '*') open_em_ast = false; else @@ -219,7 +219,7 @@ blogc_content_parse_inline(const char *src) break; } if (state == LINK_CLOSED) - b_string_append(rv, "<em>"); + sb_string_append(rv, "<em>"); if (c == '*') open_em_ast = true; else @@ -230,24 +230,24 @@ blogc_content_parse_inline(const char *src) if (!is_last && src[current + 1] == c) { current++; if (state == LINK_CLOSED) - b_string_append_printf(rv, "<%scode>", + sb_string_append_printf(rv, "<%scode>", open_code_double ? "/" : ""); open_code_double = !open_code_double; break; } if (state == LINK_CLOSED) - b_string_append_printf(rv, "<%scode>", open_code ? "/" : ""); + sb_string_append_printf(rv, "<%scode>", open_code ? "/" : ""); open_code = !open_code; break; case '!': if (state == LINK_CLOSED) { if (open_code || open_code_double) { - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; } if (!is_last && src[current + 1] != '[') { - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; } state = LINK_IMAGE; @@ -258,7 +258,7 @@ blogc_content_parse_inline(const char *src) case '[': if (state == LINK_CLOSED && (open_code || open_code_double)) { - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; } if (state == LINK_CLOSED || state == LINK_IMAGE) { @@ -288,8 +288,8 @@ blogc_content_parse_inline(const char *src) } if (state == LINK_AUTO_CLOSE) { state = LINK_CLOSED; - tmp = b_strndup(src + start, end - start); - b_string_append_printf(rv, "<a href=\"%s\">%s</a>", tmp, tmp); + tmp = sb_strndup(src + start, end - start); + sb_string_append_printf(rv, "<a href=\"%s\">%s</a>", tmp, tmp); end = 0; free(tmp); tmp = NULL; @@ -299,7 +299,7 @@ blogc_content_parse_inline(const char *src) if (state == LINK_TEXT) { if (open_bracket-- == 0) { state = LINK_TEXT_CLOSE; - tmp = b_strndup(src + start, current - start); + tmp = sb_strndup(src + start, current - start); tmp2 = blogc_content_parse_inline(tmp); free(tmp); tmp = NULL; @@ -307,7 +307,7 @@ blogc_content_parse_inline(const char *src) break; } if (state == LINK_CLOSED) - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; case '(': @@ -317,18 +317,18 @@ blogc_content_parse_inline(const char *src) break; } if (state == LINK_CLOSED) - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; case ')': if (state == LINK_URL) { state = LINK_CLOSED; - tmp = b_strndup(src + start, current - start); + tmp = sb_strndup(src + start, current - start); if (is_image) - b_string_append_printf(rv, "<img src=\"%s\" alt=\"%s\">", + sb_string_append_printf(rv, "<img src=\"%s\" alt=\"%s\">", tmp, tmp2); else - b_string_append_printf(rv, "<a href=\"%s\">%s</a>", + sb_string_append_printf(rv, "<a href=\"%s\">%s</a>", tmp, tmp2); free(tmp); tmp = NULL; @@ -338,13 +338,13 @@ blogc_content_parse_inline(const char *src) break; } if (state == LINK_CLOSED) - b_string_append_c(rv, c); + sb_string_append_c(rv, c); break; case ' ': if (state == LINK_CLOSED) { spaces++; - b_string_append_c(rv, c); + sb_string_append_c(rv, c); } if (!is_last) break; @@ -353,53 +353,53 @@ blogc_content_parse_inline(const char *src) case '\r': if (state == LINK_CLOSED) { if (spaces >= 2) { - b_string_append(rv, "<br />"); + sb_string_append(rv, "<br />"); spaces = 0; } if (c == '\n' || c == '\r') - b_string_append_c(rv, c); + sb_string_append_c(rv, c); } break; case '&': if (state == LINK_CLOSED) - b_string_append(rv, "&"); + sb_string_append(rv, "&"); break; case '<': if (state == LINK_CLOSED) - b_string_append(rv, "<"); + sb_string_append(rv, "<"); break; case '>': if (state == LINK_CLOSED) - b_string_append(rv, ">"); + sb_string_append(rv, ">"); break; case '"': if (state == LINK_CLOSED) - b_string_append(rv, """); + sb_string_append(rv, """); break; case '\'': if (state == LINK_CLOSED) - b_string_append(rv, "'"); + sb_string_append(rv, "'"); break; case '/': if (state == LINK_CLOSED) - b_string_append(rv, "/"); + sb_string_append(rv, "/"); break; default: if (state == LINK_CLOSED) - b_string_append_c(rv, c); + sb_string_append_c(rv, c); } if (is_last && state != LINK_CLOSED) { - b_string_append_c(rv, src[start_state]); + sb_string_append_c(rv, src[start_state]); tmp = blogc_content_parse_inline(src + start_state + 1); - b_string_append(rv, tmp); + sb_string_append(rv, tmp); // no need to free here, its the last iteration } current++; @@ -408,7 +408,7 @@ blogc_content_parse_inline(const char *src) free(tmp); free(tmp2); - return b_string_free(rv, false); + return sb_string_free(rv, false); } @@ -468,11 +468,11 @@ blogc_content_parse(const char *src, size_t *end_excerpt) char d = '\0'; - b_slist_t *lines = NULL; - b_slist_t *lines2 = NULL; + sb_slist_t *lines = NULL; + sb_slist_t *lines2 = NULL; - b_string_t *rv = b_string_new(); - b_string_t *tmp_str = NULL; + sb_string_t *rv = sb_string_new(); + sb_string_t *tmp_str = NULL; blogc_content_parser_state_t state = CONTENT_START_LINE; @@ -599,14 +599,14 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - tmp = b_strndup(src + start, end - start); + tmp = sb_strndup(src + start, end - start); parsed = blogc_content_parse_inline(tmp); slug = blogc_slugify(tmp); if (slug == NULL) - b_string_append_printf(rv, "<h%d>%s</h%d>%s", + sb_string_append_printf(rv, "<h%d>%s</h%d>%s", header_level, parsed, header_level, line_ending); else - b_string_append_printf(rv, "<h%d id=\"%s\">%s</h%d>%s", + sb_string_append_printf(rv, "<h%d id=\"%s\">%s</h%d>%s", header_level, slug, parsed, header_level, line_ending); free(slug); @@ -630,8 +630,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_HTML_END: if (c == '\n' || c == '\r' || is_last) { - tmp = b_strndup(src + start, end - start); - b_string_append_printf(rv, "%s%s", tmp, line_ending); + tmp = sb_strndup(src + start, end - start); + sb_string_append_printf(rv, "%s%s", tmp, line_ending); free(tmp); tmp = NULL; state = CONTENT_START_LINE; @@ -644,7 +644,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_BLOCKQUOTE: if (c == ' ' || c == '\t') break; - prefix = b_strndup(src + start, current - start); + prefix = sb_strndup(src + start, current - start); state = CONTENT_BLOCKQUOTE_START; break; @@ -652,16 +652,16 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - tmp = b_strndup(src + start2, end - start2); - if (b_str_starts_with(tmp, prefix)) { - lines = b_slist_append(lines, b_strdup(tmp + strlen(prefix))); + tmp = sb_strndup(src + start2, end - start2); + if (sb_str_starts_with(tmp, prefix)) { + lines = sb_slist_append(lines, sb_strdup(tmp + strlen(prefix))); state = CONTENT_BLOCKQUOTE_END; } else { state = CONTENT_PARAGRAPH; free(prefix); prefix = NULL; - b_slist_free_full(lines, free); + sb_slist_free_full(lines, free); lines = NULL; if (is_last) { free(tmp); @@ -677,22 +677,22 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_BLOCKQUOTE_END: if (c == '\n' || c == '\r' || is_last) { - tmp_str = b_string_new(); - for (b_slist_t *l = lines; l != NULL; l = l->next) { + tmp_str = sb_string_new(); + for (sb_slist_t *l = lines; l != NULL; l = l->next) { if (l->next == NULL) - b_string_append_printf(tmp_str, "%s", l->data); + sb_string_append_printf(tmp_str, "%s", l->data); else - b_string_append_printf(tmp_str, "%s%s", l->data, + sb_string_append_printf(tmp_str, "%s%s", l->data, line_ending); } tmp = blogc_content_parse(tmp_str->str, NULL); - b_string_append_printf(rv, "<blockquote>%s</blockquote>%s", + sb_string_append_printf(rv, "<blockquote>%s</blockquote>%s", tmp, line_ending); free(tmp); tmp = NULL; - b_string_free(tmp_str, true); + sb_string_free(tmp_str, true); tmp_str = NULL; - b_slist_free_full(lines, free); + sb_slist_free_full(lines, free); lines = NULL; free(prefix); prefix = NULL; @@ -708,7 +708,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_CODE: if (c == ' ' || c == '\t') break; - prefix = b_strndup(src + start, current - start); + prefix = sb_strndup(src + start, current - start); state = CONTENT_CODE_START; break; @@ -716,16 +716,16 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - tmp = b_strndup(src + start2, end - start2); - if (b_str_starts_with(tmp, prefix)) { - lines = b_slist_append(lines, b_strdup(tmp + strlen(prefix))); + tmp = sb_strndup(src + start2, end - start2); + if (sb_str_starts_with(tmp, prefix)) { + lines = sb_slist_append(lines, sb_strdup(tmp + strlen(prefix))); state = CONTENT_CODE_END; } else { state = CONTENT_PARAGRAPH; free(prefix); prefix = NULL; - b_slist_free_full(lines, free); + sb_slist_free_full(lines, free); lines = NULL; free(tmp); tmp = NULL; @@ -741,18 +741,18 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_CODE_END: if (c == '\n' || c == '\r' || is_last) { - b_string_append(rv, "<pre><code>"); - for (b_slist_t *l = lines; l != NULL; l = l->next) { + sb_string_append(rv, "<pre><code>"); + for (sb_slist_t *l = lines; l != NULL; l = l->next) { char *tmp_line = blogc_htmlentities(l->data); if (l->next == NULL) - b_string_append_printf(rv, "%s", tmp_line); + sb_string_append_printf(rv, "%s", tmp_line); else - b_string_append_printf(rv, "%s%s", tmp_line, + sb_string_append_printf(rv, "%s%s", tmp_line, line_ending); free(tmp_line); } - b_string_append_printf(rv, "</code></pre>%s", line_ending); - b_slist_free_full(lines, free); + sb_string_append_printf(rv, "</code></pre>%s", line_ending); + sb_slist_free_full(lines, free); lines = NULL; free(prefix); prefix = NULL; @@ -774,7 +774,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt) } if (c == ' ' || c == '\t') break; - prefix = b_strndup(src + start, current - start); + prefix = sb_strndup(src + start, current - start); state = CONTENT_UNORDERED_LIST_START; break; @@ -783,7 +783,7 @@ blogc_content_parse(const char *src, size_t *end_excerpt) break; } if (c == '\n' || c == '\r' || is_last) { - b_string_append_printf(rv, "<hr />%s", line_ending); + sb_string_append_printf(rv, "<hr />%s", line_ending); state = CONTENT_START_LINE; start = current; d = '\0'; @@ -796,30 +796,30 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - tmp = b_strndup(src + start2, end - start2); - tmp2 = b_strdup_printf("%-*s", strlen(prefix), ""); - if (b_str_starts_with(tmp, prefix)) { + tmp = sb_strndup(src + start2, end - start2); + tmp2 = sb_strdup_printf("%-*s", strlen(prefix), ""); + if (sb_str_starts_with(tmp, prefix)) { if (lines2 != NULL) { - tmp_str = b_string_new(); - for (b_slist_t *l = lines2; l != NULL; l = l->next) { + tmp_str = sb_string_new(); + for (sb_slist_t *l = lines2; l != NULL; l = l->next) { if (l->next == NULL) - b_string_append_printf(tmp_str, "%s", l->data); + sb_string_append_printf(tmp_str, "%s", l->data); else - b_string_append_printf(tmp_str, "%s%s", l->data, + sb_string_append_printf(tmp_str, "%s%s", l->data, line_ending); } - b_slist_free_full(lines2, free); + sb_slist_free_full(lines2, free); lines2 = NULL; parsed = blogc_content_parse_inline(tmp_str->str); - b_string_free(tmp_str, true); - lines = b_slist_append(lines, b_strdup(parsed)); + sb_string_free(tmp_str, true); + lines = sb_slist_append(lines, sb_strdup(parsed)); free(parsed); parsed = NULL; } - lines2 = b_slist_append(lines2, b_strdup(tmp + strlen(prefix))); + lines2 = sb_slist_append(lines2, sb_strdup(tmp + strlen(prefix))); } - else if (b_str_starts_with(tmp, tmp2)) { - lines2 = b_slist_append(lines2, b_strdup(tmp + strlen(prefix))); + else if (sb_str_starts_with(tmp, tmp2)) { + lines2 = sb_slist_append(lines2, sb_strdup(tmp + strlen(prefix))); } else { state = CONTENT_PARAGRAPH_END; @@ -829,8 +829,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt) tmp2 = NULL; free(prefix); prefix = NULL; - b_slist_free_full(lines, free); - b_slist_free_full(lines2, free); + sb_slist_free_full(lines, free); + sb_slist_free_full(lines2, free); lines = NULL; if (is_last) continue; @@ -849,28 +849,28 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { if (lines2 != NULL) { // FIXME: avoid repeting the code below - tmp_str = b_string_new(); - for (b_slist_t *l = lines2; l != NULL; l = l->next) { + tmp_str = sb_string_new(); + for (sb_slist_t *l = lines2; l != NULL; l = l->next) { if (l->next == NULL) - b_string_append_printf(tmp_str, "%s", l->data); + sb_string_append_printf(tmp_str, "%s", l->data); else - b_string_append_printf(tmp_str, "%s%s", l->data, + sb_string_append_printf(tmp_str, "%s%s", l->data, line_ending); } - b_slist_free_full(lines2, free); + sb_slist_free_full(lines2, free); lines2 = NULL; parsed = blogc_content_parse_inline(tmp_str->str); - b_string_free(tmp_str, true); - lines = b_slist_append(lines, b_strdup(parsed)); + sb_string_free(tmp_str, true); + lines = sb_slist_append(lines, sb_strdup(parsed)); free(parsed); parsed = NULL; } - b_string_append_printf(rv, "<ul>%s", line_ending); - for (b_slist_t *l = lines; l != NULL; l = l->next) - b_string_append_printf(rv, "<li>%s</li>%s", l->data, + sb_string_append_printf(rv, "<ul>%s", line_ending); + for (sb_slist_t *l = lines; l != NULL; l = l->next) + sb_string_append_printf(rv, "<li>%s</li>%s", l->data, line_ending); - b_string_append_printf(rv, "</ul>%s", line_ending); - b_slist_free_full(lines, free); + sb_string_append_printf(rv, "</ul>%s", line_ending); + sb_slist_free_full(lines, free); lines = NULL; free(prefix); prefix = NULL; @@ -907,30 +907,30 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { end = is_last && c != '\n' && c != '\r' ? src_len : (real_end != 0 ? real_end : current); - tmp = b_strndup(src + start2, end - start2); - tmp2 = b_strdup_printf("%-*s", prefix_len, ""); + tmp = sb_strndup(src + start2, end - start2); + tmp2 = sb_strdup_printf("%-*s", prefix_len, ""); if (blogc_is_ordered_list_item(tmp, prefix_len)) { if (lines2 != NULL) { - tmp_str = b_string_new(); - for (b_slist_t *l = lines2; l != NULL; l = l->next) { + tmp_str = sb_string_new(); + for (sb_slist_t *l = lines2; l != NULL; l = l->next) { if (l->next == NULL) - b_string_append_printf(tmp_str, "%s", l->data); + sb_string_append_printf(tmp_str, "%s", l->data); else - b_string_append_printf(tmp_str, "%s%s", l->data, + sb_string_append_printf(tmp_str, "%s%s", l->data, line_ending); } - b_slist_free_full(lines2, free); + sb_slist_free_full(lines2, free); lines2 = NULL; parsed = blogc_content_parse_inline(tmp_str->str); - b_string_free(tmp_str, true); - lines = b_slist_append(lines, b_strdup(parsed)); + sb_string_free(tmp_str, true); + lines = sb_slist_append(lines, sb_strdup(parsed)); free(parsed); parsed = NULL; } - lines2 = b_slist_append(lines2, b_strdup(tmp + prefix_len)); + lines2 = sb_slist_append(lines2, sb_strdup(tmp + prefix_len)); } - else if (b_str_starts_with(tmp, tmp2)) { - lines2 = b_slist_append(lines2, b_strdup(tmp + prefix_len)); + else if (sb_str_starts_with(tmp, tmp2)) { + lines2 = sb_slist_append(lines2, sb_strdup(tmp + prefix_len)); } else { state = CONTENT_PARAGRAPH_END; @@ -940,8 +940,8 @@ blogc_content_parse(const char *src, size_t *end_excerpt) tmp2 = NULL; free(parsed); parsed = NULL; - b_slist_free_full(lines, free); - b_slist_free_full(lines2, free); + sb_slist_free_full(lines, free); + sb_slist_free_full(lines2, free); lines = NULL; if (is_last) continue; @@ -960,28 +960,28 @@ blogc_content_parse(const char *src, size_t *end_excerpt) if (c == '\n' || c == '\r' || is_last) { if (lines2 != NULL) { // FIXME: avoid repeting the code below - tmp_str = b_string_new(); - for (b_slist_t *l = lines2; l != NULL; l = l->next) { + tmp_str = sb_string_new(); + for (sb_slist_t *l = lines2; l != NULL; l = l->next) { if (l->next == NULL) - b_string_append_printf(tmp_str, "%s", l->data); + sb_string_append_printf(tmp_str, "%s", l->data); else - b_string_append_printf(tmp_str, "%s%s", l->data, + sb_string_append_printf(tmp_str, "%s%s", l->data, line_ending); } - b_slist_free_full(lines2, free); + sb_slist_free_full(lines2, free); lines2 = NULL; parsed = blogc_content_parse_inline(tmp_str->str); - b_string_free(tmp_str, true); - lines = b_slist_append(lines, b_strdup(parsed)); + sb_string_free(tmp_str, true); + lines = sb_slist_append(lines, sb_strdup(parsed)); free(parsed); parsed = NULL; } - b_string_append_printf(rv, "<ol>%s", line_ending); - for (b_slist_t *l = lines; l != NULL; l = l->next) - b_string_append_printf(rv, "<li>%s</li>%s", l->data, + sb_string_append_printf(rv, "<ol>%s", line_ending); + for (sb_slist_t *l = lines; l != NULL; l = l->next) + sb_string_append_printf(rv, "<li>%s</li>%s", l->data, line_ending); - b_string_append_printf(rv, "</ol>%s", line_ending); - b_slist_free_full(lines, free); + sb_string_append_printf(rv, "</ol>%s", line_ending); + sb_slist_free_full(lines, free); lines = NULL; free(prefix); prefix = NULL; @@ -1005,9 +1005,9 @@ blogc_content_parse(const char *src, size_t *end_excerpt) case CONTENT_PARAGRAPH_END: if (c == '\n' || c == '\r' || is_last) { - tmp = b_strndup(src + start, end - start); + tmp = sb_strndup(src + start, end - start); parsed = blogc_content_parse_inline(tmp); - b_string_append_printf(rv, "<p>%s</p>%s", parsed, + sb_string_append_printf(rv, "<p>%s</p>%s", parsed, line_ending); free(parsed); parsed = NULL; @@ -1025,5 +1025,5 @@ blogc_content_parse(const char *src, size_t *end_excerpt) current++; } - return b_string_free(rv, false); + return sb_string_free(rv, false); } diff --git a/src/datetime-parser.c b/src/datetime-parser.c index 8785a89..0aeb702 100644 --- a/src/datetime-parser.c +++ b/src/datetime-parser.c @@ -17,7 +17,7 @@ #include <string.h> #include "error.h" -#include "utils/utils.h" +#include <squareball.h> #include "datetime-parser.h" @@ -380,7 +380,7 @@ blogc_convert_datetime(const char *orig, const char *format, return NULL; } - return b_strdup(buf); + return sb_strdup(buf); #endif } diff --git a/src/error.c b/src/error.c index 6256873..f05049c 100644 --- a/src/error.c +++ b/src/error.c @@ -14,16 +14,16 @@ #include <stdlib.h> #include <stdarg.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "error.h" blogc_error_t* blogc_error_new(blogc_error_type_t type, const char *msg) { - blogc_error_t *err = b_malloc(sizeof(blogc_error_t)); + blogc_error_t *err = sb_malloc(sizeof(blogc_error_t)); err->type = type; - err->msg = b_strdup(msg); + err->msg = sb_strdup(msg); return err; } @@ -33,7 +33,7 @@ blogc_error_new_printf(blogc_error_type_t type, const char *format, ...) { va_list ap; va_start(ap, format); - char *tmp = b_strdup_vprintf(format, ap); + char *tmp = sb_strdup_vprintf(format, ap); va_end(ap); blogc_error_t *rv = blogc_error_new(type, tmp); free(tmp); @@ -47,7 +47,7 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, { va_list ap; va_start(ap, format); - char *msg = b_strdup_vprintf(format, ap); + char *msg = sb_strdup_vprintf(format, ap); va_end(ap); size_t lineno = 1; @@ -88,7 +88,7 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, if (lineend <= linestart && src_len >= linestart) lineend = src_len; - char *line = b_strndup(src + linestart, lineend - linestart); + char *line = sb_strndup(src + linestart, lineend - linestart); blogc_error_t *rv = NULL; @@ -14,7 +14,7 @@ #include <stdarg.h> #include <stdio.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "file.h" #include "error.h" @@ -38,16 +38,16 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) return NULL; } - b_string_t *str = b_string_new(); + sb_string_t *str = sb_string_new(); char buffer[BLOGC_FILE_CHUNK_SIZE]; while (!feof(fp)) { size_t read_len = fread(buffer, sizeof(char), BLOGC_FILE_CHUNK_SIZE, fp); *len += read_len; - b_string_append_len(str, buffer, read_len); + sb_string_append_len(str, buffer, read_len); } fclose(fp); - return b_string_free(str, false); + return sb_string_free(str, false); } diff --git a/src/loader.c b/src/loader.c index baa81fa..8008c3a 100644 --- a/src/loader.c +++ b/src/loader.c @@ -14,7 +14,7 @@ #include <stdbool.h> #include <stdio.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "file.h" #include "source-parser.h" #include "template-parser.h" @@ -31,7 +31,7 @@ blogc_get_filename(const char *f) if (strlen(f) == 0) return NULL; - char *filename = b_strdup(f); + char *filename = sb_strdup(f); // keep a pointer to original string char *tmp = filename; @@ -52,14 +52,14 @@ blogc_get_filename(const char *f) } } - char *final_filename = b_strdup(tmp); + char *final_filename = sb_strdup(tmp); free(filename); return final_filename; } -b_slist_t* +sb_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err) { if (err == NULL || *err != NULL) @@ -68,13 +68,13 @@ blogc_template_parse_from_file(const char *f, blogc_error_t **err) char *s = blogc_file_get_contents(f, &len, err); if (s == NULL) return NULL; - b_slist_t *rv = blogc_template_parse(s, len, err); + sb_slist_t *rv = blogc_template_parse(s, len, err); free(s); return rv; } -b_trie_t* +sb_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err) { if (err == NULL || *err != NULL) @@ -83,13 +83,13 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err) char *s = blogc_file_get_contents(f, &len, err); if (s == NULL) return NULL; - b_trie_t *rv = blogc_source_parse(s, len, err); + sb_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); + sb_trie_insert(rv, "FILENAME", filename); } free(s); @@ -97,16 +97,16 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err) } -b_slist_t* -blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) +sb_slist_t* +blogc_source_parse_from_files(sb_trie_t *conf, sb_slist_t *l, blogc_error_t **err) { blogc_error_t *tmp_err = NULL; - b_slist_t *rv = NULL; + sb_slist_t *rv = NULL; unsigned int with_date = 0; - const char *filter_tag = b_trie_lookup(conf, "FILTER_TAG"); - const char *filter_page = b_trie_lookup(conf, "FILTER_PAGE"); - const char *filter_per_page = b_trie_lookup(conf, "FILTER_PER_PAGE"); + const char *filter_tag = sb_trie_lookup(conf, "FILTER_TAG"); + const char *filter_page = sb_trie_lookup(conf, "FILTER_PAGE"); + const char *filter_per_page = sb_trie_lookup(conf, "FILTER_PER_PAGE"); long page = strtol(filter_page != NULL ? filter_page : "", NULL, 10); if (page <= 0) @@ -121,27 +121,27 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) unsigned int end = start + per_page; unsigned int counter = 0; - for (b_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) { + for (sb_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); + sb_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); + sb_slist_free_full(rv, (sb_free_func_t) sb_trie_free); rv = NULL; break; } if (filter_tag != NULL) { - const char *tags_str = b_trie_lookup(s, "TAGS"); + const char *tags_str = sb_trie_lookup(s, "TAGS"); // if user wants to filter by tag and no tag is provided, skip it if (tags_str == NULL) { - b_trie_free(s); + sb_trie_free(s); continue; } - char **tags = b_str_split(tags_str, ' ', 0); + char **tags = sb_str_split(tags_str, ' ', 0); bool found = false; for (unsigned int i = 0; tags[i] != NULL; i++) { if (tags[i][0] == '\0') @@ -149,26 +149,26 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) if (0 == strcmp(tags[i], filter_tag)) found = true; } - b_strv_free(tags); + sb_strv_free(tags); if (!found) { - b_trie_free(s); + sb_trie_free(s); continue; } } if (filter_page != NULL) { if (counter < start || counter >= end) { counter++; - b_trie_free(s); + sb_trie_free(s); continue; } counter++; } - if (b_trie_lookup(s, "DATE") != NULL) + if (sb_trie_lookup(s, "DATE") != NULL) with_date++; - rv = b_slist_append(rv, s); + rv = sb_slist_append(rv, s); } - if (with_date > 0 && with_date < b_slist_length(rv)) + if (with_date > 0 && with_date < sb_slist_length(rv)) // fatal error, maybe? blogc_fprintf(stderr, "blogc: warning: 'DATE' variable provided for at least one source " @@ -176,38 +176,38 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err) "wrong values for 'DATE_FIRST' and 'DATE_LAST' variables.\n"); bool first = true; - for (b_slist_t *tmp = rv; tmp != NULL; tmp = tmp->next) { - b_trie_t *s = tmp->data; + for (sb_slist_t *tmp = rv; tmp != NULL; tmp = tmp->next) { + sb_trie_t *s = tmp->data; if (first) { - const char *val = b_trie_lookup(s, "DATE"); + const char *val = sb_trie_lookup(s, "DATE"); if (val != NULL) - b_trie_insert(conf, "DATE_FIRST", b_strdup(val)); - val = b_trie_lookup(s, "FILENAME"); + sb_trie_insert(conf, "DATE_FIRST", sb_strdup(val)); + val = sb_trie_lookup(s, "FILENAME"); if (val != NULL) - b_trie_insert(conf, "FILENAME_FIRST", b_strdup(val)); + sb_trie_insert(conf, "FILENAME_FIRST", sb_strdup(val)); first = false; } if (tmp->next == NULL) { // last - const char *val = b_trie_lookup(s, "DATE"); + const char *val = sb_trie_lookup(s, "DATE"); if (val != NULL) - b_trie_insert(conf, "DATE_LAST", b_strdup(val)); - val = b_trie_lookup(s, "FILENAME"); + sb_trie_insert(conf, "DATE_LAST", sb_strdup(val)); + val = sb_trie_lookup(s, "FILENAME"); if (val != NULL) - b_trie_insert(conf, "FILENAME_LAST", b_strdup(val)); + sb_trie_insert(conf, "FILENAME_LAST", sb_strdup(val)); } } if (filter_page != NULL) { unsigned int last_page = ceilf(((float) counter) / per_page); - b_trie_insert(conf, "CURRENT_PAGE", b_strdup_printf("%ld", page)); + sb_trie_insert(conf, "CURRENT_PAGE", sb_strdup_printf("%ld", page)); if (page > 1) - b_trie_insert(conf, "PREVIOUS_PAGE", b_strdup_printf("%ld", page - 1)); + sb_trie_insert(conf, "PREVIOUS_PAGE", sb_strdup_printf("%ld", page - 1)); if (page < last_page) - b_trie_insert(conf, "NEXT_PAGE", b_strdup_printf("%ld", page + 1)); - if (b_slist_length(rv) > 0) - b_trie_insert(conf, "FIRST_PAGE", b_strdup("1")); + sb_trie_insert(conf, "NEXT_PAGE", sb_strdup_printf("%ld", page + 1)); + if (sb_slist_length(rv) > 0) + sb_trie_insert(conf, "FIRST_PAGE", sb_strdup("1")); if (last_page > 0) - b_trie_insert(conf, "LAST_PAGE", b_strdup_printf("%d", last_page)); + sb_trie_insert(conf, "LAST_PAGE", sb_strdup_printf("%d", last_page)); } return rv; diff --git a/src/loader.h b/src/loader.h index c432e20..080aff6 100644 --- a/src/loader.h +++ b/src/loader.h @@ -9,13 +9,13 @@ #ifndef _LOADER_H #define _LOADER_H -#include "utils/utils.h" +#include <squareball.h> #include "error.h" char* blogc_get_filename(const char *f); -b_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err); -b_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err); -b_slist_t* blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, +sb_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err); +sb_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err); +sb_slist_t* blogc_source_parse_from_files(sb_trie_t *conf, sb_slist_t *l, blogc_error_t **err); #endif /* _LOADER_H */ @@ -24,7 +24,7 @@ #include <stdio.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "source-parser.h" #include "template-parser.h" #include "loader.h" @@ -71,7 +71,7 @@ blogc_print_usage(void) static void blogc_mkdir_recursive(const char *filename) { - char *fname = b_strdup(filename); + char *fname = sb_strdup(filename); for (char *tmp = fname; *tmp != '\0'; tmp++) { if (*tmp != '/' && *tmp != '\\') continue; @@ -117,9 +117,9 @@ main(int argc, char **argv) char *tmp = NULL; char **pieces = NULL; - b_slist_t *sources = NULL; - b_trie_t *config = b_trie_new(free); - b_trie_insert(config, "BLOGC_VERSION", b_strdup(PACKAGE_VERSION)); + sb_slist_t *sources = NULL; + sb_trie_t *config = sb_trie_new(free); + sb_trie_insert(config, "BLOGC_VERSION", sb_strdup(PACKAGE_VERSION)); for (unsigned int i = 1; i < argc; i++) { tmp = NULL; @@ -136,21 +136,21 @@ main(int argc, char **argv) break; case 't': if (argv[i][2] != '\0') - template = b_strdup(argv[i] + 2); + template = sb_strdup(argv[i] + 2); else if (i + 1 < argc) - template = b_strdup(argv[++i]); + template = sb_strdup(argv[++i]); break; case 'o': if (argv[i][2] != '\0') - output = b_strdup(argv[i] + 2); + output = sb_strdup(argv[i] + 2); else if (i + 1 < argc) - output = b_strdup(argv[++i]); + output = sb_strdup(argv[++i]); break; case 'p': if (argv[i][2] != '\0') - print = b_strdup(argv[i] + 2); + print = sb_strdup(argv[i] + 2); else if (i + 1 < argc) - print = b_strdup(argv[++i]); + print = sb_strdup(argv[++i]); break; case 'D': if (argv[i][2] != '\0') @@ -158,11 +158,11 @@ main(int argc, char **argv) else if (i + 1 < argc) tmp = argv[++i]; if (tmp != NULL) { - pieces = b_str_split(tmp, '=', 2); - if (b_strv_length(pieces) != 2) { + pieces = sb_str_split(tmp, '=', 2); + if (sb_strv_length(pieces) != 2) { fprintf(stderr, "blogc: error: invalid value for " "-D (must have an '='): %s\n", tmp); - b_strv_free(pieces); + sb_strv_free(pieces); rv = 2; goto cleanup; } @@ -173,13 +173,13 @@ main(int argc, char **argv) fprintf(stderr, "blogc: error: invalid value " "for -D (configuration key must be uppercase " "with '_'): %s\n", pieces[0]); - b_strv_free(pieces); + sb_strv_free(pieces); rv = 2; goto cleanup; } } - b_trie_insert(config, pieces[0], b_strdup(pieces[1])); - b_strv_free(pieces); + sb_trie_insert(config, pieces[0], sb_strdup(pieces[1])); + sb_strv_free(pieces); pieces = NULL; } break; @@ -192,17 +192,17 @@ main(int argc, char **argv) } } else - sources = b_slist_append(sources, b_strdup(argv[i])); + sources = sb_slist_append(sources, sb_strdup(argv[i])); } - if (!listing && b_slist_length(sources) == 0) { + if (!listing && sb_slist_length(sources) == 0) { blogc_print_usage(); fprintf(stderr, "blogc: error: one source file is required\n"); rv = 2; goto cleanup; } - if (!listing && b_slist_length(sources) > 1) { + if (!listing && sb_slist_length(sources) > 1) { blogc_print_usage(); fprintf(stderr, "blogc: error: only one source file should be provided, " "if running without '-l'\n"); @@ -212,14 +212,14 @@ main(int argc, char **argv) blogc_error_t *err = NULL; - b_slist_t *s = blogc_source_parse_from_files(config, sources, &err); + sb_slist_t *s = blogc_source_parse_from_files(config, sources, &err); if (err != NULL) { blogc_error_print(err); rv = 2; goto cleanup2; } - b_slist_t* l = blogc_template_parse_from_file(template, &err); + sb_slist_t* l = blogc_template_parse_from_file(template, &err); if (err != NULL) { blogc_error_print(err); rv = 2; @@ -227,7 +227,7 @@ main(int argc, char **argv) } if (print != NULL) { - const char *val = b_trie_lookup(config, print); + const char *val = sb_trie_lookup(config, print); if (val == NULL) { fprintf(stderr, "blogc: error: configuration variable not found: %s\n", print); @@ -273,13 +273,13 @@ cleanup4: cleanup3: blogc_template_free_stmts(l); cleanup2: - b_slist_free_full(s, (b_free_func_t) b_trie_free); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); blogc_error_free(err); cleanup: - b_trie_free(config); + sb_trie_free(config); free(template); free(output); free(print); - b_slist_free_full(sources, free); + sb_slist_free_full(sources, free); return rv; } diff --git a/src/renderer.c b/src/renderer.c index 5e07b0c..a7a7dd7 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -13,7 +13,7 @@ #include <stdbool.h> #include <stdio.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "datetime-parser.h" #include "error.h" #include "loader.h" @@ -23,59 +23,59 @@ const char* -blogc_get_variable(const char *name, b_trie_t *global, b_trie_t *local) +blogc_get_variable(const char *name, sb_trie_t *global, sb_trie_t *local) { const char *rv = NULL; if (local != NULL) { - rv = b_trie_lookup(local, name); + rv = sb_trie_lookup(local, name); if (rv != NULL) return rv; } if (global != NULL) - rv = b_trie_lookup(global, name); + rv = sb_trie_lookup(global, name); return rv; } char* -blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local) +blogc_format_date(const char *date, sb_trie_t *global, sb_trie_t *local) { const char *date_format = blogc_get_variable("DATE_FORMAT", global, local); if (date == NULL) return NULL; if (date_format == NULL) - return b_strdup(date); + return sb_strdup(date); blogc_error_t *err = NULL; char *rv = blogc_convert_datetime(date, date_format, &err); if (err != NULL) { blogc_error_print(err); blogc_error_free(err); - return b_strdup(date); + return sb_strdup(date); } return rv; } char* -blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local, - b_slist_t *foreach_var) +blogc_format_variable(const char *name, sb_trie_t *global, sb_trie_t *local, + sb_slist_t *foreach_var) { if (0 == strcmp(name, "FOREACH_ITEM")) { if (foreach_var != NULL && foreach_var->data != NULL) { - return b_strdup(foreach_var->data); + return sb_strdup(foreach_var->data); } return NULL; } char *var = NULL; bool must_format = false; - if (b_str_ends_with(name, "_FORMATTED")) { - var = b_strndup(name, strlen(name) - 10); + if (sb_str_ends_with(name, "_FORMATTED")) { + var = sb_strndup(name, strlen(name) - 10); must_format = true; } if (var == NULL) - var = b_strdup(name); + var = sb_strdup(name); const char *value = blogc_get_variable(var, global, local); free(var); @@ -85,30 +85,30 @@ blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local, char *rv = NULL; if (must_format) { - if (b_str_starts_with(name, "DATE_")) { + if (sb_str_starts_with(name, "DATE_")) { rv = blogc_format_date(value, global, local); } } if (rv == NULL) - return b_strdup(value); + return sb_strdup(value); return rv; } -b_slist_t* -blogc_split_list_variable(const char *name, b_trie_t *global, b_trie_t *local) +sb_slist_t* +blogc_split_list_variable(const char *name, sb_trie_t *global, sb_trie_t *local) { const char *value = blogc_get_variable(name, global, local); if (value == NULL) return NULL; - b_slist_t *rv = NULL; + sb_slist_t *rv = NULL; - char **tmp = b_str_split(value, ' ', 0); + char **tmp = sb_str_split(value, ' ', 0); for (unsigned int i = 0; tmp[i] != NULL; i++) { if (tmp[i][0] != '\0') // ignore empty strings - rv = b_slist_append(rv, tmp[i]); + rv = sb_slist_append(rv, tmp[i]); else free(tmp[i]); } @@ -119,25 +119,25 @@ blogc_split_list_variable(const char *name, b_trie_t *global, b_trie_t *local) char* -blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing) +blogc_render(sb_slist_t *tmpl, sb_slist_t *sources, sb_trie_t *config, bool listing) { if (tmpl == NULL) return NULL; - b_slist_t *current_source = NULL; - b_slist_t *listing_start = NULL; + sb_slist_t *current_source = NULL; + sb_slist_t *listing_start = NULL; - b_string_t *str = b_string_new(); + sb_string_t *str = sb_string_new(); - b_trie_t *tmp_source = NULL; + sb_trie_t *tmp_source = NULL; char *config_value = NULL; char *defined = NULL; unsigned int if_count = 0; - b_slist_t *foreach_var = NULL; - b_slist_t *foreach_var_start = NULL; - b_slist_t *foreach_start = NULL; + sb_slist_t *foreach_var = NULL; + sb_slist_t *foreach_var_start = NULL; + sb_slist_t *foreach_start = NULL; bool if_not = false; bool inside_block = false; @@ -145,7 +145,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing int cmp = 0; - b_slist_t *tmp = tmpl; + sb_slist_t *tmp = tmpl; while (tmp != NULL) { blogc_template_stmt_t *stmt = tmp->data; @@ -153,7 +153,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing case BLOGC_TEMPLATE_CONTENT_STMT: if (stmt->value != NULL) - b_string_append(str, stmt->value); + sb_string_append(str, stmt->value); break; case BLOGC_TEMPLATE_BLOCK_STMT: @@ -210,7 +210,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing config_value = blogc_format_variable(stmt->value, config, inside_block ? tmp_source : NULL, foreach_var); if (config_value != NULL) { - b_string_append(str, config_value); + sb_string_append(str, config_value); free(config_value); config_value = NULL; break; @@ -252,7 +252,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing (stmt->value2[0] == '"') && (stmt->value2[strlen(stmt->value2) - 1] == '"')) { - defined2 = b_strndup(stmt->value2 + 1, + defined2 = sb_strndup(stmt->value2 + 1, strlen(stmt->value2) - 2); } else { @@ -354,7 +354,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing } } foreach_start = NULL; - b_slist_free_full(foreach_var_start, free); + sb_slist_free_full(foreach_var_start, free); foreach_var_start = NULL; break; } @@ -364,5 +364,5 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing // no need to free temporary variables here. the template parser makes sure // that templates are sane and statements are closed. - return b_string_free(str, false); + return sb_string_free(str, false); } diff --git a/src/renderer.h b/src/renderer.h index 15204e6..3ff62e7 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -10,15 +10,15 @@ #define _RENDERER_H #include <stdbool.h> -#include "utils/utils.h" +#include <squareball.h> -const char* blogc_get_variable(const char *name, b_trie_t *global, b_trie_t *local); -char* blogc_format_date(const char *date, b_trie_t *global, b_trie_t *local); -char* blogc_format_variable(const char *name, b_trie_t *global, b_trie_t *local, - b_slist_t *foreach_var); -b_slist_t* blogc_split_list_variable(const char *name, b_trie_t *global, - b_trie_t *local); -char* blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, +const char* blogc_get_variable(const char *name, sb_trie_t *global, sb_trie_t *local); +char* blogc_format_date(const char *date, sb_trie_t *global, sb_trie_t *local); +char* blogc_format_variable(const char *name, sb_trie_t *global, sb_trie_t *local, + sb_slist_t *foreach_var); +sb_slist_t* blogc_split_list_variable(const char *name, sb_trie_t *global, + sb_trie_t *local); +char* blogc_render(sb_slist_t *tmpl, sb_slist_t *sources, sb_trie_t *config, bool listing); #endif /* _RENDERER_H */ diff --git a/src/source-parser.c b/src/source-parser.c index 65fdd4e..e3662a2 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -13,7 +13,7 @@ #include <stdbool.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "content-parser.h" #include "source-parser.h" #include "error.h" @@ -30,7 +30,7 @@ typedef enum { } blogc_source_parser_state_t; -b_trie_t* +sb_trie_t* blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) { if (err == NULL || *err != NULL) @@ -43,7 +43,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) char *key = NULL; char *tmp = NULL; char *content = NULL; - b_trie_t *rv = b_trie_new(free); + sb_trie_t *rv = sb_trie_new(free); blogc_source_parser_state_t state = SOURCE_START; @@ -73,7 +73,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') break; if (c == ':') { - key = b_strndup(src + start, current - start); + key = sb_strndup(src + start, current - start); if (((current - start == 8) && (0 == strncmp("FILENAME", src + start, 8))) || ((current - start == 7) && @@ -122,8 +122,8 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) case SOURCE_CONFIG_VALUE: if (c == '\n' || c == '\r') { - tmp = b_strndup(src + start, current - start); - b_trie_insert(rv, key, b_strdup(b_str_strip(tmp))); + tmp = sb_strndup(src + start, current - start); + sb_trie_insert(rv, key, sb_strdup(sb_str_strip(tmp))); free(tmp); free(key); key = NULL; @@ -152,12 +152,12 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) case SOURCE_CONTENT: if (current == (src_len - 1)) { - tmp = b_strndup(src + start, src_len - start); - b_trie_insert(rv, "RAW_CONTENT", tmp); + tmp = sb_strndup(src + start, src_len - start); + sb_trie_insert(rv, "RAW_CONTENT", tmp); content = blogc_content_parse(tmp, &end_excerpt); - b_trie_insert(rv, "CONTENT", content); - b_trie_insert(rv, "EXCERPT", end_excerpt == 0 ? - b_strdup(content) : b_strndup(content, end_excerpt)); + sb_trie_insert(rv, "CONTENT", content); + sb_trie_insert(rv, "EXCERPT", end_excerpt == 0 ? + sb_strdup(content) : sb_strndup(content, end_excerpt)); } break; } @@ -168,7 +168,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) current++; } - if (*err == NULL && b_trie_size(rv) == 0) { + if (*err == NULL && sb_trie_size(rv) == 0) { // ok, nothing found in the config trie, but no error set either. // let's try to be nice with the users and provide some reasonable @@ -202,7 +202,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) if (*err != NULL) { free(key); - b_trie_free(rv); + sb_trie_free(rv); return NULL; } diff --git a/src/source-parser.h b/src/source-parser.h index f359f9e..460aedb 100644 --- a/src/source-parser.h +++ b/src/source-parser.h @@ -10,10 +10,10 @@ #define _SOURCE_PARSER_H #include <stdlib.h> -#include "utils/utils.h" +#include <squareball.h> #include "error.h" -b_trie_t* blogc_source_parse(const char *src, size_t src_len, +sb_trie_t* blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err); #endif /* _SOURCE_PARSER_H */ diff --git a/src/template-parser.c b/src/template-parser.c index 1d9046e..6f23ef1 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -13,7 +13,7 @@ #include <stdbool.h> #include <string.h> -#include "utils/utils.h" +#include <squareball.h> #include "template-parser.h" #include "error.h" @@ -52,7 +52,7 @@ typedef enum { } blogc_template_parser_block_state_t; -b_slist_t* +sb_slist_t* blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) { if (err == NULL || *err != NULL) @@ -71,7 +71,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) unsigned int if_count = 0; bool foreach_open = false; - b_slist_t *stmts = NULL; + sb_slist_t *stmts = NULL; blogc_template_stmt_t *stmt = NULL; /* @@ -100,21 +100,21 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) case TEMPLATE_START: if (last) { - stmt = b_malloc(sizeof(blogc_template_stmt_t)); + stmt = sb_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; if (lstrip_next) { - tmp = b_strndup(src + start, src_len - start); - stmt->value = b_strdup(b_str_lstrip(tmp)); + tmp = sb_strndup(src + start, src_len - start); + stmt->value = sb_strdup(sb_str_lstrip(tmp)); free(tmp); tmp = NULL; lstrip_next = false; } else { - stmt->value = b_strndup(src + start, src_len - start); + stmt->value = sb_strndup(src + start, src_len - start); } stmt->op = 0; stmt->value2 = NULL; - stmts = b_slist_append(stmts, stmt); + stmts = sb_slist_append(stmts, stmt); previous = stmt; stmt = NULL; } @@ -131,21 +131,21 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else state = TEMPLATE_VARIABLE_START; if (end > start) { - stmt = b_malloc(sizeof(blogc_template_stmt_t)); + stmt = sb_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; if (lstrip_next) { - tmp = b_strndup(src + start, end - start); - stmt->value = b_strdup(b_str_lstrip(tmp)); + tmp = sb_strndup(src + start, end - start); + stmt->value = sb_strdup(sb_str_lstrip(tmp)); free(tmp); tmp = NULL; lstrip_next = false; } else { - stmt->value = b_strndup(src + start, end - start); + stmt->value = sb_strndup(src + start, end - start); } stmt->op = 0; stmt->value2 = NULL; - stmts = b_slist_append(stmts, stmt); + stmts = sb_slist_append(stmts, stmt); previous = stmt; stmt = NULL; } @@ -159,7 +159,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) if ((previous != NULL) && (previous->type == BLOGC_TEMPLATE_CONTENT_STMT)) { - previous->value = b_str_rstrip(previous->value); // does not need copy + previous->value = sb_str_rstrip(previous->value); // does not need copy } state = TEMPLATE_BLOCK_START; break; @@ -559,19 +559,19 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) op_start = 0; op_end = 0; } - stmt = b_malloc(sizeof(blogc_template_stmt_t)); + stmt = sb_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = NULL; stmt->op = tmp_op; stmt->value2 = NULL; if (end > start) - stmt->value = b_strndup(src + start, end - start); + stmt->value = sb_strndup(src + start, end - start); if (end2 > start2) { - stmt->value2 = b_strndup(src + start2, end2 - start2); + stmt->value2 = sb_strndup(src + start2, end2 - start2); start2 = 0; end2 = 0; } - stmts = b_slist_append(stmts, stmt); + stmts = sb_slist_append(stmts, stmt); previous = stmt; stmt = NULL; state = TEMPLATE_START; @@ -622,9 +622,9 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) void -blogc_template_free_stmts(b_slist_t *stmts) +blogc_template_free_stmts(sb_slist_t *stmts) { - for (b_slist_t *tmp = stmts; tmp != NULL; tmp = tmp->next) { + for (sb_slist_t *tmp = stmts; tmp != NULL; tmp = tmp->next) { blogc_template_stmt_t *data = tmp->data; if (data == NULL) continue; @@ -632,5 +632,5 @@ blogc_template_free_stmts(b_slist_t *stmts) free(data->value2); free(data); } - b_slist_free(stmts); + sb_slist_free(stmts); } diff --git a/src/template-parser.h b/src/template-parser.h index 6cd2c80..de5113e 100644 --- a/src/template-parser.h +++ b/src/template-parser.h @@ -9,7 +9,7 @@ #ifndef _TEMPLATE_PARSER_H #define _TEMPLATE_PARSER_H -#include "utils/utils.h" +#include <squareball.h> #include "error.h" /* @@ -44,8 +44,8 @@ typedef struct { blogc_template_stmt_operator_t op; } blogc_template_stmt_t; -b_slist_t* blogc_template_parse(const char *src, size_t src_len, +sb_slist_t* blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err); -void blogc_template_free_stmts(b_slist_t *stmts); +void blogc_template_free_stmts(sb_slist_t *stmts); #endif /* _TEMPLATE_GRAMMAR_H */ diff --git a/src/utils/mem.c b/src/utils/mem.c deleted file mode 100644 index 693d555..0000000 --- a/src/utils/mem.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /* HAVE_CONFIG_H */ - -#include <stdlib.h> -#include <stdio.h> -#include "utils.h" - - -void* -b_malloc(size_t size) -{ - // simple things simple! - void *rv = malloc(size); - if (rv == NULL) { - fprintf(stderr, "fatal error: Failed to allocate memory!\n"); - exit(1); - } - return rv; -} - - -void* -b_realloc(void *ptr, size_t size) -{ - // simple things even simpler :P - void *rv = realloc(ptr, size); - if (rv == NULL && size != 0) { - fprintf(stderr, "fatal error: Failed to reallocate memory!\n"); - free(ptr); - exit(1); - } - return rv; -} diff --git a/src/utils/slist.c b/src/utils/slist.c deleted file mode 100644 index 9753aa7..0000000 --- a/src/utils/slist.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /* HAVE_CONFIG_H */ - -#include <stdlib.h> -#include "utils.h" - - -b_slist_t* -b_slist_append(b_slist_t *l, void *data) -{ - b_slist_t *node = malloc(sizeof(b_slist_t)); - if (node == NULL) { - l = NULL; - return l; - } - node->data = data; - node->next = NULL; - if (l == NULL) - l = node; - else { - b_slist_t *tmp; - for (tmp = l; tmp->next != NULL; tmp = tmp->next); - tmp->next = node; - } - return l; -} - - -void -b_slist_free_full(b_slist_t *l, b_free_func_t free_func) -{ - while (l != NULL) { - b_slist_t *tmp = l->next; - free_func(l->data); - free(l); - l = tmp; - } -} - - -void -b_slist_free(b_slist_t *l) -{ - while (l != NULL) { - b_slist_t *tmp = l->next; - free(l); - l = tmp; - } -} - - -unsigned int -b_slist_length(b_slist_t *l) -{ - unsigned int i; - b_slist_t *tmp; - for (tmp = l, i = 0; tmp != NULL; tmp = tmp->next, i++); - return i; -} diff --git a/src/utils/strings.c b/src/utils/strings.c deleted file mode 100644 index 3151612..0000000 --- a/src/utils/strings.c +++ /dev/null @@ -1,314 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /* HAVE_CONFIG_H */ - -#include <ctype.h> -#include <string.h> -#include <stdarg.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include "utils.h" - - -char* -b_strdup(const char *s) -{ - if (s == NULL) - return NULL; - size_t l = strlen(s); - char *tmp = malloc(l + 1); - if (tmp == NULL) - return NULL; - memcpy(tmp, s, l + 1); - return tmp; -} - - -char* -b_strndup(const char *s, size_t n) -{ - if (s == NULL) - return NULL; - size_t l = strnlen(s, n); - char *tmp = malloc(l + 1); - if (tmp == NULL) - return NULL; - memcpy(tmp, s, l); - tmp[l] = '\0'; - return tmp; -} - - -char* -b_strdup_vprintf(const char *format, va_list ap) -{ - va_list ap2; - va_copy(ap2, ap); - int l = vsnprintf(NULL, 0, format, ap2); - va_end(ap2); - if (l < 0) - return NULL; - char *tmp = malloc(l + 1); - if (!tmp) - return NULL; - int l2 = vsnprintf(tmp, l + 1, format, ap); - if (l2 < 0) { - free(tmp); - return NULL; - } - return tmp; -} - - -char* -b_strdup_printf(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - char *tmp = b_strdup_vprintf(format, ap); - va_end(ap); - return tmp; -} - - -bool -b_str_starts_with(const char *str, const char *prefix) -{ - int str_l = strlen(str); - int str_lp = strlen(prefix); - if (str_lp > str_l) - return false; - return strncmp(str, prefix, str_lp) == 0; -} - - -bool -b_str_ends_with(const char *str, const char *suffix) -{ - int str_l = strlen(str); - int str_ls = strlen(suffix); - if (str_ls > str_l) - return false; - return strcmp(str + str_l - str_ls, suffix) == 0; -} - - -char* -b_str_lstrip(char *str) -{ - if (str == NULL) - return NULL; - int i; - size_t str_len = strlen(str); - for (i = 0; i < str_len; i++) { - if ((str[i] != ' ') && (str[i] != '\t') && (str[i] != '\n') && - (str[i] != '\r') && (str[i] != '\t') && (str[i] != '\f') && - (str[i] != '\v')) - { - str += i; - break; - } - if (i == str_len - 1) { - str += str_len; - break; - } - } - return str; -} - - -char* -b_str_rstrip(char *str) -{ - if (str == NULL) - return NULL; - int i; - size_t str_len = strlen(str); - for (i = str_len - 1; i >= 0; i--) { - if ((str[i] != ' ') && (str[i] != '\t') && (str[i] != '\n') && - (str[i] != '\r') && (str[i] != '\t') && (str[i] != '\f') && - (str[i] != '\v')) - { - str[i + 1] = '\0'; - break; - } - if (i == 0) { - str[0] = '\0'; - break; - } - } - return str; -} - - -char* -b_str_strip(char *str) -{ - return b_str_lstrip(b_str_rstrip(str)); -} - - -char** -b_str_split(const char *str, char c, unsigned int max_pieces) -{ - if (!str) - return NULL; - char **rv = b_malloc(sizeof(char*)); - unsigned int i, start = 0, count = 0; - for (i = 0; i < strlen(str) + 1; i++) { - if (str[0] == '\0') - break; - if ((str[i] == c && (!max_pieces || count + 1 < max_pieces)) || str[i] == '\0') { - rv = b_realloc(rv, (count + 1) * sizeof(char*)); - rv[count] = b_malloc(i - start + 1); - memcpy(rv[count], str + start, i - start); - rv[count++][i - start] = '\0'; - start = i + 1; - } - } - rv = b_realloc(rv, (count + 1) * sizeof(char*)); - rv[count] = NULL; - return rv; -} - - -char* -b_str_replace(const char *str, const char search, const char *replace) -{ - char **pieces = b_str_split(str, search, 0); - if (pieces == NULL) - return NULL; - char* rv = b_strv_join((const char**) pieces, replace); - b_strv_free(pieces); - return rv; -} - - -void -b_strv_free(char **strv) -{ - if (strv == NULL) - return; - unsigned int i; - for (i = 0; strv[i] != NULL; i++) - free(strv[i]); - free(strv); -} - - -char* -b_strv_join(const char **strv, const char *separator) -{ - if (strv == NULL) - return NULL; - unsigned int i = 0; - b_string_t *str = b_string_new(); - for (i = 0; strv[i] != NULL; i++) { - str = b_string_append(str, strv[i]); - if (strv[i+1] != NULL) - str = b_string_append(str, separator); - } - return b_string_free(str, false); -} - - -unsigned int -b_strv_length(char **strv) -{ - if (!strv) - return 0; - unsigned int i; - for (i = 0; strv[i] != NULL; i++); - return i; -} - - -b_string_t* -b_string_new(void) -{ - b_string_t* rv = b_malloc(sizeof(b_string_t)); - rv->str = NULL; - rv->len = 0; - rv->allocated_len = 0; - - // initialize with empty string - rv = b_string_append(rv, ""); - - return rv; -} - - -char* -b_string_free(b_string_t *str, bool free_str) -{ - char *rv = NULL; - if (free_str) - free(str->str); - else - rv = str->str; - free(str); - return rv; -} - - -b_string_t* -b_string_append_len(b_string_t *str, const char *suffix, size_t len) -{ - if (suffix == NULL) - return str; - size_t old_len = str->len; - str->len += len; - if (str->len + 1 > str->allocated_len) { - str->allocated_len = (((str->len + 1) / B_STRING_CHUNK_SIZE) + 1) * B_STRING_CHUNK_SIZE; - str->str = b_realloc(str->str, str->allocated_len); - } - memcpy(str->str + old_len, suffix, len); - str->str[str->len] = '\0'; - return str; -} - - -b_string_t* -b_string_append(b_string_t *str, const char *suffix) -{ - if (suffix == NULL) - return str; - return b_string_append_len(str, suffix, strlen(suffix)); -} - - -b_string_t* -b_string_append_c(b_string_t *str, char c) -{ - size_t old_len = str->len; - str->len += 1; - if (str->len + 1 > str->allocated_len) { - str->allocated_len = (((str->len + 1) / B_STRING_CHUNK_SIZE) + 1) * B_STRING_CHUNK_SIZE; - str->str = b_realloc(str->str, str->allocated_len); - } - str->str[old_len] = c; - str->str[str->len] = '\0'; - return str; -} - - -b_string_t* -b_string_append_printf(b_string_t *str, const char *format, ...) -{ - va_list ap; - va_start(ap, format); - char *tmp = b_strdup_vprintf(format, ap); - va_end(ap); - str = b_string_append(str, tmp); - free(tmp); - return str; -} diff --git a/src/utils/trie.c b/src/utils/trie.c deleted file mode 100644 index b8c1e63..0000000 --- a/src/utils/trie.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif /* HAVE_CONFIG_H */ - -#include <stdlib.h> -#include "utils.h" - - -b_trie_t* -b_trie_new(b_free_func_t free_func) -{ - b_trie_t *trie = b_malloc(sizeof(b_trie_t)); - trie->root = NULL; - trie->free_func = free_func; - return trie; -} - - -static void -b_trie_free_node(b_trie_t *trie, b_trie_node_t *node) -{ - if (node == NULL) - return; - if (node->data != NULL && trie->free_func != NULL) - trie->free_func(node->data); - b_trie_free_node(trie, node->next); - b_trie_free_node(trie, node->child); - free(node); -} - - -void -b_trie_free(b_trie_t *trie) -{ - if (trie == NULL) - return; - b_trie_free_node(trie, trie->root); - free(trie); -} - - -void -b_trie_insert(b_trie_t *trie, const char *key, void *data) -{ - if (data == NULL || key == NULL) - return; - - b_trie_node_t *parent = NULL; - b_trie_node_t *previous; - b_trie_node_t *current; - b_trie_node_t *tmp; - - while (1) { - - if (trie->root == NULL || (parent != NULL && parent->child == NULL)) { - current = b_malloc(sizeof(b_trie_node_t)); - current->key = *key; - current->data = NULL; - current->next = NULL; - current->child = NULL; - if (trie->root == NULL) - trie->root = current; - else - parent->child = current; - parent = current; - goto clean; - } - - tmp = parent == NULL ? trie->root : parent->child; - previous = NULL; - - while (tmp != NULL && tmp->key != *key) { - previous = tmp; - tmp = tmp->next; - } - - parent = tmp; - - if (previous == NULL || parent != NULL) - goto clean; - - current = b_malloc(sizeof(b_trie_node_t)); - current->key = *key; - current->data = NULL; - current->next = NULL; - current->child = NULL; - previous->next = current; - parent = current; - -clean: - if (*key == '\0') { - if (parent->data != NULL && trie->free_func != NULL) - trie->free_func(parent->data); - parent->data = data; - break; - } - key++; - } -} - - -void* -b_trie_lookup(b_trie_t *trie, const char *key) -{ - if (trie->root == NULL || key == NULL) - return NULL; - - b_trie_node_t *parent = trie->root; - b_trie_node_t *tmp; - while (1) { - for (tmp = parent; tmp != NULL; tmp = tmp->next) { - - if (tmp->key == *key) { - if (tmp->key == '\0') - return tmp->data; - parent = tmp->child; - break; - } - } - if (tmp == NULL) - return NULL; - - if (*key == '\0') - break; - key++; - } - return NULL; -} - - -static void -b_trie_size_node(b_trie_node_t *node, unsigned int *count) -{ - if (node == NULL) - return; - - if (node->key == '\0') - (*count)++; - - b_trie_size_node(node->next, count); - b_trie_size_node(node->child, count); -} - - -unsigned int -b_trie_size(b_trie_t *trie) -{ - if (trie == NULL) - return 0; - - unsigned int count = 0; - b_trie_size_node(trie->root, &count); - return count; -} - - -static void -b_trie_foreach_node(b_trie_node_t *node, b_string_t *str, void (*func)(const char *key, void *data)) -{ - if (node == NULL) - return; - - if (node->key == '\0') { - func(str->str, node->data); - b_string_free(str, true); - } - - if (node->child != NULL) { - b_string_t *child = b_string_new(); - child = b_string_append(child, str->str); - child = b_string_append_c(child, node->key); - b_trie_foreach_node(node->child, child, func); - } - - if (node->next != NULL) - b_trie_foreach_node(node->next, str, func); - - if (node->child != NULL && node->next == NULL) - b_string_free(str, true); -} - - -void -b_trie_foreach(b_trie_t *trie, void (*func)(const char *key, void *data)) -{ - if (trie->root == NULL) - return; - - b_string_t *str = b_string_new(); - b_trie_foreach_node(trie->root, str, func); -} diff --git a/src/utils/utils.h b/src/utils/utils.h deleted file mode 100644 index dc67497..0000000 --- a/src/utils/utils.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifndef _UTILS_UTILS_H -#define _UTILS_UTILS_H - -#include <stdbool.h> -#include <stdlib.h> -#include <stdarg.h> - -#define B_STRING_CHUNK_SIZE 128 - -typedef void (*b_free_func_t) (void *ptr); - -typedef struct _b_slist_t { - struct _b_slist_t *next; - void *data; -} b_slist_t; - -typedef struct _b_string_t { - char *str; - size_t len; - size_t allocated_len; -} b_string_t; - -typedef struct _b_trie_node_t { - char key; - void *data; - struct _b_trie_node_t *next, *child; -} b_trie_node_t; - -typedef struct _b_trie_t { - b_trie_node_t *root; - b_free_func_t free_func; -} b_trie_t; - -b_slist_t* b_slist_append(b_slist_t *l, void *data); -void b_slist_free_full(b_slist_t *l, b_free_func_t free_func); -void b_slist_free(b_slist_t *l); -unsigned int b_slist_length(b_slist_t *l); - -char* b_strdup(const char *s); -char* b_strndup(const char *s, size_t n); -char* b_strdup_vprintf(const char *format, va_list ap); -char* b_strdup_printf(const char *format, ...); -bool b_str_starts_with(const char *str, const char *prefix); -bool b_str_ends_with(const char *str, const char *suffix); -char* b_str_lstrip(char *str); -char* b_str_rstrip(char *str); -char* b_str_strip(char *str); -char** b_str_split(const char *str, char c, unsigned int max_pieces); -char* b_str_replace(const char *str, const char search, const char *replace); -void b_strv_free(char **strv); -char* b_strv_join(const char **strv, const char *separator); -unsigned int b_strv_length(char **strv); - -b_string_t* b_string_new(void); -char* b_string_free(b_string_t *str, bool free_str); -b_string_t* b_string_append_len(b_string_t *str, const char *suffix, size_t len); -b_string_t* b_string_append(b_string_t *str, const char *suffix); -b_string_t* b_string_append_c(b_string_t *str, char c); -b_string_t* b_string_append_printf(b_string_t *str, const char *format, ...); - -b_trie_t* b_trie_new(b_free_func_t free_func); -void b_trie_free(b_trie_t *trie); -void b_trie_insert(b_trie_t *trie, const char *key, void *data); -void* b_trie_lookup(b_trie_t *trie, const char *key); -unsigned int b_trie_size(b_trie_t *trie); -void b_trie_foreach(b_trie_t *trie, void (*func)(const char *key, void *data)); - -void* b_malloc(size_t size); -void* b_realloc(void *ptr, size_t size); - -#endif /* _UTILS_UTILS_H */ |