Skip to content

Commit

Permalink
Merge changes from v4
Browse files Browse the repository at this point in the history
  • Loading branch information
mildsunrise committed Apr 7, 2015
1 parent 810c79d commit 2b890c1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bin/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
54 changes: 27 additions & 27 deletions bin/hoedown.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "html.h"

#include "common.h"
/*#include <time.h>*/
#include <time.h>


/* FEATURES INFO / DEFAULTS */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
57 changes: 55 additions & 2 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -74,6 +81,7 @@ void
hoedown_buffer_free(hoedown_buffer *buf)
{
if (!buf) return;
assert(buf && buf->unit);

buf->data_free(buf->data);

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
9 changes: 9 additions & 0 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);

Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 2b890c1

Please sign in to comment.