tvote.c: improve display of poll files - vote - simple cgi voting system for web and gopher (HTM) git clone git://src.adamsgaard.dk/vote (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 10c2ffb581321cb59e676d3f577ce0946debe5c6 (DIR) parent 9afaec1473d2044388e45ede5f1a4ed38d198939 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Sun, 27 Sep 2020 10:05:36 +0200 vote.c: improve display of poll files Diffstat: M vote.c | 50 +++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) --- (DIR) diff --git a/vote.c b/vote.c t@@ -50,6 +50,53 @@ print_html_foot() } void +print_poll_line(char *line) +{ + printf("<tr><td>"); + while (*line) { + switch(*line) { + case '\t': + printf("</td><td>"); + break; + default: + putchar(*line); + break; + } + (void)*line++; + } + puts("</td></tr>"); +} + +void +print_poll_file(FILE *fp) +{ + char *line = NULL; + size_t linesize = 0; + ssize_t linelen; + unsigned int lineno = 0; + + while ((linelen = getline(&line, &linesize, fp)) != -1) { + lineno++; + if (line[linelen - 1] == '\n') + line[--linelen] = '\0'; + + if (lineno == 1) { + printf("<h1>"); + fwrite(line, linelen, 1, stdout); + printf("</h1>"); + printf("<table>\n"); + } else { + print_poll_line(line); + } + /* puts("<br>"); */ + } + free(line); + if (ferror(fp)) + err(1, "print_poll_file: getline"); + puts("</table>"); +} + +void show_poll(const char *poll_name) { FILE *fd; t@@ -67,10 +114,9 @@ show_poll(const char *poll_name) http_status(404); exit(1); } else { + print_poll_file(fd); fclose(fd); } - - printf("<h2>poll: '%s'</h2>\n", poll); } void