1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

bug 1080: Move common code to dump_references()

This code was included in four variants of dump_to_file().
Move it to a new function dump_references() and make dump_to_file()
then call that.  This makes the code size a little smaller.
The time cost will be negligible.
This commit is contained in:
Kalle Olavi Niemitalo 2009-06-08 23:53:01 +03:00 committed by Kalle Olavi Niemitalo
parent 200e36c002
commit 35a091e8f0
2 changed files with 45 additions and 34 deletions

View File

@ -199,40 +199,8 @@ fail:
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;
}
}
if (dump_references(document, fd, buf))
goto fail;
mem_free(buf);
return 0;

View File

@ -438,6 +438,49 @@ end:
#define D_BUF 65536
/*! @return 0 on success, -1 on error */
static int
dump_references(struct document *document, int fd, unsigned char buf[D_BUF])
{
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)
return -1;
for (x = 0; x < document->nlinks; x++) {
struct link *link = &document->links[x];
unsigned char *where = link->where;
size_t reflen;
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);
}
reflen = strlen(buf);
if (hard_write(fd, buf, reflen) != reflen)
return -1;
}
}
return 0;
}
static int
write_char(unsigned char c, int fd, unsigned char *buf, int *bptr)
{