From d67cf2f9afa40361bdefe195704fba881fd7c7d2 Mon Sep 17 00:00:00 2001
From: Marek Roszko <mark.roszko@gmail.com>
Date: Sat, 12 Dec 2020 13:11:51 -0500
Subject: [PATCH] Replace wxFile usage with wxFFile

Buffered libc wxFFile is better thinking about syscall wxFile going wrong.
---
 common/dialogs/wx_html_report_panel.cpp   |  3 ++-
 common/hotkeys_basic.cpp                  |  8 +++-----
 common/page_layout/page_layout_reader.cpp |  3 ++-
 eeschema/bom_plugins.cpp                  |  3 ++-
 eeschema/eeschema.cpp                     |  5 +++--
 eeschema/files-io.cpp                     |  5 +++--
 eeschema/sim/sim_plot_frame.cpp           |  3 ++-
 gerbview/gerbview.cpp                     |  5 +++--
 kicad/kicad_manager_frame.cpp             | 14 ++++++++------
 libs/sexpr/sexpr_parser.cpp               |  3 ++-
 10 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/common/dialogs/wx_html_report_panel.cpp b/common/dialogs/wx_html_report_panel.cpp
index 3651dd5bcf..f3c39db71a 100644
--- a/common/dialogs/wx_html_report_panel.cpp
+++ b/common/dialogs/wx_html_report_panel.cpp
@@ -27,6 +27,7 @@
 #include <gal/color4d.h>
 #include <wx/clipbrd.h>
 #include <kicad_string.h>
+#include <wx/ffile.h>
 
 WX_HTML_REPORT_PANEL::WX_HTML_REPORT_PANEL( wxWindow* parent,
                                             wxWindowID id,
@@ -335,7 +336,7 @@ void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
     if( fn.GetExt().IsEmpty() )
         fn.SetExt( "txt" );
 
-    wxFile f( fn.GetFullPath(), wxFile::write );
+    wxFFile f( fn.GetFullPath(), "wb" );
 
     if( !f.IsOpened() )
     {
diff --git a/common/hotkeys_basic.cpp b/common/hotkeys_basic.cpp
index 8e239711cd..2c615b8313 100644
--- a/common/hotkeys_basic.cpp
+++ b/common/hotkeys_basic.cpp
@@ -335,7 +335,7 @@ void ReadHotKeyConfig( wxString fileName, std::map<std::string, int>& aHotKeys )
     if( !wxFile::Exists( fileName ) )
         return;
 
-    wxFile file( fileName, wxFile::OpenMode::read );
+    wxFFile file( fileName, "rb" );
 
     if( !file.IsOpened() )       // There is a problem to open file
         return;
@@ -418,15 +418,13 @@ int ReadLegacyHotkeyConfigFile( const wxString& aFilename, std::map<std::string,
     if( !wxFile::Exists( fn.GetFullPath() ) )
         return 0;
 
-    wxFile cfgfile( fn.GetFullPath() );
+    wxFFile cfgfile( fn.GetFullPath(), "rb" );
 
     if( !cfgfile.IsOpened() )       // There is a problem to open file
         return 0;
 
     // get length
-    cfgfile.SeekEnd();
-    wxFileOffset size = cfgfile.Tell();
-    cfgfile.Seek( 0 );
+    wxFileOffset size = cfgfile.Length();
 
     // read data
     std::vector<char> buffer( size );
diff --git a/common/page_layout/page_layout_reader.cpp b/common/page_layout/page_layout_reader.cpp
index e889d1e9f6..c6f52b1edb 100644
--- a/common/page_layout/page_layout_reader.cpp
+++ b/common/page_layout/page_layout_reader.cpp
@@ -36,6 +36,7 @@
 #include <page_layout/ws_draw_item.h>
 #include <page_layout/ws_painter.h>
 #include <page_layout/page_layout_reader_lexer.h>
+#include <wx/ffile.h>
 
 #include <wx/file.h>
 #include <wx/mstream.h>
@@ -837,7 +838,7 @@ void WS_DATA_MODEL::SetPageLayout( const wxString& aFullFileName, bool Append )
         }
     }
 
-    wxFile wksFile( fullFileName );
+    wxFFile wksFile( fullFileName, "rb" );
 
     if( ! wksFile.IsOpened() )
     {
diff --git a/eeschema/bom_plugins.cpp b/eeschema/bom_plugins.cpp
index 4d992946ec..32f0c7018b 100644
--- a/eeschema/bom_plugins.cpp
+++ b/eeschema/bom_plugins.cpp
@@ -23,6 +23,7 @@
  */
 
 #include "bom_plugins.h"
+#include <wx/ffile.h>
 
 BOM_GENERATOR_HANDLER::BOM_GENERATOR_HANDLER( const wxString& aFile )
     : m_file( aFile )
@@ -95,7 +96,7 @@ wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
     if( aEndSection.IsEmpty() )
         return wxEmptyString;
 
-    wxFile fdata( m_file.GetFullPath() );        // dtor will close the file
+    wxFFile fdata( m_file.GetFullPath(), "rb" );        // dtor will close the file
     wxString data;
 
     if( !fdata.ReadAll( &data ) )
diff --git a/eeschema/eeschema.cpp b/eeschema/eeschema.cpp
index 722c27a79e..13e802691a 100644
--- a/eeschema/eeschema.cpp
+++ b/eeschema/eeschema.cpp
@@ -43,6 +43,7 @@
 #include <sexpr/sexpr_parser.h>
 #include <kiface_ids.h>
 #include <netlist_exporters/netlist_exporter_kicad.h>
+#include <wx/ffile.h>
 
 #include <schematic.h>
 #include <connection_graph.h>
@@ -377,12 +378,12 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
                     }
                 } );
 
-            wxFile destNetList( destFile.GetFullPath(), wxFile::write );
+            wxFFile destNetList( destFile.GetFullPath(), "wb" );
 
             if( destNetList.IsOpened() )
                 success = destNetList.Write( sexpr->AsString( 0 ) );
 
-            // wxFile dtor will close the file
+            // wxFFile dtor will close the file
         }
         catch( ... )
         {
diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp
index ac09b3317d..1ebd934474 100644
--- a/eeschema/files-io.cpp
+++ b/eeschema/files-io.cpp
@@ -57,6 +57,7 @@
 #include <widgets/infobar.h>
 #include <wildcards_and_files_ext.h>
 #include <page_layout/ws_data_model.h>
+#include <wx/ffile.h>
 #include <wx/stdpaths.h>
 #include <tools/ee_inspection_tool.h>
 
@@ -927,9 +928,9 @@ bool SCH_EDIT_FRAME::importFile( const wxString& aFileName, int aFileType )
 
             BASE_SCREEN::m_PageLayoutDescrFileName = "empty.kicad_wks";
             wxFileName layoutfn( Prj().GetProjectPath(), BASE_SCREEN::m_PageLayoutDescrFileName );
-            wxFile layoutfile;
+            wxFFile layoutfile;
 
-            if( layoutfile.Create( layoutfn.GetFullPath() ) )
+            if( layoutfile.Open( layoutfn.GetFullPath(), "wb" ) )
             {
                 layoutfile.Write( WS_DATA_MODEL::EmptyLayout() );
                 layoutfile.Close();
diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp
index 75af8420eb..976481029f 100644
--- a/eeschema/sim/sim_plot_frame.cpp
+++ b/eeschema/sim/sim_plot_frame.cpp
@@ -45,6 +45,7 @@
 #include <tool/tool_manager.h>
 #include <tools/ee_actions.h>
 #include <eeschema_settings.h>
+#include <wx/ffile.h>
 
 SIM_PLOT_TYPE operator|( SIM_PLOT_TYPE aFirst, SIM_PLOT_TYPE aSecond )
 {
@@ -1099,7 +1100,7 @@ void SIM_PLOT_FRAME::menuSaveCsv( wxCommandEvent& event )
     if( saveDlg.ShowModal() == wxID_CANCEL )
         return;
 
-    wxFile out( saveDlg.GetPath(), wxFile::write );
+    wxFFile out( saveDlg.GetPath(), "wb" );
     bool timeWritten = false;
 
     for( const auto& t : CurrentPlot()->GetTraces() )
diff --git a/gerbview/gerbview.cpp b/gerbview/gerbview.cpp
index 4275059876..1f25ed6fb1 100644
--- a/gerbview/gerbview.cpp
+++ b/gerbview/gerbview.cpp
@@ -35,6 +35,7 @@
 #include <pgm_base.h>
 #include <settings/settings_manager.h>
 #include <wildcards_and_files_ext.h>
+#include <wx/ffile.h>
 
 using json = nlohmann::json;
 
@@ -207,12 +208,12 @@ void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProje
                 }
             }
 
-            wxFile destJobFile( destFile.GetFullPath(), wxFile::write );
+            wxFFile destJobFile( destFile.GetFullPath(), "wb" );
 
             if( destJobFile.IsOpened() )
                 success = destJobFile.Write( js.dump( 0 ) );
 
-            // wxFile dtor will close the file
+            // wxFFile dtor will close the file
         }
         catch( ... )
         {
diff --git a/kicad/kicad_manager_frame.cpp b/kicad/kicad_manager_frame.cpp
index f6916ff9ac..be1d82e8e9 100644
--- a/kicad/kicad_manager_frame.cpp
+++ b/kicad/kicad_manager_frame.cpp
@@ -49,6 +49,8 @@
 #include <tools/kicad_manager_control.h>
 #include <wildcards_and_files_ext.h>
 #include <widgets/app_progress_dialog.h>
+#include <wx/ffile.h>
+
 
 #ifdef __WXMAC__
 #include <MacTypes.h>
@@ -474,11 +476,11 @@ void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName,
             if( !wxFileName::FileExists( srcFileName )
                 || !wxCopyFile( srcFileName, destFileName.GetFullPath() ) )
             {
-                wxFile file( destFileName.GetFullPath(), wxFile::write );
+                wxFFile file( destFileName.GetFullPath(), "wb" );
 
                 if( file.IsOpened() )
                     file.Write( wxT( "{\n}\n") );
-                // wxFile dtor will close the file
+                // wxFFile dtor will close the file
             }
         }
     }
@@ -494,7 +496,7 @@ void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName,
         // If a <project>.kicad_sch file does not exist, create a "stub" file ( minimal schematic file )
         if( !fn.FileExists() )
         {
-            wxFile file( fn.GetFullPath(), wxFile::write );
+            wxFFile file( fn.GetFullPath(), "wb" );
 
             if( file.IsOpened() )
                 file.Write( wxT( "(kicad_sch (version 20200310) (host eeschema \"unknown\")\n"
@@ -502,7 +504,7 @@ void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName,
                                  "  (symbol_instances)\n)\n" ) );
 
 
-            // wxFile dtor will close the file
+            // wxFFile dtor will close the file
         }
 
         // If a <project>.kicad_pcb or <project>.brd file does not exist,
@@ -513,12 +515,12 @@ void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName,
 
         if( !fn.FileExists() && !leg_fn.FileExists() )
         {
-            wxFile file( fn.GetFullPath(), wxFile::write );
+            wxFFile file( fn.GetFullPath(), "wb" );
 
             if( file.IsOpened() )
                 file.Write( wxT( "(kicad_pcb (version 4) (host kicad \"dummy file\") )\n" ) );
 
-            // wxFile dtor will close the file
+            // wxFFile dtor will close the file
         }
     }
 
diff --git a/libs/sexpr/sexpr_parser.cpp b/libs/sexpr/sexpr_parser.cpp
index 62586acd5a..ae2feda990 100644
--- a/libs/sexpr/sexpr_parser.cpp
+++ b/libs/sexpr/sexpr_parser.cpp
@@ -26,6 +26,7 @@
 #include <fstream>
 #include <streambuf>
 #include <wx/file.h>
+#include <wx/ffile.h>
 
 #include <macros.h>
 
@@ -62,7 +63,7 @@ namespace SEXPR
         // the filename is not always a UTF7 string, so do not use ifstream
         // that do not work with unicode chars.
         wxString fname( FROM_UTF8( aFileName.c_str() ) );
-        wxFile file( fname );
+        wxFFile file( fname, "rb" );
         size_t length = file.Length();
 
         if( length <= 0 )