Work in progress to allow arbitrary chars in references, etc.

This commit is contained in:
Jeff Young 2018-09-27 11:56:51 +01:00
parent eaf3d48260
commit 521183a587
3 changed files with 95 additions and 10 deletions

View File

@ -39,6 +39,79 @@
*/ */
static const char illegalFileNameChars[] = "\\/:\"<>|"; 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 += "&quot;";
else if( c == '\'' )
converted += "&apos;";
else if( c == '&' )
converted += "&amp;";
else if( c == '<' )
converted += "&lt;";
else if( c == '>' )
converted += "&gt;";
else if( c == '\\' )
converted += "&Backslash;";
else if( c == '/' )
converted += "&frasl;";
else if( c == '|' )
converted += "&verbar;";
else if( c == ':' )
converted += "&colon;";
else if( c == ' ' )
converted += "&nbsp;";
else if( c == '%' )
converted += "&percnt;";
else if( c == '$' )
converted += "&dollar;";
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( "&quot;", "\"" );
converted.Replace( "&apos;", "'" );
converted.Replace( "&lt;", "<" );
converted.Replace( "&gt;", ">" );
converted.Replace( "&Backslash;", "\\" );
converted.Replace( "&frasl;", "/" );
converted.Replace( "&verbar;", "|" );
converted.Replace( "&colon;", ":" );
converted.Replace( "&nbsp;", " " );
converted.Replace( "&percnt;", "%" );
converted.Replace( "&dollar;", "$" );
converted.Replace( "&tab;", "\t" );
converted.Replace( "&Newline;", "\n" );
// must be done last
converted.Replace( "&amp;", "&" );
return converted;
}
int ReadDelimitedText( wxString* aDest, const char* aSource ) int ReadDelimitedText( wxString* aDest, const char* aSource )
{ {

View File

@ -32,7 +32,7 @@
#include <common.h> #include <common.h>
#include <kiway.h> #include <kiway.h>
#include <confirm.h> #include <confirm.h>
#include <kicad_string.h>
#include <sch_base_frame.h> #include <sch_base_frame.h>
#include <sch_component.h> #include <sch_component.h>
#include <class_libentry.h> #include <class_libentry.h>
@ -40,7 +40,6 @@
#include <sch_component.h> #include <sch_component.h>
#include <template_fieldnames.h> #include <template_fieldnames.h>
#include <class_library.h> #include <class_library.h>
#include <sch_validators.h>
#include <dialog_edit_one_field.h> #include <dialog_edit_one_field.h>
@ -101,12 +100,7 @@ DIALOG_EDIT_ONE_FIELD::DIALOG_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, const wxS
void DIALOG_EDIT_ONE_FIELD::init() void DIALOG_EDIT_ONE_FIELD::init()
{ {
wxString msg;
SetInitialFocus( m_TextValue ); 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. // Disable options for graphic text editing which are not needed for fields.
m_CommonConvert->Show( false ); m_CommonConvert->Show( false );
@ -188,7 +182,7 @@ void DIALOG_EDIT_ONE_FIELD::OnSetFocusText( wxFocusEvent& event )
bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow() bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow()
{ {
m_TextValue->SetValue( m_text ); m_TextValue->SetValue( UnescapeString( m_text ) );
m_posX.SetValue( m_position.x ); m_posX.SetValue( m_position.x );
m_posY.SetValue( m_position.y ); m_posY.SetValue( m_position.y );
@ -206,9 +200,8 @@ bool DIALOG_EDIT_ONE_FIELD::TransferDataToWindow()
bool DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow() 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 ) if( m_fieldId == REFERENCE )
{ {
// Test if the reference string is valid: // Test if the reference string is valid:
@ -218,6 +211,14 @@ bool DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow()
return false; 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_isVertical = m_orientChoice->GetSelection() == 1;
m_position = wxPoint( m_posX.GetValue(), m_posY.GetValue() ); m_position = wxPoint( m_posX.GetValue(), m_posY.GetValue() );

View File

@ -36,6 +36,17 @@
#include <wx/filename.h> #include <wx/filename.h>
/**
* 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 * Function ReadDelimitedText
* copies bytes from @a aSource delimited string segment to @a aDest buffer. * copies bytes from @a aSource delimited string segment to @a aDest buffer.