From 2b890c168326591e8d283cad7c2c9e6cb34d18db Mon Sep 17 00:00:00 2001 From: Xavier Mendez Date: Wed, 8 Apr 2015 00:14:24 +0200 Subject: Merge changes from v4 --- bin/common.h | 2 +- bin/hoedown.c | 54 +++++++++++++++++++++++++++--------------------------- src/buffer.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/buffer.h | 9 +++++++++ src/document.c | 5 +++-- 5 files changed, 95 insertions(+), 32 deletions(-) diff --git a/bin/common.h b/bin/common.h index 5d55acc..e4d2eda 100644 --- a/bin/common.h +++ b/bin/common.h @@ -83,7 +83,7 @@ parse_options( result = parse_short_option(arg[pos], next, opaque); if (!result) return 0; if (result == 2) { - i++; + if (next == next_arg) i++; break; } } diff --git a/bin/hoedown.c b/bin/hoedown.c index 5aac21a..c0e5279 100644 --- a/bin/hoedown.c +++ b/bin/hoedown.c @@ -2,7 +2,7 @@ #include "html.h" #include "common.h" -/*#include */ +#include /* FEATURES INFO / DEFAULTS */ @@ -362,7 +362,7 @@ int main(int argc, char **argv) { struct option_data data; - /*struct timespec start, end;*/ + clock_t t1, t2; FILE *file = stdin; hoedown_buffer *ib, *ob; hoedown_renderer *renderer = NULL; @@ -398,13 +398,9 @@ main(int argc, char **argv) /* Read everything */ ib = hoedown_buffer_new(data.iunit); - while (!feof(file)) { - if (ferror(file)) { - fprintf(stderr, "I/O errors found while reading input.\n"); - return 5; - } - hoedown_buffer_grow(ib, ib->size + data.iunit); - ib->size += fread(ib->data + ib->size, 1, data.iunit, file); + if (hoedown_buffer_putf(ib, file)) { + fprintf(stderr, "I/O errors found while reading input.\n"); + return 5; } if (file != stdin) fclose(file); @@ -425,35 +421,39 @@ main(int argc, char **argv) ob = hoedown_buffer_new(data.ounit); document = hoedown_document_new(renderer, data.extensions, data.max_nesting); - /*clock_gettime(CLOCK_MONOTONIC, &start);*/ + t1 = clock(); hoedown_document_render(document, ob, ib->data, ib->size); - /*clock_gettime(CLOCK_MONOTONIC, &end);*/ - - /* Write the result to stdout */ - (void)fwrite(ob->data, 1, ob->size, stdout); - - /* Show rendering time */ - if (data.show_time) { - /*TODO: enable this - long long elapsed = (end.tv_sec - start.tv_sec)*1e9 + (end.tv_nsec - start.tv_nsec); - if (elapsed < 1e9) - fprintf(stderr, "Time spent on rendering: %.2f ms.\n", ((double)elapsed)/1e6); - else - fprintf(stderr, "Time spent on rendering: %.3f s.\n", ((double)elapsed)/1e9); - */ - } + t2 = clock(); /* Cleanup */ hoedown_buffer_free(ib); - hoedown_buffer_free(ob); - hoedown_document_free(document); renderer_free(renderer); + /* Write the result to stdout */ + (void)fwrite(ob->data, 1, ob->size, stdout); + hoedown_buffer_free(ob); + if (ferror(stdout)) { fprintf(stderr, "I/O errors found while writing output.\n"); return 5; } + /* Show rendering time */ + if (data.show_time) { + double elapsed; + + if (t1 == -1 || t2 == -1) { + fprintf(stderr, "Failed to get the time.\n"); + return 1; + } + + elapsed = (double)(t2 - t1) / CLOCKS_PER_SEC; + if (elapsed < 1) + fprintf(stderr, "Time spent on rendering: %7.2f ms.\n", elapsed*1e3); + else + fprintf(stderr, "Time spent on rendering: %6.3f s.\n", elapsed); + } + return 0; } diff --git a/src/buffer.c b/src/buffer.c index 2ca0fd1..af77b70 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -62,6 +62,13 @@ hoedown_buffer_init( buf->buffer_free = buffer_free; } +void +hoedown_buffer_uninit(hoedown_buffer *buf) +{ + assert(buf && buf->unit); + buf->data_free(buf->data); +} + hoedown_buffer * hoedown_buffer_new(size_t unit) { @@ -74,6 +81,7 @@ void hoedown_buffer_free(hoedown_buffer *buf) { if (!buf) return; + assert(buf && buf->unit); buf->data_free(buf->data); @@ -138,6 +146,19 @@ hoedown_buffer_putc(hoedown_buffer *buf, uint8_t c) buf->size += 1; } +int +hoedown_buffer_putf(hoedown_buffer *buf, FILE *file) +{ + assert(buf && buf->unit); + + while (!(feof(file) || ferror(file))) { + hoedown_buffer_grow(buf, buf->size + buf->unit); + buf->size += fread(buf->data + buf->size, 1, buf->unit, file); + } + + return ferror(file); +} + void hoedown_buffer_set(hoedown_buffer *buf, const uint8_t *data, size_t size) { @@ -174,8 +195,6 @@ hoedown_buffer_prefix(const hoedown_buffer *buf, const char *prefix) { size_t i; - assert(buf && buf->unit); - for (i = 0; i < buf->size; ++i) { if (prefix[i] == 0) return 0; @@ -253,3 +272,37 @@ hoedown_buffer_printf(hoedown_buffer *buf, const char *fmt, ...) buf->size += n; } + +void hoedown_buffer_put_utf8(hoedown_buffer *buf, unsigned int c) { + unsigned char unichar[4]; + + assert(buf && buf->unit); + + if (c < 0x80) { + hoedown_buffer_putc(buf, c); + } + else if (c < 0x800) { + unichar[0] = 192 + (c / 64); + unichar[1] = 128 + (c % 64); + hoedown_buffer_put(buf, unichar, 2); + } + else if (c - 0xd800u < 0x800) { + HOEDOWN_BUFPUTSL(buf, "\xef\xbf\xbd"); + } + else if (c < 0x10000) { + unichar[0] = 224 + (c / 4096); + unichar[1] = 128 + (c / 64) % 64; + unichar[2] = 128 + (c % 64); + hoedown_buffer_put(buf, unichar, 3); + } + else if (c < 0x110000) { + unichar[0] = 240 + (c / 262144); + unichar[1] = 128 + (c / 4096) % 64; + unichar[2] = 128 + (c / 64) % 64; + unichar[3] = 128 + (c % 64); + hoedown_buffer_put(buf, unichar, 4); + } + else { + HOEDOWN_BUFPUTSL(buf, "\xef\xbf\xbd"); + } +} diff --git a/src/buffer.h b/src/buffer.h index fcda83c..d7703f8 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -59,6 +59,9 @@ void hoedown_buffer_init( hoedown_free_callback buffer_free ); +/* hoedown_buffer_uninit: uninitialize an existing buffer */ +void hoedown_buffer_uninit(hoedown_buffer *buf); + /* hoedown_buffer_new: allocate a new buffer */ hoedown_buffer *hoedown_buffer_new(size_t unit) __attribute__ ((malloc)); @@ -77,6 +80,9 @@ void hoedown_buffer_puts(hoedown_buffer *buf, const char *str); /* hoedown_buffer_putc: append a single char to a buffer */ void hoedown_buffer_putc(hoedown_buffer *buf, uint8_t c); +/* hoedown_buffer_putf: read from a file and append to a buffer, until EOF or error */ +int hoedown_buffer_putf(hoedown_buffer *buf, FILE* file); + /* hoedown_buffer_set: replace the buffer's contents with raw data */ void hoedown_buffer_set(hoedown_buffer *buf, const uint8_t *data, size_t size); @@ -101,6 +107,9 @@ const char *hoedown_buffer_cstr(hoedown_buffer *buf); /* hoedown_buffer_printf: formatted printing to a buffer */ void hoedown_buffer_printf(hoedown_buffer *buf, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); +/* hoedown_buffer_put_utf8: put a Unicode character encoded as UTF-8 */ +void hoedown_buffer_put_utf8(hoedown_buffer *buf, unsigned int codepoint); + /* hoedown_buffer_free: free the buffer */ void hoedown_buffer_free(hoedown_buffer *buf); diff --git a/src/document.c b/src/document.c index 47f6cf2..613fd6d 100644 --- a/src/document.c +++ b/src/document.c @@ -459,7 +459,7 @@ tag_length(uint8_t *data, size_t size, hoedown_autolink_type *autolink) static void parse_inline(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t size) { - size_t i = 0, end = 0; + size_t i = 0, end = 0, consumed = 0; hoedown_buffer work = { 0, 0, 0, 0, NULL, NULL, NULL }; uint8_t *active_char = doc->active_char; @@ -483,12 +483,13 @@ parse_inline(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t si if (end >= size) break; i = end; - end = markdown_char_ptrs[ (int)active_char[data[end]] ](ob, doc, data + i, i, size - i); + end = markdown_char_ptrs[ (int)active_char[data[end]] ](ob, doc, data + i, i - consumed, size - i); if (!end) /* no action from the callback */ end = i + 1; else { i += end; end = i; + consumed = i; } } } -- cgit v1.2.1