From 200e36c0028a0f2dc43748c39074acf5e2d8283e Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Mon, 8 Jun 2009 23:30:44 +0300 Subject: [PATCH] bug 1080: Fold dump_color_mode* functions together Instead of having four separate function definitions, have just one sprinkled with #ifdefs, and #include that four times. The purpose being to make it clearer which parts of these functions are identical and which ones differ. As a side effect, this change makes ELinks ignore --dump-color-mode when dumping in UTF-8. Colourful UTF-8 dumping has not been implemented and the fallback is now different from before. --- src/viewer/dump/dump-color-mode.h | 239 +++++++++++++++ src/viewer/dump/dump.c | 489 +----------------------------- 2 files changed, 249 insertions(+), 479 deletions(-) create mode 100644 src/viewer/dump/dump-color-mode.h diff --git a/src/viewer/dump/dump-color-mode.h b/src/viewer/dump/dump-color-mode.h new file mode 100644 index 00000000..1fac4daf --- /dev/null +++ b/src/viewer/dump/dump-color-mode.h @@ -0,0 +1,239 @@ +/* Color modes for dumping to a file. + * + * dump.c includes this file multiple times, once for each color mode. + * Each time, it defines at most one of the following macros: + * + * - DUMP_COLOR_MODE_16 + * - DUMP_COLOR_MODE_256 + * - DUMP_COLOR_MODE_TRUE + * + * This file then defines a dumping function specifically for that + * color mode. This is supposedly faster than runtime checks. */ + +#ifdef DUMP_COLOR_MODE_16 +static int +dump_to_file_16(struct document *document, int fd) +#elif defined(DUMP_COLOR_MODE_256) +static int +dump_to_file_256(struct document *document, int fd) +#elif defined(DUMP_COLOR_MODE_TRUE) +static int +dump_to_file_true_color(struct document *document, int fd) +#else +int +dump_to_file(struct document *document, int fd) +#endif +{ + int y; + int bptr = 0; + unsigned char *buf = mem_alloc(D_BUF); +#ifdef DUMP_COLOR_MODE_16 + unsigned char color = 0; + int width = get_opt_int("document.dump.width"); +#elif defined(DUMP_COLOR_MODE_256) + unsigned char foreground = 0; + unsigned char background = 0; + int width = get_opt_int("document.dump.width"); +#elif defined(DUMP_COLOR_MODE_TRUE) + static unsigned char color[6] = {255, 255, 255, 0, 0, 0}; + unsigned char *foreground = &color[0]; + unsigned char *background = &color[3]; + int width = get_opt_int("document.dump.width"); +#endif /* DUMP_COLOR_MODE_TRUE */ + + if (!buf) return -1; + +#ifdef CONFIG_UTF8 + if (is_cp_utf8(document->options.cp)) + goto utf8; +#endif /* CONFIG_UTF8 */ + + for (y = 0; y < document->height; y++) { + int white = 0; + int x; + +#ifdef DUMP_COLOR_MODE_16 + write_color_16(color, fd, buf, &bptr); +#elif defined(DUMP_COLOR_MODE_256) + write_color_256("38", foreground, fd, buf, &bptr); + write_color_256("48", background, fd, buf, &bptr); +#elif defined(DUMP_COLOR_MODE_TRUE) + write_true_color("38", foreground, fd, buf, &bptr); + write_true_color("48", background, fd, buf, &bptr); +#endif /* DUMP_COLOR_MODE_TRUE */ + + for (x = 0; x < document->data[y].length; x++) { + unsigned char c; + unsigned char attr = document->data[y].chars[x].attr; +#ifdef DUMP_COLOR_MODE_16 + unsigned char color1 = document->data[y].chars[x].color[0]; + + if (color != color1) { + color = color1; + if (write_color_16(color, fd, buf, &bptr)) + goto fail; + } +#elif defined(DUMP_COLOR_MODE_256) + unsigned char color1 = document->data[y].chars[x].color[0]; + unsigned char color2 = document->data[y].chars[x].color[1]; + + if (foreground != color1) { + foreground = color1; + if (write_color_256("38", foreground, fd, buf, &bptr)) + goto fail; + } + + if (background != color2) { + background = color2; + if (write_color_256("48", background, fd, buf, &bptr)) + goto fail; + } +#elif defined(DUMP_COLOR_MODE_TRUE) + unsigned char *new_foreground = &document->data[y].chars[x].color[0]; + unsigned char *new_background = &document->data[y].chars[x].color[3]; + + if (memcmp(foreground, new_foreground, 3)) { + foreground = new_foreground; + if (write_true_color("38", foreground, fd, buf, &bptr)) + goto fail; + } + + if (memcmp(background, new_background, 3)) { + background = new_background; + if (write_true_color("48", background, fd, buf, &bptr)) + goto fail; + } +#endif /* DUMP_COLOR_MODE_TRUE */ + + c = document->data[y].chars[x].data; + + if ((attr & SCREEN_ATTR_FRAME) + && c >= 176 && c < 224) + c = frame_dumb[c - 176]; + + if (c <= ' ') { + /* Count spaces. */ + white++; + continue; + } + + /* Print spaces if any. */ + while (white) { + if (write_char(' ', fd, buf, &bptr)) + goto fail; + white--; + } + + /* Print normal char. */ + if (write_char(c, fd, buf, &bptr)) + goto fail; + } +#if defined(DUMP_COLOR_MODE_16) || defined(DUMP_COLOR_MODE_256) || defined(DUMP_COLOR_MODE_TRUE) + for (;x < width; x++) { + if (write_char(' ', fd, buf, &bptr)) + goto fail; + } +#endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */ + + /* Print end of line. */ + if (write_char('\n', fd, buf, &bptr)) + goto fail; + } +#ifdef CONFIG_UTF8 + goto ref; +utf8: + for (y = 0; y < document->height; y++) { + int white = 0; + int x; + + for (x = 0; x < document->data[y].length; x++) { + unicode_val_T c; + unsigned char attr = document->data[y].chars[x].attr; + + c = document->data[y].chars[x].data; + + if ((attr & SCREEN_ATTR_FRAME) + && c >= 176 && c < 224) + c = frame_dumb[c - 176]; + else { + unsigned char *utf8_buf = encode_utf8(c); + + while (*utf8_buf) { + if (write_char(*utf8_buf++, + fd, buf, &bptr)) goto fail; + } + + x += unicode_to_cell(c) - 1; + + continue; + } + + if (c <= ' ') { + /* Count spaces. */ + white++; + continue; + } + + /* Print spaces if any. */ + while (white) { + if (write_char(' ', fd, buf, &bptr)) + goto fail; + white--; + } + + /* Print normal char. */ + if (write_char(c, fd, buf, &bptr)) + goto fail; + } + + /* Print end of line. */ + if (write_char('\n', fd, buf, &bptr)) + goto fail; + } +ref: +#endif /* CONFIG_UTF8 */ + + if (hard_write(fd, buf, bptr) != bptr) { +fail: + mem_free(buf); + return -1; + } + + if (document->nlinks && get_opt_bool("document.dump.references")) { + int x; + unsigned char *header = "\nReferences\n\n Visible links\n"; + int headlen = strlen(header); + + if (hard_write(fd, header, headlen) != headlen) + goto fail; + + for (x = 0; x < document->nlinks; x++) { + struct link *link = &document->links[x]; + unsigned char *where = link->where; + + if (!where) continue; + + if (document->options.links_numbering) { + if (link->title && *link->title) + snprintf(buf, D_BUF, "%4d. %s\n\t%s\n", + x + 1, link->title, where); + else + snprintf(buf, D_BUF, "%4d. %s\n", + x + 1, where); + } else { + if (link->title && *link->title) + snprintf(buf, D_BUF, " . %s\n\t%s\n", + link->title, where); + else + snprintf(buf, D_BUF, " . %s\n", where); + } + + bptr = strlen(buf); + if (hard_write(fd, buf, bptr) != bptr) + goto fail; + } + } + + mem_free(buf); + return 0; +} diff --git a/src/viewer/dump/dump.c b/src/viewer/dump/dump.c index f240129c..847ee194 100644 --- a/src/viewer/dump/dump.c +++ b/src/viewer/dump/dump.c @@ -471,111 +471,9 @@ write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr) return 0; } - -static int -dump_to_file_16(struct document *document, int fd) -{ - int y; - int bptr = 0; - unsigned char *buf = mem_alloc(D_BUF); - unsigned char color = 0; - int width = get_opt_int("document.dump.width"); - - if (!buf) return -1; - - for (y = 0; y < document->height; y++) { - int white = 0; - int x; - - write_color_16(color, fd, buf, &bptr); - for (x = 0; x < document->data[y].length; x++) { - unsigned char c; - unsigned char attr = document->data[y].chars[x].attr; - unsigned char color1 = document->data[y].chars[x].color[0]; - - if (color != color1) { - color = color1; - if (write_color_16(color, fd, buf, &bptr)) - goto fail; - } - - c = document->data[y].chars[x].data; - - if ((attr & SCREEN_ATTR_FRAME) - && c >= 176 && c < 224) - c = frame_dumb[c - 176]; - - if (c <= ' ') { - /* Count spaces. */ - white++; - continue; - } - - /* Print spaces if any. */ - while (white) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - white--; - } - - /* Print normal char. */ - if (write_char(c, fd, buf, &bptr)) - goto fail; - } - for (;x < width; x++) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - } - - /* Print end of line. */ - if (write_char('\n', fd, buf, &bptr)) - goto fail; - } - - if (hard_write(fd, buf, bptr) != bptr) { -fail: - mem_free(buf); - return -1; - } - - if (document->nlinks && get_opt_bool("document.dump.references")) { - int x; - unsigned char *header = "\nReferences\n\n Visible links\n"; - int headlen = strlen(header); - - if (hard_write(fd, header, headlen) != headlen) - goto fail; - - for (x = 0; x < document->nlinks; x++) { - struct link *link = &document->links[x]; - unsigned char *where = link->where; - - if (!where) continue; - - if (document->options.links_numbering) { - if (link->title && *link->title) - snprintf(buf, D_BUF, "%4d. %s\n\t%s\n", - x + 1, link->title, where); - else - snprintf(buf, D_BUF, "%4d. %s\n", - x + 1, where); - } else { - if (link->title && *link->title) - snprintf(buf, D_BUF, " . %s\n\t%s\n", - link->title, where); - else - snprintf(buf, D_BUF, " . %s\n", where); - } - - bptr = strlen(buf); - if (hard_write(fd, buf, bptr) != bptr) - goto fail; - } - } - - mem_free(buf); - return 0; -} +#define DUMP_COLOR_MODE_16 +#include "dump-color-mode.h" +#undef DUMP_COLOR_MODE_16 /* configure --enable-debug uses gcc -Wall -Werror, and -Wall includes * -Wunused-function, so declaring or defining any unused function @@ -595,119 +493,9 @@ write_color_256(unsigned char *str, unsigned char color, int fd, unsigned char * return 0; } -static int -dump_to_file_256(struct document *document, int fd) -{ - int y; - int bptr = 0; - unsigned char *buf = mem_alloc(D_BUF); - unsigned char foreground = 0; - unsigned char background = 0; - int width = get_opt_int("document.dump.width"); - - if (!buf) return -1; - - for (y = 0; y < document->height; y++) { - int white = 0; - int x; - write_color_256("38", foreground, fd, buf, &bptr); - write_color_256("48", background, fd, buf, &bptr); - - for (x = 0; x < document->data[y].length; x++) { - unsigned char c; - unsigned char attr = document->data[y].chars[x].attr; - unsigned char color1 = document->data[y].chars[x].color[0]; - unsigned char color2 = document->data[y].chars[x].color[1]; - - if (foreground != color1) { - foreground = color1; - if (write_color_256("38", foreground, fd, buf, &bptr)) - goto fail; - } - - if (background != color2) { - background = color2; - if (write_color_256("48", background, fd, buf, &bptr)) - goto fail; - } - - c = document->data[y].chars[x].data; - - if ((attr & SCREEN_ATTR_FRAME) - && c >= 176 && c < 224) - c = frame_dumb[c - 176]; - - if (c <= ' ') { - /* Count spaces. */ - white++; - continue; - } - - /* Print spaces if any. */ - while (white) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - white--; - } - - /* Print normal char. */ - if (write_char(c, fd, buf, &bptr)) - goto fail; - } - for (;x < width; x++) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - } - - /* Print end of line. */ - if (write_char('\n', fd, buf, &bptr)) - goto fail; - } - - if (hard_write(fd, buf, bptr) != bptr) { -fail: - mem_free(buf); - return -1; - } - - if (document->nlinks && get_opt_bool("document.dump.references")) { - int x; - unsigned char *header = "\nReferences\n\n Visible links\n"; - int headlen = strlen(header); - - if (hard_write(fd, header, headlen) != headlen) - goto fail; - - for (x = 0; x < document->nlinks; x++) { - struct link *link = &document->links[x]; - unsigned char *where = link->where; - - if (!where) continue; - - if (document->options.links_numbering) { - if (link->title && *link->title) - snprintf(buf, D_BUF, "%4d. %s\n\t%s\n", - x + 1, link->title, where); - else - snprintf(buf, D_BUF, "%4d. %s\n", - x + 1, where); - } else { - if (link->title && *link->title) - snprintf(buf, D_BUF, " . %s\n\t%s\n", - link->title, where); - else - snprintf(buf, D_BUF, " . %s\n", where); - } - - bptr = strlen(buf); - if (hard_write(fd, buf, bptr) != bptr) - goto fail; - } - } - - mem_free(buf); - return 0; -} +#define DUMP_COLOR_MODE_256 +#include "dump-color-mode.h" +#undef DUMP_COLOR_MODE_256 #endif /* defined(CONFIG_88_COLORS) || defined(CONFIG_256_COLORS) */ @@ -726,269 +514,12 @@ write_true_color(unsigned char *str, unsigned char *color, int fd, unsigned char return 0; } -static int -dump_to_file_true_color(struct document *document, int fd) -{ - static unsigned char color[6] = {255, 255, 255, 0, 0, 0}; - int y; - int bptr = 0; - unsigned char *buf = mem_alloc(D_BUF); - unsigned char *foreground = &color[0]; - unsigned char *background = &color[3]; - int width = get_opt_int("document.dump.width"); - - if (!buf) return -1; - - for (y = 0; y < document->height; y++) { - int white = 0; - int x; - write_true_color("38", foreground, fd, buf, &bptr); - write_true_color("48", background, fd, buf, &bptr); - - for (x = 0; x < document->data[y].length; x++) { - unsigned char c; - unsigned char attr = document->data[y].chars[x].attr; - unsigned char *new_foreground = &document->data[y].chars[x].color[0]; - unsigned char *new_background = &document->data[y].chars[x].color[3]; - - if (memcmp(foreground, new_foreground, 3)) { - foreground = new_foreground; - if (write_true_color("38", foreground, fd, buf, &bptr)) - goto fail; - } - - if (memcmp(background, new_background, 3)) { - background = new_background; - if (write_true_color("48", background, fd, buf, &bptr)) - goto fail; - } - - c = document->data[y].chars[x].data; - - if ((attr & SCREEN_ATTR_FRAME) - && c >= 176 && c < 224) - c = frame_dumb[c - 176]; - - if (c <= ' ') { - /* Count spaces. */ - white++; - continue; - } - - /* Print spaces if any. */ - while (white) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - white--; - } - - /* Print normal char. */ - if (write_char(c, fd, buf, &bptr)) - goto fail; - } - for (;x < width; x++) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - } - - /* Print end of line. */ - if (write_char('\n', fd, buf, &bptr)) - goto fail; - } - - if (hard_write(fd, buf, bptr) != bptr) { -fail: - mem_free(buf); - return -1; - } - - if (document->nlinks && get_opt_bool("document.dump.references")) { - int x; - unsigned char *header = "\nReferences\n\n Visible links\n"; - int headlen = strlen(header); - - if (hard_write(fd, header, headlen) != headlen) - goto fail; - - for (x = 0; x < document->nlinks; x++) { - struct link *link = &document->links[x]; - unsigned char *where = link->where; - - if (!where) continue; - - if (document->options.links_numbering) { - if (link->title && *link->title) - snprintf(buf, D_BUF, "%4d. %s\n\t%s\n", - x + 1, link->title, where); - else - snprintf(buf, D_BUF, "%4d. %s\n", - x + 1, where); - } else { - if (link->title && *link->title) - snprintf(buf, D_BUF, " . %s\n\t%s\n", - link->title, where); - else - snprintf(buf, D_BUF, " . %s\n", where); - } - - bptr = strlen(buf); - if (hard_write(fd, buf, bptr) != bptr) - goto fail; - } - } - - mem_free(buf); - return 0; -} +#define DUMP_COLOR_MODE_TRUE +#include "dump-color-mode.h" +#undef DUMP_COLOR_MODE_TRUE #endif /* CONFIG_TRUE_COLOR */ -int -dump_to_file(struct document *document, int fd) -{ - int y; - int bptr = 0; - unsigned char *buf = mem_alloc(D_BUF); - if (!buf) return -1; - -#ifdef CONFIG_UTF8 - if (is_cp_utf8(document->options.cp)) - goto utf8; -#endif /* CONFIG_UTF8 */ - - for (y = 0; y < document->height; y++) { - int white = 0; - int x; - - for (x = 0; x < document->data[y].length; x++) { - unsigned char c; - unsigned char attr = document->data[y].chars[x].attr; - - c = document->data[y].chars[x].data; - - if ((attr & SCREEN_ATTR_FRAME) - && c >= 176 && c < 224) - c = frame_dumb[c - 176]; - - if (c <= ' ') { - /* Count spaces. */ - white++; - continue; - } - - /* Print spaces if any. */ - while (white) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - white--; - } - - /* Print normal char. */ - if (write_char(c, fd, buf, &bptr)) - goto fail; - } - - /* Print end of line. */ - if (write_char('\n', fd, buf, &bptr)) - goto fail; - } -#ifdef CONFIG_UTF8 - goto ref; -utf8: - for (y = 0; y < document->height; y++) { - int white = 0; - int x; - - for (x = 0; x < document->data[y].length; x++) { - unicode_val_T c; - unsigned char attr = document->data[y].chars[x].attr; - - c = document->data[y].chars[x].data; - - if ((attr & SCREEN_ATTR_FRAME) - && c >= 176 && c < 224) - c = frame_dumb[c - 176]; - else { - unsigned char *utf8_buf = encode_utf8(c); - - while (*utf8_buf) { - if (write_char(*utf8_buf++, - fd, buf, &bptr)) goto fail; - } - - x += unicode_to_cell(c) - 1; - - continue; - } - - if (c <= ' ') { - /* Count spaces. */ - white++; - continue; - } - - /* Print spaces if any. */ - while (white) { - if (write_char(' ', fd, buf, &bptr)) - goto fail; - white--; - } - - /* Print normal char. */ - if (write_char(c, fd, buf, &bptr)) - goto fail; - } - - /* Print end of line. */ - if (write_char('\n', fd, buf, &bptr)) - goto fail; - } -ref: -#endif /* CONFIG_UTF8 */ - - if (hard_write(fd, buf, bptr) != bptr) { -fail: - mem_free(buf); - return -1; - } - - if (document->nlinks && get_opt_bool("document.dump.references")) { - int x; - unsigned char *header = "\nReferences\n\n Visible links\n"; - int headlen = strlen(header); - - if (hard_write(fd, header, headlen) != headlen) - goto fail; - - for (x = 0; x < document->nlinks; x++) { - struct link *link = &document->links[x]; - unsigned char *where = link->where; - - if (!where) continue; - - if (document->options.links_numbering) { - if (link->title && *link->title) - snprintf(buf, D_BUF, "%4d. %s\n\t%s\n", - x + 1, link->title, where); - else - snprintf(buf, D_BUF, "%4d. %s\n", - x + 1, where); - } else { - if (link->title && *link->title) - snprintf(buf, D_BUF, " . %s\n\t%s\n", - link->title, where); - else - snprintf(buf, D_BUF, " . %s\n", where); - } - - bptr = strlen(buf); - if (hard_write(fd, buf, bptr) != bptr) - goto fail; - } - } - - mem_free(buf); - return 0; -} +#include "dump-color-mode.h" #undef D_BUF