From: José Fonseca Date: Tue, 16 Feb 2010 16:28:59 +0000 (+0000) Subject: Escape special XML chars. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=b5f29aa186ed32b2f715a66aa4a65fc8e6a34707;p=apitrace Escape special XML chars. --- diff --git a/log.cpp b/log.cpp index 8064c4b..1e05287 100644 --- a/log.cpp +++ b/log.cpp @@ -117,10 +117,64 @@ static inline void Write(const char *szText) { Write(szText, strlen(szText)); } +static inline void Write(char c) +{ + Write(&c, 1); +} + +static inline void +WriteF(const char *format, ...) +{ + char szBuffer[4096]; + va_list ap; + va_start(ap, format); + vsnprintf(szBuffer, sizeof(szBuffer), format, ap); + va_end(ap); + Write(szBuffer); +} + +static inline void +Escape(char c) +{ + switch(c) { + case '&': + Write("&"); + break; + case '<': + Write("<"); + break; + case '>': + Write(">"); + break; + case '"': + Write("""); + break; + case '\'': + Write("'"); + break; + default: + Write(c); + } +} + static inline void -Escape(const char *s) { - /* FIXME */ - Write(s); +Escape(const char *s) +{ + unsigned char c; + while((c = *s++) != 0) { + Escape(c); + } +} + +static inline void +EscapeF(const char *format, ...) +{ + char szBuffer[4096]; + va_list ap; + va_start(ap, format); + vsnprintf(szBuffer, sizeof(szBuffer), format, ap); + va_end(ap); + Escape(szBuffer); } static inline void @@ -230,20 +284,13 @@ void Text(const char *text) { Escape(text); } -static void TextChar(char c) { - char szText[2]; - szText[0] = c; - szText[1] = 0; - Text(szText); -} - void TextF(const char *format, ...) { char szBuffer[4096]; va_list ap; va_start(ap, format); vsnprintf(szBuffer, sizeof(szBuffer), format, ap); va_end(ap); - Text(szBuffer); + Escape(szBuffer); } static LARGE_INTEGER frequency = {0}; @@ -326,73 +373,73 @@ void EndReference(void) { void DumpString(const char *str) { const unsigned char *p = (const unsigned char *)str; if (!str) { - Log::Text("NULL"); + Write("NULL"); return; } - Log::Text("\""); + Write("\""); unsigned char c; while((c = *p++) != 0) { if(c == '\"') - Text("\\\""); + Write("\\\""); else if(c == '\\') - Text("\\\\"); + Write("\\\\"); else if(c >= 0x20 && c <= 0x7e) - TextChar(c); + Write(c); else if(c == '\t') - Text(" "); + Write(" "); else if(c == '\r') - Text(" "); + Write(" "); else if(c == '\n') - Text(" "); + Write(" "); else { unsigned char octal0 = c & 0x7; unsigned char octal1 = (c >> 3) & 0x7; unsigned char octal2 = (c >> 3) & 0x7; if(octal2) - TextF("\\%u%u%u", octal2, octal1, octal0); + WriteF("\\%u%u%u", octal2, octal1, octal0); else if(octal1) - TextF("\\%u%u", octal1, octal0); + WriteF("\\%u%u", octal1, octal0); else - TextF("\\%u", octal0); + WriteF("\\%u", octal0); } } - Log::Text("\""); + Write("\""); } void DumpWString(const wchar_t *str) { const wchar_t *p = str; if (!str) { - Log::Text("NULL"); + Write("NULL"); return; } - Log::Text("L\""); + Write("L\""); wchar_t c; while((c = *p++) != 0) { if(c == '\"') - Text("\\\""); + Write("\\\""); else if(c == '\\') - Text("\\\\"); + Write("\\\\"); else if(c >= 0x20 && c <= 0x7e) - TextChar((char)c); + Write((char)c); else if(c == '\t') - Text(" "); + Write(" "); else if(c == '\r') - Text(" "); + Write(" "); else if(c == '\n') - Text(" "); + Write(" "); else { unsigned octal0 = c & 0x7; unsigned octal1 = (c >> 3) & 0x7; unsigned octal2 = (c >> 3) & 0x7; if(octal2) - TextF("\\%u%u%u", octal2, octal1, octal0); + WriteF("\\%u%u%u", octal2, octal1, octal0); else if(octal1) - TextF("\\%u%u", octal1, octal0); + WriteF("\\%u%u", octal1, octal0); else - TextF("\\%u", octal0); + WriteF("\\%u", octal0); } } - Log::Text("\""); + Write("\""); } } /* namespace Log */