From 6eca886292de391611037f5d3633e56669c40e0c Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Sun, 5 Jul 2020 12:25:04 -0400 Subject: [PATCH] Fix some UTF8 issues with project settings Fixes https://gitlab.com/kicad/code/kicad/-/issues/4808 --- common/settings/json_settings.cpp | 12 +++++++----- common/settings/settings_manager.cpp | 6 ++++-- include/project/project_local_settings.h | 5 +++++ kicad/treeprojectfiles.cpp | 3 +++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index d519fadb55..fb39e2ec2c 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -153,7 +153,8 @@ bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory ) } else { - path.Assign( aDirectory, m_filename, getFileExt() ); + wxString dir( aDirectory.c_str(), wxConvUTF8 ); + path.Assign( dir, m_filename, getFileExt() ); } if( !path.Exists() ) @@ -182,7 +183,7 @@ bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory ) { try { - std::ifstream in( path.GetFullPath().ToStdString() ); + std::ifstream in( path.GetFullPath().ToUTF8() ); in >> *this; // If parse succeeds, check if schema migration is required @@ -294,7 +295,8 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce ) } else { - path.Assign( aDirectory, m_filename, getFileExt() ); + wxString dir( aDirectory.c_str(), wxConvUTF8 ); + path.Assign( dir, m_filename, getFileExt() ); } if( !m_createIfMissing && !path.FileExists() ) @@ -338,7 +340,7 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce ) try { - std::ofstream file( path.GetFullPath().ToStdString() ); + std::ofstream file( path.GetFullPath().ToUTF8() ); file << std::setw( 2 ) << *this << std::endl; } catch( const std::exception& e ) @@ -551,4 +553,4 @@ void to_json( nlohmann::json& aJson, const wxString& aString ) void from_json( const nlohmann::json& aJson, wxString& aString ) { aString = wxString( aJson.get().c_str(), wxConvUTF8 ); -} \ No newline at end of file +} diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 22fa8a15b7..b37e270da8 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -706,7 +706,7 @@ bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath, bool aSetActive ) m_projects[fullPath].reset( project.release() ); - std::string fn( path.GetName() ); + std::string fn( path.GetName().ToUTF8() ); PROJECT_LOCAL_SETTINGS* settings = static_cast( RegisterSettings( new PROJECT_LOCAL_SETTINGS( fn ) ) ); @@ -783,7 +783,9 @@ bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject ) aProject.setProjectFile( file ); file->SetProject( &aProject ); - return file->LoadFromFile( std::string( fullFn.GetPath().ToUTF8() ) ); + std::string path( fullFn.GetPath().ToUTF8() ); + + return file->LoadFromFile( path ); } diff --git a/include/project/project_local_settings.h b/include/project/project_local_settings.h index a2ce498777..0fca124b65 100644 --- a/include/project/project_local_settings.h +++ b/include/project/project_local_settings.h @@ -62,6 +62,11 @@ protected: return ProjectLocalSettingsFileExtension; } + wxString getLegacyFileExt() const override + { + return wxT( "NO_SUCH_FILE_EXTENSION" ); + } + private: /// A link to the owning project diff --git a/kicad/treeprojectfiles.cpp b/kicad/treeprojectfiles.cpp index 5d5233a25a..877bf8744a 100644 --- a/kicad/treeprojectfiles.cpp +++ b/kicad/treeprojectfiles.cpp @@ -94,6 +94,9 @@ int TREEPROJECTFILES::OnCompareItems( const wxTreeItemId& item1, const wxTreeIte TREEPROJECT_ITEM* myitem1 = (TREEPROJECT_ITEM*) GetItemData( item1 ); TREEPROJECT_ITEM* myitem2 = (TREEPROJECT_ITEM*) GetItemData( item2 ); + if( !myitem1 || !myitem2 ) + return 0; + if( myitem1->GetType() == TREE_DIRECTORY && myitem2->GetType() != TREE_DIRECTORY ) return -1;