diff --git a/common/footprint_info.cpp b/common/footprint_info.cpp index 40ed54d073..b109a37f82 100644 --- a/common/footprint_info.cpp +++ b/common/footprint_info.cpp @@ -146,8 +146,7 @@ FOOTPRINT_LIST* FOOTPRINT_LIST::GetInstance( KIWAY& aKiway ) if( !footprintInfo->GetCount() ) { - wxTextFile footprintInfoCache( aKiway.Prj().GetProjectPath() + "fp-info-cache" ); - footprintInfo->ReadCacheFromFile( &footprintInfoCache ); + footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + "fp-info-cache" ); } return footprintInfo; diff --git a/include/footprint_info.h b/include/footprint_info.h index 07b7162a71..f6a5501287 100644 --- a/include/footprint_info.h +++ b/include/footprint_info.h @@ -187,8 +187,8 @@ public: { } - virtual void WriteCacheToFile( wxTextFile* aFile ) { }; - virtual void ReadCacheFromFile( wxTextFile* aFile ) { }; + virtual void WriteCacheToFile( const wxString& aFilePath ) {}; + virtual void ReadCacheFromFile( const wxString& aFilePath ){}; /** * @return the number of items stored in list diff --git a/pcbnew/footprint_info_impl.cpp b/pcbnew/footprint_info_impl.cpp index 61d7233cc7..f6d1ca09e3 100644 --- a/pcbnew/footprint_info_impl.cpp +++ b/pcbnew/footprint_info_impl.cpp @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include @@ -339,59 +341,55 @@ FOOTPRINT_LIST_IMPL::~FOOTPRINT_LIST_IMPL() } -void FOOTPRINT_LIST_IMPL::WriteCacheToFile( wxTextFile* aCacheFile ) +void FOOTPRINT_LIST_IMPL::WriteCacheToFile( const wxString& aFilePath ) { - if( aCacheFile->Exists() ) - { - if( !aCacheFile->Open() ) - return; + wxFFileOutputStream outStream( aFilePath ); + wxTextOutputStream txtStream( outStream ); - aCacheFile->Clear(); - } - else + if( !outStream.IsOk() ) { - if( !aCacheFile->Create() ) - return; + return; } - aCacheFile->AddLine( wxString::Format( "%lld", m_list_timestamp ) ); + txtStream << wxString::Format( "%lld", m_list_timestamp ) << endl; - for( auto& fpinfo : m_list ) + for( std::unique_ptr& fpinfo : m_list ) { - aCacheFile->AddLine( fpinfo->GetLibNickname() ); - aCacheFile->AddLine( fpinfo->GetName() ); - aCacheFile->AddLine( EscapeString( fpinfo->GetDescription(), CTX_LINE ) ); - aCacheFile->AddLine( EscapeString( fpinfo->GetKeywords(), CTX_LINE ) ); - aCacheFile->AddLine( wxString::Format( "%d", fpinfo->GetOrderNum() ) ); - aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetPadCount() ) ); - aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetUniquePadCount() ) ); + txtStream << fpinfo->GetLibNickname() << endl; + txtStream << fpinfo->GetName() << endl; + txtStream << EscapeString( fpinfo->GetDescription(), CTX_LINE ) << endl; + txtStream << EscapeString( fpinfo->GetKeywords(), CTX_LINE ) << endl; + txtStream << wxString::Format( "%d", fpinfo->GetOrderNum() ) << endl; + txtStream << wxString::Format( "%u", fpinfo->GetPadCount() ) << endl; + txtStream << wxString::Format( "%u", fpinfo->GetUniquePadCount() ) << endl; } - aCacheFile->Write(); - aCacheFile->Close(); + txtStream.Flush(); } -void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( wxTextFile* aCacheFile ) +void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( const wxString& aFilePath ) { + wxTextFile cacheFile( aFilePath ); + m_list_timestamp = 0; m_list.clear(); try { - if( aCacheFile->Exists() && aCacheFile->Open() ) + if( cacheFile.Exists() && cacheFile.Open() ) { - aCacheFile->GetFirstLine().ToLongLong( &m_list_timestamp ); + cacheFile.GetFirstLine().ToLongLong( &m_list_timestamp ); - while( aCacheFile->GetCurrentLine() + 6 < aCacheFile->GetLineCount() ) + while( cacheFile.GetCurrentLine() + 6 < cacheFile.GetLineCount() ) { - wxString libNickname = aCacheFile->GetNextLine(); - wxString name = aCacheFile->GetNextLine(); - wxString description = UnescapeString( aCacheFile->GetNextLine() ); - wxString keywords = UnescapeString( aCacheFile->GetNextLine() ); - int orderNum = wxAtoi( aCacheFile->GetNextLine() ); - unsigned int padCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() ); - unsigned int uniquePadCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() ); + wxString libNickname = cacheFile.GetNextLine(); + wxString name = cacheFile.GetNextLine(); + wxString description = UnescapeString( cacheFile.GetNextLine() ); + wxString keywords = UnescapeString( cacheFile.GetNextLine() ); + int orderNum = wxAtoi( cacheFile.GetNextLine() ); + unsigned int padCount = (unsigned) wxAtoi( cacheFile.GetNextLine() ); + unsigned int uniquePadCount = (unsigned) wxAtoi( cacheFile.GetNextLine() ); auto* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, description, keywords, orderNum, padCount, uniquePadCount ); @@ -409,6 +407,6 @@ void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( wxTextFile* aCacheFile ) if( m_list.size() == 0 ) m_list_timestamp = 0; - if( aCacheFile->IsOpened() ) - aCacheFile->Close(); + if( cacheFile.IsOpened() ) + cacheFile.Close(); } diff --git a/pcbnew/footprint_info_impl.h b/pcbnew/footprint_info_impl.h index 3df0f3f896..c6610a1ddf 100644 --- a/pcbnew/footprint_info_impl.h +++ b/pcbnew/footprint_info_impl.h @@ -117,8 +117,8 @@ public: FOOTPRINT_LIST_IMPL(); virtual ~FOOTPRINT_LIST_IMPL(); - void WriteCacheToFile( wxTextFile* aFile ) override; - void ReadCacheFromFile( wxTextFile* aFile ) override; + void WriteCacheToFile( const wxString& aFilePath ) override; + void ReadCacheFromFile( const wxString& aFilePath ) override; bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr, PROGRESS_REPORTER* aProgressReporter = nullptr ) override; diff --git a/pcbnew/pcb_base_edit_frame.cpp b/pcbnew/pcb_base_edit_frame.cpp index 85b15baf44..8f13c08f6f 100644 --- a/pcbnew/pcb_base_edit_frame.cpp +++ b/pcbnew/pcb_base_edit_frame.cpp @@ -52,8 +52,7 @@ PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent, { if( !GFootprintList.GetCount() ) { - wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" ); - GFootprintList.ReadCacheFromFile( &footprintInfoCache ); + GFootprintList.ReadCacheFromFile( Prj().GetProjectPath() + "fp-info-cache" ); } } @@ -70,8 +69,7 @@ void PCB_BASE_EDIT_FRAME::doCloseWindow() if( mgr->IsProjectOpen() && wxFileName::IsDirWritable( Prj().GetProjectPath() ) ) { - wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" ); - GFootprintList.WriteCacheToFile( &footprintInfoCache ); + GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + "fp-info-cache" ); } // Close the project if we are standalone, so it gets cleaned up properly