[PATCH] lib: fwts_log_html: convert misc ASCII chars to HTML mnemonics

Colin King colin.king at canonical.com
Mon Jun 11 10:01:50 UTC 2012


From: Colin Ian King <colin.king at canonical.com>

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/src/fwts_log_html.c |  207 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 198 insertions(+), 9 deletions(-)

diff --git a/src/lib/src/fwts_log_html.c b/src/lib/src/fwts_log_html.c
index 1ca79b8..a040b43 100644
--- a/src/lib/src/fwts_log_html.c
+++ b/src/lib/src/fwts_log_html.c
@@ -34,6 +34,133 @@ typedef struct {
 	const char *name;
 } fwts_log_html_stack_t;
 
+typedef struct {
+	unsigned char	ch;	/* ASCII */
+	char *		html;	/* HTML equivalent */
+} fwts_log_html_ascii_t;
+
+/*
+ * ASCII to HTML conversion table:
+ * ISO 10646, ISO 8879, ISO 8859-1 Latin alphabet No. 1
+ * Browser support: All browsers
+ */
+static fwts_log_html_ascii_t fwts_log_html_ascii_table[] = {
+	{ '"',	""" },
+	{ '&', 	"&" },
+	{ '<', 	"<" },
+	{ '>',	">" },
+	{ '{',	"{" },
+	{ '|',	"|" },
+	{ '}',	"}" },
+	{ '~',	"~" },
+
+	{ 160,	" " },
+	{ 161,	"&iexl;" },
+	{ 162,	"¢" },
+	{ 163,	"£" },
+	{ 164,	"¤" },
+	{ 165,	"¥" },
+	{ 166,	"¦" },
+	{ 167,	"§" },
+	{ 168,	"¨" },
+	{ 169,	"©" },
+	{ 170,	"ª" },
+	{ 171,	"«" },
+	{ 172,	"¬" },
+	{ 173,	"­" },
+	{ 174,	"®" },
+	{ 175,	"¯" },
+
+	{ 176,	"°" },
+	{ 177,	"±" },
+	{ 178,	"&sup2;" },
+	{ 179,	"&sup3;" },
+	{ 180,	"´" },
+	{ 181,	"µ" },
+	{ 182,	"¶" },
+	{ 183,	"·" },
+	{ 184,	"¸" },
+	{ 185,	"&sup1;" },
+	{ 186,	"º" },
+	{ 187,	"»" },
+	{ 187,	"&frac14;" },
+	{ 189,	"&frac12;" },
+	{ 190,	"&frac34;" },
+	{ 191,	"¿" },
+
+	/* Probably never used, but here in case */
+
+	{ 192,	"À" },
+	{ 193,	"Á" },
+	{ 194,	"Â" },
+	{ 195,	"Ã" },
+	{ 196,	"Ä" },
+	{ 197,	"Å" },
+	{ 198,	"Æ" },
+	{ 199,	"Ç" },
+	{ 200,	"È" },
+	{ 201,	"É" },
+	{ 202,	"Ê" },
+	{ 203,	"Ë" },
+	{ 204,	"&lgrave;" },
+	{ 205,	"ĺ" },
+	{ 206,	"&lcirc;" },
+	{ 207,	"&luml;" },
+
+	{ 208,	"Ð" },
+	{ 209,	"Ñ" },
+	{ 210,	"Ò" },
+	{ 211,	"Ó" },
+	{ 212,	"Ô" },
+	{ 213,	"Õ" },
+	{ 214,	"Ö" },
+	{ 215,	"×" },
+	{ 216,	"Ø" },
+	{ 217,	"Ù" },
+	{ 218,	"Ú" },
+	{ 219,	"Û" },
+	{ 220,	"Ü" },
+	{ 221,	"Ý" },
+	{ 222,	"Þ" },
+	{ 223,	"ß" },
+
+	{ 224,	"à" },
+	{ 225,	"á" },
+	{ 226,	"â" },
+	{ 227,	"ã" },
+	{ 228,	"ä" },
+	{ 229,	"å" },
+	{ 230,	"æ" },
+	{ 231,	"ç" },
+	{ 232,	"è" },
+	{ 233,	"é" },
+	{ 234,	"ê" },
+	{ 235,	"&emuml;" },
+	{ 236,	"ì" },
+	{ 237,	"í" },
+	{ 238,	"î" },
+	{ 239,	"ï" },
+
+	{ 240,	"ð" },
+	{ 241,	"ñ" },
+	{ 242,	"ò" },
+	{ 243,	"ó" },
+	{ 244,	"ô" },
+	{ 245,	"õ" },
+	{ 246,	"ö" },
+	{ 247,	"÷" },
+	{ 248,	"ø" },
+	{ 249,	"ù" },
+	{ 250,	"ú" },
+	{ 251,	"û" },
+	{ 252,	"ü" },
+	{ 253,	"ý" },
+	{ 254,	"þ" },
+	{ 255,	"ÿ" },
+
+	{ 0, 	NULL },
+};
+
 static fwts_log_html_stack_t html_stack[MAX_HTML_STACK];
 static int html_stack_index = 0;
 
@@ -49,6 +176,58 @@ static void fwts_log_html(fwts_log_file *log_file, const char *fmt, ...)
 	va_end(args);
 }
 
+/*
+ *  fwts_log_html_convert_ascii()
+ *	return a static string containing any convertion from ASCII to a
+ *	HTML representation of char ch.  If no conversion needed, just
+ *	return a string containing the ch.
+ */
+static char *fwts_log_html_convert_ascii(const char ch)
+{
+	int i;
+	static char buf[2];
+
+	for (i = 0; fwts_log_html_ascii_table[i].html != NULL; i++) {
+		if (fwts_log_html_ascii_table[i].ch == ch)
+			return fwts_log_html_ascii_table[i].html;
+	}
+
+	/* No mapping, just return string containing ch */
+	buf[0] = ch;
+	buf[1] = '\0';
+
+	return buf;
+}
+
+/*
+ *  fwts_log_html_convert_ascii_str()
+ *	convert an ASCII string into a HTML encoded string. The returned
+ *	string needs free'ing once finished with.
+ */
+static char *fwts_log_html_convert_ascii_str(const char *buffer)
+{
+	const char *str1;
+	char *converted;
+	size_t len = 0;
+
+	/* Step 1, figure out how much space we need */
+	for (str1 = buffer; *str1; str1++) {
+		char *str = fwts_log_html_convert_ascii(*str1);
+		len += strlen(str);
+	}
+
+	if ((converted = calloc(len + 1, 1)) == NULL)
+		return NULL;
+
+	/* Step 2, convert */
+	for (str1 = buffer; *str1; str1++) {
+		char *str = fwts_log_html_convert_ascii(*str1);
+		strcat(converted, str);
+	}
+
+	return converted;
+}
+
 
 /*
  *  fwts_log_print_html()
@@ -67,6 +246,7 @@ static int fwts_log_print_html(
 	char *style;
 	char *code_start;
 	char *code_end;
+	char *html_converted;
 
 	if (!((field & LOG_FIELD_MASK) & fwts_log_filter))
 		return 0;
@@ -74,6 +254,12 @@ static int fwts_log_print_html(
 	if (field & (LOG_NEWLINE | LOG_SEPARATOR | LOG_DEBUG))
 		return 0;
 
+	if ((html_converted = fwts_log_html_convert_ascii_str(buffer)) == NULL) {
+		/* We can't report an error via the logging mechanism in case we loop */
+		fprintf(stderr, "Out of memory converting html.\n");
+		exit(EXIT_FAILURE);
+	}
+
 	fwts_log_html(log_file, "<TR>\n");
 
 	if (field & LOG_VERBATUM) {
@@ -87,23 +273,24 @@ static int fwts_log_print_html(
 	switch (field & LOG_FIELD_MASK) {
 	case LOG_ERROR:
 		fwts_log_html(log_file, "  <TD class=style_error>Error</TD>"
-			"<TD COLSPAN=2>%s</TD>\n", buffer);
+			"<TD COLSPAN=2>%s</TD>\n", html_converted);
 		break;
 	case LOG_WARNING:
 		fwts_log_html(log_file, "  <TD class=style_error>Warning</TD>"
 			"<TD COLSPAN=2 class=style_advice_info>%s%s%s</TD>\n",
-			code_start, buffer, code_end);
+			code_start, html_converted, code_end);
 		break;
 	case LOG_HEADING:
 		fwts_log_html(log_file, "<TD COLSPAN=2 class=style_heading>%s%s%s</TD>\n",
-			code_start, buffer, code_end);
+			code_start, html_converted, code_end);
 		break;
 	case LOG_INFO:
 		fwts_log_html(log_file, "  <TD></TD><TD COLSPAN=2 class=style_infos>%s%s%s</TD>\n",
-			code_start, buffer, code_end);
+			code_start, html_converted, code_end);
 		break;
 	case LOG_PASSED:
-		fwts_log_html(log_file, "<TD class=style_passed>PASSED</TD><TD>%s</TD>\n", buffer);
+		fwts_log_html(log_file, "<TD class=style_passed>PASSED</TD><TD>%s</TD>\n",
+			html_converted);
 		break;
 	case LOG_FAILED:
 		switch (level) {
@@ -128,30 +315,32 @@ static int fwts_log_print_html(
 		str = fwts_log_level_to_str(level);
 
 		fwts_log_html(log_file, "  <TD%s>%s [%s]</TD>\n", style, *status ? status : "", str);
-		fwts_log_html(log_file, "  <TD>%s</TD>\n", buffer);
+		fwts_log_html(log_file, "  <TD>%s</TD>\n", html_converted);
 		break;
 
 	case LOG_SKIPPED:
 		fwts_log_html(log_file, "<TD class=style_skipped>Skipped</TD>"
-			"<TD>%s%s%s</TD>\n", code_start, buffer, code_end);
+			"<TD>%s%s%s</TD>\n", code_start, html_converted, code_end);
 		break;
 
 	case LOG_SUMMARY:
 		fwts_log_html(log_file, "  <TD></TD>"
 			"<TD COLSPAN=2 class=style_summary>%s%s%s</TD>\n",
-			code_start, buffer, code_end);
+			code_start, html_converted, code_end);
 		break;
 
 	case LOG_ADVICE:
 		fwts_log_html(log_file, "  <TD class=style_advice>Advice</TD>"
 			"<TD COLSPAN=2 class=style_advice_info>%s%s%s</TD>\n",
-			code_start, buffer, code_end);
+			code_start, html_converted, code_end);
 		break;
 
 	default:
 		break;
 	}
 
+	free(html_converted);
+
 	fwts_log_html(log_file, "</TR>\n");
 	fflush(log_file->fp);
 
-- 
1.7.10.4





More information about the fwts-devel mailing list