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:
parent
2f0cefffb5
commit
04dabd5bf1
src/viewer/dump
@ -34,13 +34,12 @@
|
||||
#endif /* CONFIG_UTF8 */
|
||||
|
||||
static int
|
||||
DUMP_FUNCTION_COLOR(struct document *document, int fd,
|
||||
unsigned char buf[D_BUF])
|
||||
DUMP_FUNCTION_COLOR(struct document *document, struct dump_output *out)
|
||||
{
|
||||
#ifdef CONFIG_UTF8
|
||||
if (is_cp_utf8(document->options.cp))
|
||||
return DUMP_FUNCTION_UTF8(document, fd, buf);
|
||||
return DUMP_FUNCTION_UTF8(document, out);
|
||||
#endif /* CONFIG_UTF8 */
|
||||
|
||||
return DUMP_FUNCTION_UNIBYTE(document, fd, buf);
|
||||
return DUMP_FUNCTION_UNIBYTE(document, out);
|
||||
}
|
||||
|
@ -23,11 +23,9 @@
|
||||
*/
|
||||
|
||||
static int
|
||||
DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
|
||||
unsigned char buf[D_BUF])
|
||||
DUMP_FUNCTION_SPECIALIZED(struct document *document, struct dump_output *out)
|
||||
{
|
||||
int y;
|
||||
int bptr = 0;
|
||||
#ifdef DUMP_COLOR_MODE_16
|
||||
unsigned char color = 0;
|
||||
const int width = get_opt_int("document.dump.width");
|
||||
@ -47,13 +45,13 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
|
||||
int x;
|
||||
|
||||
#ifdef DUMP_COLOR_MODE_16
|
||||
write_color_16(color, fd, buf, &bptr);
|
||||
write_color_16(color, out);
|
||||
#elif defined(DUMP_COLOR_MODE_256)
|
||||
write_color_256("38", foreground, fd, buf, &bptr);
|
||||
write_color_256("48", background, fd, buf, &bptr);
|
||||
write_color_256("38", foreground, out);
|
||||
write_color_256("48", background, out);
|
||||
#elif defined(DUMP_COLOR_MODE_TRUE)
|
||||
write_true_color("38", foreground, fd, buf, &bptr);
|
||||
write_true_color("48", background, fd, buf, &bptr);
|
||||
write_true_color("38", foreground, out);
|
||||
write_true_color("48", background, out);
|
||||
#endif /* DUMP_COLOR_MODE_TRUE */
|
||||
|
||||
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
|
||||
if (color != color1) {
|
||||
color = color1;
|
||||
if (write_color_16(color, fd, buf, &bptr))
|
||||
if (write_color_16(color, out))
|
||||
return -1;
|
||||
}
|
||||
|
||||
#elif defined(DUMP_COLOR_MODE_256)
|
||||
if (foreground != color1) {
|
||||
foreground = color1;
|
||||
if (write_color_256("38", foreground, fd, buf, &bptr))
|
||||
if (write_color_256("38", foreground, out))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (background != color2) {
|
||||
background = color2;
|
||||
if (write_color_256("48", background, fd, buf, &bptr))
|
||||
if (write_color_256("48", background, out))
|
||||
return -1;
|
||||
}
|
||||
|
||||
#elif defined(DUMP_COLOR_MODE_TRUE)
|
||||
if (memcmp(foreground, new_foreground, 3)) {
|
||||
foreground = new_foreground;
|
||||
if (write_true_color("38", foreground, fd, buf, &bptr))
|
||||
if (write_true_color("38", foreground, out))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (memcmp(background, new_background, 3)) {
|
||||
background = new_background;
|
||||
if (write_true_color("48", background, fd, buf, &bptr))
|
||||
if (write_true_color("48", background, out))
|
||||
return -1;
|
||||
}
|
||||
#endif /* DUMP_COLOR_MODE_TRUE */
|
||||
@ -142,7 +140,7 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
|
||||
|
||||
/* Print spaces if any. */
|
||||
while (white) {
|
||||
if (write_char(' ', fd, buf, &bptr))
|
||||
if (write_char(' ', out))
|
||||
return -1;
|
||||
white--;
|
||||
}
|
||||
@ -151,29 +149,28 @@ DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
|
||||
#ifdef DUMP_CHARSET_UTF8
|
||||
utf8_buf = encode_utf8(c);
|
||||
while (*utf8_buf) {
|
||||
if (write_char(*utf8_buf++,
|
||||
fd, buf, &bptr)) return -1;
|
||||
if (write_char(*utf8_buf++, out)) return -1;
|
||||
}
|
||||
|
||||
#else /* !DUMP_CHARSET_UTF8 */
|
||||
if (write_char(c, fd, buf, &bptr))
|
||||
if (write_char(c, out))
|
||||
return -1;
|
||||
#endif /* !DUMP_CHARSET_UTF8 */
|
||||
}
|
||||
|
||||
#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))
|
||||
if (write_char(' ', out))
|
||||
return -1;
|
||||
}
|
||||
#endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */
|
||||
|
||||
/* Print end of line. */
|
||||
if (write_char('\n', fd, buf, &bptr))
|
||||
if (write_char('\n', out))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (hard_write(fd, buf, bptr) != bptr)
|
||||
if (dump_output_flush(out))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
@ -53,21 +53,73 @@ static int dump_redir_count = 0;
|
||||
|
||||
#define D_BUF 65536
|
||||
|
||||
static int
|
||||
write_char(unsigned char c, int fd, unsigned char *buf, int *bptr)
|
||||
{
|
||||
buf[(*bptr)++] = c;
|
||||
if ((*bptr) >= D_BUF) {
|
||||
if (hard_write(fd, buf, (*bptr)) != (*bptr))
|
||||
return -1;
|
||||
(*bptr) = 0;
|
||||
}
|
||||
/** A place where dumping functions write their output. The data
|
||||
* first goes to the buffer in this structure. When the buffer is
|
||||
* full enough, it is flushed to a file descriptor. */
|
||||
struct dump_output {
|
||||
/** How many bytes are in #buf already. */
|
||||
size_t bufpos;
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
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 *data = bufor;
|
||||
@ -81,7 +133,7 @@ write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr)
|
||||
bufor[7] = '\0';
|
||||
}
|
||||
while(*data) {
|
||||
if (write_char(*data++, fd, buf, bptr)) return -1;
|
||||
if (write_char(*data++, out)) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -103,14 +155,14 @@ write_color_16(unsigned char color, int fd, unsigned char *buf, int *bptr)
|
||||
|
||||
static int
|
||||
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 *data = bufor;
|
||||
|
||||
snprintf(bufor, 16, "\033[%s;5;%dm", str, color);
|
||||
while(*data) {
|
||||
if (write_char(*data++, fd, buf, bptr)) return -1;
|
||||
if (write_char(*data++, out)) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -131,14 +183,14 @@ write_color_256(const unsigned char *str, unsigned char color,
|
||||
|
||||
static int
|
||||
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 *data = bufor;
|
||||
|
||||
snprintf(bufor, 24, "\033[%s;2;%d;%d;%dm", str, color[0], color[1], color[2]);
|
||||
while(*data) {
|
||||
if (write_char(*data++, fd, buf, bptr)) return -1;
|
||||
if (write_char(*data++, out)) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -209,16 +261,16 @@ dump_references(struct document *document, int fd, unsigned char buf[D_BUF])
|
||||
int
|
||||
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;
|
||||
|
||||
if (!buf) return -1;
|
||||
if (!out) return -1;
|
||||
|
||||
error = dump_nocolor(document, fd, buf);
|
||||
error = dump_nocolor(document, out);
|
||||
if (!error)
|
||||
error = dump_references(document, fd, buf);
|
||||
error = dump_references(document, fd, out->buf);
|
||||
|
||||
mem_free(buf);
|
||||
mem_free(out);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -230,7 +282,7 @@ dump_formatted(int fd, struct download *download, struct cache_entry *cached)
|
||||
struct document_view formatted;
|
||||
struct view_state vs;
|
||||
int width;
|
||||
unsigned char *buf;
|
||||
struct dump_output *out;
|
||||
|
||||
if (!cached) return;
|
||||
|
||||
@ -250,47 +302,47 @@ dump_formatted(int fd, struct download *download, struct cache_entry *cached)
|
||||
|
||||
render_document(&vs, &formatted, &o);
|
||||
|
||||
buf = mem_alloc(D_BUF);
|
||||
if (buf) {
|
||||
out = dump_output_alloc(fd);
|
||||
if (out) {
|
||||
int error;
|
||||
|
||||
switch (o.color_mode) {
|
||||
case COLOR_MODE_DUMP:
|
||||
case COLOR_MODE_MONO: /* FIXME: inversion */
|
||||
error = dump_nocolor(formatted.document, fd, buf);
|
||||
error = dump_nocolor(formatted.document, out);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* If the desired color mode was not compiled in,
|
||||
* use 16 colors. */
|
||||
case COLOR_MODE_16:
|
||||
error = dump_16color(formatted.document, fd, buf);
|
||||
error = dump_16color(formatted.document, out);
|
||||
break;
|
||||
|
||||
#ifdef CONFIG_88_COLORS
|
||||
case COLOR_MODE_88:
|
||||
error = dump_256color(formatted.document, fd, buf);
|
||||
error = dump_256color(formatted.document, out);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_256_COLORS
|
||||
case COLOR_MODE_256:
|
||||
error = dump_256color(formatted.document, fd, buf);
|
||||
error = dump_256color(formatted.document, out);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_TRUE_COLOR
|
||||
case COLOR_MODE_TRUE_COLOR:
|
||||
error = dump_truecolor(formatted.document, fd, buf);
|
||||
error = dump_truecolor(formatted.document, out);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!error)
|
||||
dump_references(formatted.document, fd, buf);
|
||||
dump_references(formatted.document, fd, out->buf);
|
||||
|
||||
mem_free(buf);
|
||||
} /* if buf */
|
||||
mem_free(out);
|
||||
} /* if out */
|
||||
|
||||
detach_formatted(&formatted);
|
||||
destroy_vs(&vs, 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user