From 521183a587e3fc6b13c43e81a3e4d39c6b140b2e Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 27 Sep 2018 11:56:51 +0100 Subject: [PATCH] Work in progress to allow arbitrary chars in references, etc. --- common/string.cpp | 73 ++++++++++++++++++++++ eeschema/dialogs/dialog_edit_one_field.cpp | 21 ++++--- include/kicad_string.h | 11 ++++ 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/common/string.cpp b/common/string.cpp index 08ed5e8861..1d1d8bef6c 100644 --- a/common/string.cpp +++ b/common/string.cpp @@ -39,6 +39,79 @@ */ static const char illegalFileNameChars[] = "\\/:\"<>|"; +/** + * These Escape/Unescape routines use HTML-entity-reference-style encoding to handle + * characters which are: + * (a) not legal in filenames + * (b) used as control characters in LIB_IDs + * (c) used to delineate hierarchical paths + */ +wxString EscapeString( const wxString& aSource ) +{ + wxString converted; + + for( wxUniChar c: aSource ) + { + if( c == '\"' ) + converted += """; + else if( c == '\'' ) + converted += "'"; + else if( c == '&' ) + converted += "&"; + else if( c == '<' ) + converted += "<"; + else if( c == '>' ) + converted += ">"; + else if( c == '\\' ) + converted += "∖"; + else if( c == '/' ) + converted += "⁄"; + else if( c == '|' ) + converted += "|"; + else if( c == ':' ) + converted += ":"; + else if( c == ' ' ) + converted += " "; + else if( c == '%' ) + converted += "%"; + else if( c == '$' ) + converted += "$"; + else if( c == '\t' ) + converted += "&tab;"; + else if( c == '\n' || c == '\r' ) + converted += "&Newline;"; + else + converted += c; + } + + return converted; +} + + +wxString UnescapeString( const wxString& aSource ) +{ + wxString converted = aSource; + + converted.Replace( """, "\"" ); + converted.Replace( "'", "'" ); + converted.Replace( "<", "<" ); + converted.Replace( ">", ">" ); + converted.Replace( "∖", "\\" ); + converted.Replace( "⁄", "/" ); + converted.Replace( "|", "|" ); + converted.Replace( ":", ":" ); + converted.Replace( " ", " " ); + converted.Replace( "%", "%" ); + converted.Replace( "$", "$" ); + converted.Replace( "&tab;", "\t" ); + converted.Replace( "&Newline;", "\n" ); + + // must be done last + converted.Replace( "&", "&" ); + + return converted; +} + int ReadDelimitedText( wxString* aDest, const char* aSource ) { diff --git a/eeschema/dialogs/dialog_edit_one_field.cpp b/eeschema/dialogs/dialog_edit_one_field.cpp index e6295cbc45..a378d69326 100644 --- a/eeschema/dialogs/dialog_edit_one_field.cpp +++ b/eeschema/dialogs/dialog_edit_one_field.cpp @@ -32,7 +32,7 @@ #include #include #include - +#include #include #include #include @@ -40,7 +40,6 @@ #include #include #include -#include #include @@ -101,12 +100,7 @@ DIALOG_EDIT_ONE_FIELD::DIALOG_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, const wxS void DIALOG_EDIT_ONE_FIELD::init() { - wxString msg; - SetInitialFocus( m_TextValue ); - SCH_BASE_FRAME* parent = GetParent(); - bool libedit = parent->IsType( FRAME_SCH_LIB_EDITOR ); - m_TextValue->SetValidator( SCH_FIELD_VALIDATOR( libedit, m_fieldId, &m_text ) ); // Disable options for graphic text editing which are not needed for fields. m_CommonConvert->Show( false ); @@ -188,7 +182,7 @@ void DIALOG_EDIT_ONE_FIELD::OnSetFocusText( wxFocusEvent& event ) bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow() { - m_TextValue->SetValue( m_text ); + m_TextValue->SetValue( UnescapeString( m_text ) ); m_posX.SetValue( m_position.x ); m_posY.SetValue( m_position.y ); @@ -206,9 +200,8 @@ bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow() bool DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow() { - m_text = m_TextValue->GetValue(); + m_text = EscapeString( m_TextValue->GetValue() ); - // There are lots of specific tests required to validate field text. if( m_fieldId == REFERENCE ) { // Test if the reference string is valid: @@ -218,6 +211,14 @@ bool DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow() return false; } } + else if( m_fieldId == VALUE ) + { + if( m_text.IsEmpty() ) + { + DisplayError( this, _( "Value may not be empty." ) ); + return false; + } + } m_isVertical = m_orientChoice->GetSelection() == 1; m_position = wxPoint( m_posX.GetValue(), m_posY.GetValue() ); diff --git a/include/kicad_string.h b/include/kicad_string.h index e997aa2928..e499fa9336 100644 --- a/include/kicad_string.h +++ b/include/kicad_string.h @@ -36,6 +36,17 @@ #include +/** + * These Escape/Unescape routines use HTML-entity-reference-style encoding to handle + * characters which are: + * (a) not legal in filenames + * (b) used as control characters in LIB_IDs + * (c) used to delineate hierarchical paths + */ +wxString EscapeString( const wxString& aSource ); + +wxString UnescapeString( const wxString& aSource ); + /** * Function ReadDelimitedText * copies bytes from @a aSource delimited string segment to @a aDest buffer.