Harden drawing sheet loading slightly...

The fact it blindly assumed it would always work makes me sad :( So this is just ducktape
This commit is contained in:
Marek Roszko 2021-05-01 13:17:39 -04:00
parent 18ff8ea4a0
commit cc1bfd4c18
3 changed files with 29 additions and 13 deletions

View File

@ -808,7 +808,7 @@ void DS_DATA_MODEL::SetPageLayout( const char* aPageLayout, bool Append, const w
} }
void DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append ) bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append )
{ {
wxString fullFileName = aFullFileName; wxString fullFileName = aFullFileName;
@ -824,7 +824,7 @@ void DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append
wxLogMessage( wxT( "Drawing sheet file <%s> not found" ), fullFileName.GetData() ); wxLogMessage( wxT( "Drawing sheet file <%s> not found" ), fullFileName.GetData() );
#endif #endif
SetDefaultLayout(); SetDefaultLayout();
return; return false;
} }
} }
@ -834,14 +834,17 @@ void DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append
{ {
if( !Append ) if( !Append )
SetDefaultLayout(); SetDefaultLayout();
return; return false;
} }
size_t filelen = wksFile.Length(); size_t filelen = wksFile.Length();
char * buffer = new char[filelen+10]; std::unique_ptr<char[]> buffer = std::make_unique<char[]>(filelen+10);
if( wksFile.Read( buffer, filelen ) != filelen ) if( wksFile.Read( buffer.get(), filelen ) != filelen )
wxLogMessage( _("The file \"%s\" was not fully read"), fullFileName.GetData() ); {
wxLogMessage( _( "The file \"%s\" was not fully read" ), fullFileName.GetData() );
return false;
}
else else
{ {
buffer[filelen]=0; buffer[filelen]=0;
@ -849,7 +852,7 @@ void DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append
if( ! Append ) if( ! Append )
ClearList(); ClearList();
DRAWING_SHEET_READER_PARSER pl_parser( buffer, fullFileName ); DRAWING_SHEET_READER_PARSER pl_parser( buffer.get(), fullFileName );
try try
{ {
@ -858,8 +861,14 @@ void DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append
catch( const IO_ERROR& ioe ) catch( const IO_ERROR& ioe )
{ {
wxLogMessage( ioe.What() ); wxLogMessage( ioe.What() );
return false;
}
catch( const std::bad_alloc& )
{
wxLogMessage( "Memory exhaustion reading drawing sheet" );
return false;
} }
} }
delete[] buffer; return true;
} }

View File

@ -154,7 +154,7 @@ public:
* default internal description. * default internal description.
* @param Append if true: do not delete old layout, and load only \a aFullFileName. * @param Append if true: do not delete old layout, and load only \a aFullFileName.
*/ */
void LoadDrawingSheet( const wxString& aFullFileName = wxEmptyString, bool Append = false ); bool LoadDrawingSheet( const wxString& aFullFileName = wxEmptyString, bool Append = false );
/** /**
* Populate the list from a S expr description stored in a string. * Populate the list from a S expr description stored in a string.

View File

@ -223,7 +223,16 @@ bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
{ {
if( wxFileExists( aFullFileName ) ) if( wxFileExists( aFullFileName ) )
{ {
DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName ); bool loaded = false;
loaded = DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName );
if( !loaded )
{
ShowInfoBarError( _( "Error reading drawing sheet" ), true );
return false;
}
SetCurrentFileName( aFullFileName ); SetCurrentFileName( aFullFileName );
UpdateFileHistory( aFullFileName ); UpdateFileHistory( aFullFileName );
GetScreen()->ClrModify(); GetScreen()->ClrModify();
@ -233,9 +242,7 @@ bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
if( fn.FileExists() && !fn.IsFileWritable() ) if( fn.FileExists() && !fn.IsFileWritable() )
{ {
m_infoBar->RemoveAllButtons(); ShowInfoBarWarning( _( "Layout file is read only." ), true );
m_infoBar->AddCloseButton();
m_infoBar->ShowMessage( _( "Layout file is read only." ), wxICON_WARNING );
} }
return true; return true;