From ec497d5d9aa1987ffd54ab6bc0994c5fcb969390 Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Fri, 26 Apr 2013 21:12:57 -0500 Subject: [PATCH] Fix SVG plot with filename containing & character. --- common/common_plotSVG_functions.cpp | 63 ++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/common/common_plotSVG_functions.cpp b/common/common_plotSVG_functions.cpp index 766a29ff01..d5a039e441 100644 --- a/common/common_plotSVG_functions.cpp +++ b/common/common_plotSVG_functions.cpp @@ -99,6 +99,65 @@ #include #include + + +/** + * Function XmlEsc + * translates '<' to "<", '>' to ">" and so on, according to the spec: + * http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping + * May be moved to a library if needed generally, but not expecting that. + */ +static wxString XmlEsc( const wxString& aStr, bool isAttribute = false ) +{ + wxString escaped; + + escaped.reserve( aStr.length() ); + + for( wxString::const_iterator it = aStr.begin(); it != aStr.end(); ++it ) + { + const wxChar c = *it; + + switch( c ) + { + case wxS( '<' ): + escaped.append( wxS( "<" ) ); + break; + case wxS( '>' ): + escaped.append( wxS( ">" ) ); + break; + case wxS( '&' ): + escaped.append( wxS( "&" ) ); + break; + case wxS( '\r' ): + escaped.append( wxS( " " ) ); + break; + default: + if( isAttribute ) + { + switch( c ) + { + case wxS( '"' ): + escaped.append( wxS( """ ) ); + break; + case wxS( '\t' ): + escaped.append( wxS( " " ) ); + break; + case wxS( '\n' ): + escaped.append( wxS( " " )); + break; + default: + escaped.append(c); + } + } + else + escaped.append(c); + } + } + + return escaped; +} + + SVG_PLOTTER::SVG_PLOTTER() { m_graphics_changed = true; @@ -460,10 +519,10 @@ bool SVG_PLOTTER::StartPlot() fprintf( outputFile, "SVG Picture created as %s date %s \n", - TO_UTF8( wxFileName( filename ).GetFullName() ), date_buf ); + TO_UTF8( XmlEsc( wxFileName( filename ).GetFullName() ) ), date_buf ); // End of header fprintf( outputFile, " Picture generated by %s \n", - TO_UTF8( creator ) ); + TO_UTF8( XmlEsc( creator ) ) ); // output the pen and brush color (RVB values in hex) and opacity double opacity = 1.0; // 0.0 (transparent to 1.0 (solid)