1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

dump: Move the buffer into new struct dump_output

This commit is contained in:
Kalle Olavi Niemitalo 2009-06-19 14:34:51 +03:00 committed by Kalle Olavi Niemitalo
parent 2f0cefffb5
commit 04dabd5bf1
3 changed files with 103 additions and 55 deletions

View File

@ -34,13 +34,12 @@
#endif /* CONFIG_UTF8 */ #endif /* CONFIG_UTF8 */
static int static int
DUMP_FUNCTION_COLOR(struct document *document, int fd, DUMP_FUNCTION_COLOR(struct document *document, struct dump_output *out)
unsigned char buf[D_BUF])
{ {
#ifdef CONFIG_UTF8 #ifdef CONFIG_UTF8
if (is_cp_utf8(document->options.cp)) if (is_cp_utf8(document->options.cp))
return DUMP_FUNCTION_UTF8(document, fd, buf); return DUMP_FUNCTION_UTF8(document, out);
#endif /* CONFIG_UTF8 */ #endif /* CONFIG_UTF8 */
return DUMP_FUNCTION_UNIBYTE(document, fd, buf); return DUMP_FUNCTION_UNIBYTE(document, out);
} }

View File

@ -23,11 +23,9 @@
*/ */
static int static int
DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd, DUMP_FUNCTION_SPECIALIZED(struct document *document, struct dump_output *out)
unsigned char buf[D_BUF])
{ {
int y; int y;
int bptr = 0;
#ifdef DUMP_COLOR_MODE_16 #ifdef DUMP_COLOR_MODE_16
unsigned char color = 0; unsigned char color = 0;
const int width = get_opt_int("document.dump.width"); const int width = get_opt_int("document.dump.width");
@ -47,13 +45,13 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
int x; int x;
#ifdef DUMP_COLOR_MODE_16 #ifdef DUMP_COLOR_MODE_16
write_color_16(color, fd, buf, &bptr); write_color_16(color, out);
#elif defined(DUMP_COLOR_MODE_256) #elif defined(DUMP_COLOR_MODE_256)
write_color_256("38", foreground, fd, buf, &bptr); write_color_256("38", foreground, out);
write_color_256("48", background, fd, buf, &bptr); write_color_256("48", background, out);
#elif defined(DUMP_COLOR_MODE_TRUE) #elif defined(DUMP_COLOR_MODE_TRUE)
write_true_color("38", foreground, fd, buf, &bptr); write_true_color("38", foreground, out);
write_true_color("48", background, fd, buf, &bptr); write_true_color("48", background, out);
#endif /* DUMP_COLOR_MODE_TRUE */ #endif /* DUMP_COLOR_MODE_TRUE */
for (x = 0; x < document->data[y].length; x++) { for (x = 0; x < document->data[y].length; x++) {
@ -93,33 +91,33 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
#ifdef DUMP_COLOR_MODE_16 #ifdef DUMP_COLOR_MODE_16
if (color != color1) { if (color != color1) {
color = color1; color = color1;
if (write_color_16(color, fd, buf, &bptr)) if (write_color_16(color, out))
return -1; return -1;
} }
#elif defined(DUMP_COLOR_MODE_256) #elif defined(DUMP_COLOR_MODE_256)
if (foreground != color1) { if (foreground != color1) {
foreground = color1; foreground = color1;
if (write_color_256("38", foreground, fd, buf, &bptr)) if (write_color_256("38", foreground, out))
return -1; return -1;
} }
if (background != color2) { if (background != color2) {
background = color2; background = color2;
if (write_color_256("48", background, fd, buf, &bptr)) if (write_color_256("48", background, out))
return -1; return -1;
} }
#elif defined(DUMP_COLOR_MODE_TRUE) #elif defined(DUMP_COLOR_MODE_TRUE)
if (memcmp(foreground, new_foreground, 3)) { if (memcmp(foreground, new_foreground, 3)) {
foreground = new_foreground; foreground = new_foreground;
if (write_true_color("38", foreground, fd, buf, &bptr)) if (write_true_color("38", foreground, out))
return -1; return -1;
} }
if (memcmp(background, new_background, 3)) { if (memcmp(background, new_background, 3)) {
background = new_background; background = new_background;
if (write_true_color("48", background, fd, buf, &bptr)) if (write_true_color("48", background, out))
return -1; return -1;
} }
#endif /* DUMP_COLOR_MODE_TRUE */ #endif /* DUMP_COLOR_MODE_TRUE */
@ -142,7 +140,7 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
/* Print spaces if any. */ /* Print spaces if any. */
while (white) { while (white) {
if (write_char(' ', fd, buf, &bptr)) if (write_char(' ', out))
return -1; return -1;
white--; white--;
} }
@ -151,29 +149,28 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
#ifdef DUMP_CHARSET_UTF8 #ifdef DUMP_CHARSET_UTF8
utf8_buf = encode_utf8(c); utf8_buf = encode_utf8(c);
while (*utf8_buf) { while (*utf8_buf) {
if (write_char(*utf8_buf++, if (write_char(*utf8_buf++, out)) return -1;
fd, buf, &bptr)) return -1;
} }
#else /* !DUMP_CHARSET_UTF8 */ #else /* !DUMP_CHARSET_UTF8 */
if (write_char(c, fd, buf, &bptr)) if (write_char(c, out))
return -1; return -1;
#endif /* !DUMP_CHARSET_UTF8 */ #endif /* !DUMP_CHARSET_UTF8 */
} }
#if defined(DUMP_COLOR_MODE_16) || defined(DUMP_COLOR_MODE_256) || defined(DUMP_COLOR_MODE_TRUE) #if defined(DUMP_COLOR_MODE_16) || defined(DUMP_COLOR_MODE_256) || defined(DUMP_COLOR_MODE_TRUE)
for (;x < width; x++) { for (;x < width; x++) {
if (write_char(' ', fd, buf, &bptr)) if (write_char(' ', out))
return -1; return -1;
} }
#endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */ #endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */
/* Print end of line. */ /* Print end of line. */
if (write_char('\n', fd, buf, &bptr)) if (write_char('\n', out))
return -1; return -1;
} }
if (hard_write(fd, buf, bptr) != bptr) if (dump_output_flush(out))
return -1; return -1;
return 0; return 0;

View File

@ -53,21 +53,73 @@ static int dump_redir_count = 0;
#define D_BUF 65536 #define D_BUF 65536
static int /** A place where dumping functions write their output. The data
write_char(unsigned char c, int fd, unsigned char *buf, int *bptr) * first goes to the buffer in this structure. When the buffer is
{ * full enough, it is flushed to a file descriptor. */
buf[(*bptr)++] = c; struct dump_output {
if ((*bptr) >= D_BUF) { /** How many bytes are in #buf already. */
if (hard_write(fd, buf, (*bptr)) != (*bptr)) size_t bufpos;
return -1;
(*bptr) = 0;
}
/** A file descriptor to which the buffer should eventually be
* flushed. */
int fd;
/** Bytes waiting to be flushed. */
unsigned char buf[D_BUF];
};
/** Allocate and initialize a struct dump_output.
* The caller should eventually free the structure with mem_free().
*
* @param fd
* The file descriptor to which the output will be written.
*
* @return The new structure, or NULL on error.
*
* @relates dump_output */
static struct dump_output *
dump_output_alloc(int fd)
{
struct dump_output *out = mem_alloc(sizeof(*out));
if (out) {
out->fd = fd;
out->bufpos = 0;
}
return out;
}
/** Flush buffered output to the file.
*
* @return 0 on success, or -1 on error.
*
* @post If this succeeds, then out->bufpos == 0, so that the buffer
* has room for more data.
*
* @relates dump_output */
static int
dump_output_flush(struct dump_output *out)
{
if (hard_write(out->fd, out->buf, out->bufpos) != out->bufpos)
return -1;
out->bufpos = 0;
return 0; return 0;
} }
static int static int
write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr) write_char(unsigned char c, struct dump_output *out)
{
if (out->bufpos >= D_BUF) {
if (dump_output_flush(out))
return -1;
}
out->buf[out->bufpos++] = c;
return 0;
}
static int
write_color_16(unsigned char color, struct dump_output *out)
{ {
unsigned char bufor[] = "\033[0;30;40m"; unsigned char bufor[] = "\033[0;30;40m";
unsigned char *data = bufor; unsigned char *data = bufor;
@ -81,7 +133,7 @@ write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr)
bufor[7] = '\0'; bufor[7] = '\0';
} }
while(*data) { while(*data) {
if (write_char(*data++, fd, buf, bptr)) return -1; if (write_char(*data++, out)) return -1;
} }
return 0; return 0;
} }
@ -103,14 +155,14 @@ write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr)
static int static int
write_color_256(const unsigned char *str, unsigned char color, write_color_256(const unsigned char *str, unsigned char color,
int fd, unsigned char *buf, int *bptr) struct dump_output *out)
{ {
unsigned char bufor[16]; unsigned char bufor[16];
unsigned char *data = bufor; unsigned char *data = bufor;
snprintf(bufor, 16, "\033[%s;5;%dm", str, color); snprintf(bufor, 16, "\033[%s;5;%dm", str, color);
while(*data) { while(*data) {
if (write_char(*data++, fd, buf, bptr)) return -1; if (write_char(*data++, out)) return -1;
} }
return 0; return 0;
} }
@ -131,14 +183,14 @@ write_color_256(const unsigned char *str, unsigned char color,
static int static int
write_true_color(const unsigned char *str, const unsigned char *color, write_true_color(const unsigned char *str, const unsigned char *color,
int fd, unsigned char *buf, int *bptr) struct dump_output *out)
{ {
unsigned char bufor[24]; unsigned char bufor[24];
unsigned char *data = bufor; unsigned char *data = bufor;
snprintf(bufor, 24, "\033[%s;2;%d;%d;%dm", str, color[0], color[1], color[2]); snprintf(bufor, 24, "\033[%s;2;%d;%d;%dm", str, color[0], color[1], color[2]);
while(*data) { while(*data) {
if (write_char(*data++, fd, buf, bptr)) return -1; if (write_char(*data++, out)) return -1;
} }
return 0; return 0;
} }
@ -209,16 +261,16 @@ dump_references(struct document *document, int fd, unsigned char buf[D_BUF])
int int
dump_to_file(struct document *document, int fd) dump_to_file(struct document *document, int fd)
{ {
unsigned char *buf = mem_alloc(D_BUF); struct dump_output *out = dump_output_alloc(fd);
int error; int error;
if (!buf) return -1; if (!out) return -1;
error = dump_nocolor(document, fd, buf); error = dump_nocolor(document, out);
if (!error) if (!error)
error = dump_references(document, fd, buf); error = dump_references(document, fd, out->buf);
mem_free(buf); mem_free(out);
return error; return error;
} }
@ -230,7 +282,7 @@ dump_formatted(int fd, struct download *download, struct cache_entry *cached)
struct document_view formatted; struct document_view formatted;
struct view_state vs; struct view_state vs;
int width; int width;
unsigned char *buf; struct dump_output *out;
if (!cached) return; if (!cached) return;
@ -250,47 +302,47 @@ dump_formatted(int fd, struct download *download, struct cache_entry *cached)
render_document(&vs, &formatted, &o); render_document(&vs, &formatted, &o);
buf = mem_alloc(D_BUF); out = dump_output_alloc(fd);
if (buf) { if (out) {
int error; int error;
switch (o.color_mode) { switch (o.color_mode) {
case COLOR_MODE_DUMP: case COLOR_MODE_DUMP:
case COLOR_MODE_MONO: /* FIXME: inversion */ case COLOR_MODE_MONO: /* FIXME: inversion */
error = dump_nocolor(formatted.document, fd, buf); error = dump_nocolor(formatted.document, out);
break; break;
default: default:
/* If the desired color mode was not compiled in, /* If the desired color mode was not compiled in,
* use 16 colors. */ * use 16 colors. */
case COLOR_MODE_16: case COLOR_MODE_16:
error = dump_16color(formatted.document, fd, buf); error = dump_16color(formatted.document, out);
break; break;
#ifdef CONFIG_88_COLORS #ifdef CONFIG_88_COLORS
case COLOR_MODE_88: case COLOR_MODE_88:
error = dump_256color(formatted.document, fd, buf); error = dump_256color(formatted.document, out);
break; break;
#endif #endif
#ifdef CONFIG_256_COLORS #ifdef CONFIG_256_COLORS
case COLOR_MODE_256: case COLOR_MODE_256:
error = dump_256color(formatted.document, fd, buf); error = dump_256color(formatted.document, out);
break; break;
#endif #endif
#ifdef CONFIG_TRUE_COLOR #ifdef CONFIG_TRUE_COLOR
case COLOR_MODE_TRUE_COLOR: case COLOR_MODE_TRUE_COLOR:
error = dump_truecolor(formatted.document, fd, buf); error = dump_truecolor(formatted.document, out);
break; break;
#endif #endif
} }
if (!error) if (!error)
dump_references(formatted.document, fd, buf); dump_references(formatted.document, fd, out->buf);
mem_free(buf); mem_free(out);
} /* if buf */ } /* if out */
detach_formatted(&formatted); detach_formatted(&formatted);
destroy_vs(&vs, 1); destroy_vs(&vs, 1);