From 4a9f82109a09209a9217f646bb571b9ba6252d95 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 7 Mar 2019 21:41:30 +0000 Subject: [PATCH] Uniformly quote strings which may have user content. Fixes: lp:1655193 * https://bugs.launchpad.net/kicad/+bug/1655193 --- common/richio.cpp | 64 +++++++++++++++++------------------------ pcbnew/kicad_plugin.cpp | 5 ++-- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/common/richio.cpp b/common/richio.cpp index 0e00fc277d..9cbaaf1b36 100644 --- a/common/richio.cpp +++ b/common/richio.cpp @@ -432,50 +432,40 @@ int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... ) std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) { - static const char quoteThese[] = "\t ()\n\r"; - - if( !aWrapee.size() || // quote null string as "" - aWrapee[0]=='#' || // quote a potential s-expression comment, so it is not a comment - aWrapee[0]=='"' || // NextTok() will travel through DSN_STRING path anyway, then must apply escapes - aWrapee.find_first_of( quoteThese ) != std::string::npos ) - { - std::string ret; + std::string ret; - ret.reserve( aWrapee.size()*2 + 2 ); + ret.reserve( aWrapee.size()*2 + 2 ); - ret += '"'; + ret += '"'; - for( std::string::const_iterator it = aWrapee.begin(); it!=aWrapee.end(); ++it ) + for( std::string::const_iterator it = aWrapee.begin(); it!=aWrapee.end(); ++it ) + { + switch( *it ) { - switch( *it ) - { - case '\n': - ret += '\\'; - ret += 'n'; - break; - case '\r': - ret += '\\'; - ret += 'r'; - break; - case '\\': - ret += '\\'; - ret += '\\'; - break; - case '"': - ret += '\\'; - ret += '"'; - break; - default: - ret += *it; - } + case '\n': + ret += '\\'; + ret += 'n'; + break; + case '\r': + ret += '\\'; + ret += 'r'; + break; + case '\\': + ret += '\\'; + ret += '\\'; + break; + case '"': + ret += '\\'; + ret += '"'; + break; + default: + ret += *it; } - - ret += '"'; - - return ret; } - return aWrapee; + ret += '"'; + + return ret; } diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index a3081a9d35..5b6960571d 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -1515,7 +1515,7 @@ void PCB_IO::format( TEXTE_PCB* aText, int aNestLevel ) const void PCB_IO::format( TEXTE_MODULE* aText, int aNestLevel ) const { - wxString type; + std::string type; switch( aText->GetType() ) { @@ -1524,8 +1524,7 @@ void PCB_IO::format( TEXTE_MODULE* aText, int aNestLevel ) const case TEXTE_MODULE::TEXT_is_DIVERS: type = "user"; } - m_out->Print( aNestLevel, "(fp_text %s %s (at %s", - m_out->Quotew( type ).c_str(), + m_out->Print( aNestLevel, "(fp_text %s %s (at %s", type.c_str(), m_out->Quotew( aText->GetText() ).c_str(), FormatInternalUnits( aText->GetPos0() ).c_str() );