-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlTable.c
66 lines (58 loc) · 1.87 KB
/
htmlTable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "htmlTable.h"
#include <stdarg.h>
#include <stdlib.h>
#define TABLE_HEAD "thead"
#define TABLE_BODY "tbody"
#define TABLE_HEADING "th"
#define TABLE_ROW "tr"
#define TABLE_DATA "td"
struct table {
FILE * file;
unsigned int columns;
};
static void addNodes(const char * fatherNode, const char * childNode, htmlTable table, va_list arg);
htmlTable newTable (const char * fileName, unsigned int columns, ...) {
errno = 0;
FILE * file = fopen(fileName, "w");
if ( file == NULL ) {
return NULL;
}
htmlTable table = malloc(sizeof(struct table));
if ( errno == ENOMEM ) {
fclose(file);
return NULL;
}
table->file = file;
table->columns = columns;
va_list args;
va_start(args, columns);
fputs("<link href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css\" rel=\"stylesheet\" "
"integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" "
"crossorigin=\"anonymous\">", file);
fputs("<html><table class=\"table table-striped table-hover\">", file);
fprintf(file, "<%s>", TABLE_HEAD);
addNodes(TABLE_ROW, TABLE_HEADING, table, args);
fprintf(file, "</%s><%s>", TABLE_HEAD, TABLE_BODY);
return table;
}
void
addHTMLRow(const htmlTable table, ...) {
va_list args;
va_start(args, table);
addNodes(TABLE_ROW, TABLE_DATA, table, args);
}
void
closeHTMLTable(htmlTable table) {
fprintf(table->file, "</%s></table></html>", TABLE_BODY);
fclose(table->file);
free(table);
}
static void
addNodes(const char * fatherNode, const char * childNode, htmlTable table, va_list arg) {
fprintf(table->file, "<%s>", fatherNode);
for (int i = 0; i < table->columns; i++) {
fprintf(table->file, "<%s>%s</%s>", childNode, va_arg(arg, char *), childNode);
}
va_end(arg);
fprintf(table->file, "</%s>", fatherNode);
}