Settings management: try to fix full filename issues when using non ASCII7 chars.

The fix convert all std::string storing a path to wxString (unicode support)
wxString were already used at many place to store paths, but not all.
For internal calculations mixing char strings and wide char strings is a recipe
for bug: any missing conversion between UTF8 and wxString breaks paths.
This commit is contained in:
jean-pierre charras 2020-08-02 11:23:21 +02:00
parent f3b92903e2
commit df4226f896
14 changed files with 54 additions and 58 deletions

View File

@ -34,7 +34,7 @@ extern const char* traceSettings;
const int projectFileSchemaVersion = 1;
PROJECT_FILE::PROJECT_FILE( const std::string& aFullPath ) :
PROJECT_FILE::PROJECT_FILE( const wxString& aFullPath ) :
JSON_SETTINGS( aFullPath, SETTINGS_LOC::PROJECT, projectFileSchemaVersion ),
m_sheets(),
m_boards(),
@ -630,7 +630,7 @@ bool PROJECT_FILE::MigrateFromLegacy( wxConfigBase* aCfg )
}
bool PROJECT_FILE::SaveToFile( const std::string& aDirectory, bool aForce )
bool PROJECT_FILE::SaveToFile( const wxString& aDirectory, bool aForce )
{
wxASSERT( m_project );

View File

@ -25,7 +25,7 @@
const int projectLocalSettingsVersion = 1;
PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( const std::string& aFilename ) :
PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( const wxString& aFilename ) :
JSON_SETTINGS( aFilename, SETTINGS_LOC::PROJECT, projectLocalSettingsVersion,
/* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false,
/* aWriteFile = */ true ),
@ -164,7 +164,7 @@ bool PROJECT_LOCAL_SETTINGS::MigrateFromLegacy( wxConfigBase* aLegacyConfig )
}
bool PROJECT_LOCAL_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
bool PROJECT_LOCAL_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
{
wxASSERT( m_project );

View File

@ -31,7 +31,7 @@ extern const char* traceSettings;
const int colorsSchemaVersion = 1;
COLOR_SETTINGS::COLOR_SETTINGS( std::string aFilename ) :
COLOR_SETTINGS::COLOR_SETTINGS( wxString aFilename ) :
JSON_SETTINGS( std::move( aFilename ), SETTINGS_LOC::COLORS, colorsSchemaVersion ),
m_overrideSchItemColors( false )
{

View File

@ -36,7 +36,7 @@
extern const char* traceSettings;
JSON_SETTINGS::JSON_SETTINGS( const std::string& aFilename, SETTINGS_LOC aLocation,
JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation,
int aSchemaVersion, bool aCreateIfMissing, bool aCreateIfDefault,
bool aWriteFile ) :
nlohmann::json(),
@ -69,7 +69,7 @@ JSON_SETTINGS::~JSON_SETTINGS()
wxString JSON_SETTINGS::GetFullFilename() const
{
return wxString( m_filename.c_str(), wxConvUTF8 ) + "." + getFileExt();
return wxString( m_filename + "." + getFileExt() );
}
@ -92,7 +92,7 @@ void JSON_SETTINGS::Load()
}
bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory )
bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
{
// First, load all params to default values
clear();
@ -158,13 +158,8 @@ bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory )
}
else
{
wxString dir( aDirectory.c_str(), wxConvUTF8 );
#ifdef __WINDOWS__
wxString dir( aDirectory );
path.Assign( dir, m_filename, getFileExt() );
#else
wxString name( m_filename.c_str(), wxConvUTF8 );
path.Assign( dir, name, getFileExt() );
#endif
}
if( !path.Exists() )
@ -249,7 +244,7 @@ bool JSON_SETTINGS::LoadFromFile( const std::string& aDirectory )
for( auto settings : m_nested_settings )
settings->LoadFromFile();
wxLogTrace( traceSettings, "Loaded %s with schema %d", GetFullFilename(), m_schemaVersion );
wxLogTrace( traceSettings, "Loaded <%s> with schema %d", GetFullFilename(), m_schemaVersion );
// If we migrated, clean up the legacy file (with no extension)
if( legacy_migrated || migrated )
@ -289,13 +284,13 @@ void JSON_SETTINGS::ResetToDefaults()
}
bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
{
if( !m_writeFile )
return false;
// Default PROJECT won't have a filename set
if( m_filename.empty() )
if( m_filename.IsEmpty() )
return false;
wxFileName path;
@ -307,7 +302,7 @@ bool JSON_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
}
else
{
wxString dir( aDirectory.c_str(), wxConvUTF8 );
wxString dir( aDirectory );
path.Assign( dir, m_filename, getFileExt() );
}

View File

@ -41,7 +41,7 @@ NESTED_SETTINGS::~NESTED_SETTINGS()
}
bool NESTED_SETTINGS::LoadFromFile( const std::string& aDirectory )
bool NESTED_SETTINGS::LoadFromFile( const wxString& aDirectory )
{
clear();
bool success = false;
@ -75,7 +75,7 @@ bool NESTED_SETTINGS::LoadFromFile( const std::string& aDirectory )
}
bool NESTED_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
bool NESTED_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
{
if( !m_parent )
return false;

View File

@ -86,7 +86,7 @@ JSON_SETTINGS* SETTINGS_MANAGER::RegisterSettings( JSON_SETTINGS* aSettings, boo
ptr->SetManager( this );
wxLogTrace( traceSettings, "Registered new settings object %s", ptr->GetFullFilename() );
wxLogTrace( traceSettings, "Registered new settings object <%s>", ptr->GetFullFilename() );
if( aLoadNow )
ptr->LoadFromFile( GetPathForSettingsFile( ptr.get() ) );
@ -249,9 +249,10 @@ void SETTINGS_MANAGER::registerColorSettings( const wxString& aFilename )
{
if( m_color_settings.count( aFilename ) )
return;
//wxMessageBox( aFilename, "registerColorSettings1" );
//wxMessageBox( aFilename.ToStdString().c_str(), "registerColorSettings2" );
m_color_settings[aFilename] = static_cast<COLOR_SETTINGS*>(
RegisterSettings( new COLOR_SETTINGS( aFilename.ToStdString() ) ) );
RegisterSettings( new COLOR_SETTINGS( aFilename ) ) );
}
@ -317,7 +318,7 @@ void SETTINGS_MANAGER::SaveColorSettings( COLOR_SETTINGS* aSettings, const std::
aNamespace );
nlohmann::json backup = aSettings->at( ptr );
std::string path = GetColorSettingsPath();
wxString path = GetColorSettingsPath();
aSettings->LoadFromFile( path );
@ -328,7 +329,7 @@ void SETTINGS_MANAGER::SaveColorSettings( COLOR_SETTINGS* aSettings, const std::
}
std::string SETTINGS_MANAGER::GetPathForSettingsFile( JSON_SETTINGS* aSettings )
wxString SETTINGS_MANAGER::GetPathForSettingsFile( JSON_SETTINGS* aSettings )
{
wxASSERT( aSettings );
@ -338,7 +339,7 @@ std::string SETTINGS_MANAGER::GetPathForSettingsFile( JSON_SETTINGS* aSettings )
return GetUserSettingsPath();
case SETTINGS_LOC::PROJECT:
return std::string( Prj().GetProjectPath().ToUTF8() );
return std::string( Prj().GetProjectPath() );
case SETTINGS_LOC::COLORS:
return GetColorSettingsPath();
@ -545,20 +546,20 @@ bool SETTINGS_MANAGER::IsSettingsPathValid( const wxString& aPath )
}
std::string SETTINGS_MANAGER::GetColorSettingsPath()
wxString SETTINGS_MANAGER::GetColorSettingsPath()
{
wxFileName path;
path.AssignDir( GetUserSettingsPath() );
path.AppendDir( "colors" );
return path.GetPath().ToStdString();
return path.GetPath();
}
std::string SETTINGS_MANAGER::GetUserSettingsPath()
wxString SETTINGS_MANAGER::GetUserSettingsPath()
{
static std::string user_settings_path;
static wxString user_settings_path;
if( user_settings_path.empty() )
user_settings_path = calculateUserSettingsPath();
@ -567,7 +568,7 @@ std::string SETTINGS_MANAGER::GetUserSettingsPath()
}
std::string SETTINGS_MANAGER::calculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
wxString SETTINGS_MANAGER::calculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
{
wxFileName cfgpath;
@ -707,7 +708,7 @@ bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath, bool aSetActive )
m_projects[fullPath].reset( project.release() );
std::string fn( path.GetName().ToUTF8() );
wxString fn( path.GetName() );
PROJECT_LOCAL_SETTINGS* settings = static_cast<PROJECT_LOCAL_SETTINGS*>(
RegisterSettings( new PROJECT_LOCAL_SETTINGS( fn ) ) );
@ -762,7 +763,7 @@ bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath )
return false;
PROJECT_FILE* project = m_project_files.at( path );
std::string projectPath = GetPathForSettingsFile( project );
wxString projectPath = GetPathForSettingsFile( project );
project->SaveToFile( projectPath );
Prj().GetLocalSettings().SaveToFile( projectPath );
@ -774,7 +775,7 @@ bool SETTINGS_MANAGER::SaveProject( const wxString& aFullPath )
bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject )
{
wxFileName fullFn( aProject.GetProjectFullName() );
std::string fn( fullFn.GetName().ToUTF8() );
wxString fn( fullFn.GetName() );
PROJECT_FILE* file =
static_cast<PROJECT_FILE*>( RegisterSettings( new PROJECT_FILE( fn ), false ) );
@ -784,7 +785,7 @@ bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject )
aProject.setProjectFile( file );
file->SetProject( &aProject );
std::string path( fullFn.GetPath().ToUTF8() );
wxString path( fullFn.GetPath() );
return file->LoadFromFile( path );
}
@ -810,7 +811,7 @@ bool SETTINGS_MANAGER::unloadProjectFile( PROJECT* aProject, bool aSave )
if( it != m_settings.end() )
{
std::string projectPath = GetPathForSettingsFile( it->get() );
wxString projectPath = GetPathForSettingsFile( it->get() );
FlushAndRelease( &aProject->GetLocalSettings(), aSave );

View File

@ -340,7 +340,7 @@ public:
BOARD_DESIGN_SETTINGS& operator=( const BOARD_DESIGN_SETTINGS& aOther );
bool LoadFromFile( const std::string& aDirectory = "" ) override;
bool LoadFromFile( const wxString& aDirectory = "" ) override;
BOARD_STACKUP& GetStackupDescriptor() { return m_stackup; }

View File

@ -66,13 +66,13 @@ public:
* Constructs the project file for a project
* @param aFullPath is the full disk path to the project
*/
PROJECT_FILE( const std::string& aFullPath );
PROJECT_FILE( const wxString& aFullPath );
virtual ~PROJECT_FILE() = default;
virtual bool MigrateFromLegacy( wxConfigBase* aCfg ) override;
bool SaveToFile( const std::string& aDirectory = "", bool aForce = false ) override;
bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ) override;
void SetProject( PROJECT* aProject )
{

View File

@ -43,13 +43,13 @@ class PROJECT;
class PROJECT_LOCAL_SETTINGS : public JSON_SETTINGS
{
public:
PROJECT_LOCAL_SETTINGS( const std::string& aFilename );
PROJECT_LOCAL_SETTINGS( const wxString& aFilename );
virtual ~PROJECT_LOCAL_SETTINGS() {}
bool MigrateFromLegacy( wxConfigBase* aLegacyConfig ) override;
bool SaveToFile( const std::string& aDirectory = "", bool aForce = false ) override;
bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ) override;
void SetProject( PROJECT* aProject )
{

View File

@ -60,7 +60,7 @@ public:
*/
std::vector<COLOR4D> m_Palette;
explicit COLOR_SETTINGS( std::string aFilename = "user" );
explicit COLOR_SETTINGS( wxString aFilename = "user" );
virtual ~COLOR_SETTINGS() {}

View File

@ -45,21 +45,21 @@ enum class SETTINGS_LOC {
class JSON_SETTINGS : public nlohmann::json
{
public:
JSON_SETTINGS( const std::string& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion ) :
JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion ) :
JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true ) {}
JSON_SETTINGS( const std::string& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion,
JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, int aSchemaVersion,
bool aCreateIfMissing, bool aCreateIfDefault, bool aWriteFile );
virtual ~JSON_SETTINGS();
std::string GetFilename() const { return m_filename; }
wxString GetFilename() const { return m_filename; }
wxString GetFullFilename() const;
SETTINGS_LOC GetLocation() const { return m_location; }
void SetLegacyFilename( const std::string& aFilename ) { m_legacy_filename = aFilename; }
void SetLegacyFilename( const wxString& aFilename ) { m_legacy_filename = aFilename; }
/**
* Updates the parameters of this object based on the current JSON document contents
@ -78,14 +78,14 @@ public:
* @param aDirectory is the path to the file
* @return true if the file was found on disk and loaded or migrated
*/
virtual bool LoadFromFile( const std::string& aDirectory = "" );
virtual bool LoadFromFile( const wxString& aDirectory = "" );
/**
* Calls Store() and then writes the contents of the JSON document to a file
* @param aDirectory is the directory to save to, including trailing separator
c * @return true if the file was saved
*/
virtual bool SaveToFile( const std::string& aDirectory = "", bool aForce = false );
virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );
/**
* Resets all parameters to default values. Does NOT write to file or update underlying JSON.
@ -224,11 +224,11 @@ protected:
return wxEmptyString;
}
/// The filename (not including path) of this settings file
std::string m_filename;
/// The filename (not including path) of this settings file (inicode)
wxString m_filename;
/// The filename of the wxConfig legacy file (if different from m_filename)
std::string m_legacy_filename;
wxString m_legacy_filename;
/// The location of this settings file (@see SETTINGS_LOC)
SETTINGS_LOC m_location;

View File

@ -40,14 +40,14 @@ public:
* Loads the JSON document from the parent and then calls Load()
* @param aDirectory
*/
bool LoadFromFile( const std::string& aDirectory = "" ) override;
bool LoadFromFile( const wxString& aDirectory = "" ) override;
/**
* Calls Store() and then saves the JSON document contents into the parent JSON_SETTINGS
* @param aDirectory is ignored
* @return true if the document contents were updated
*/
bool SaveToFile( const std::string& aDirectory = "", bool aForce = false ) override;
bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ) override;
void SetParent( JSON_SETTINGS* aParent );

View File

@ -146,7 +146,7 @@ public:
* @param aSettings is the settings object
* @return a path based on aSettings->m_location
*/
std::string GetPathForSettingsFile( JSON_SETTINGS* aSettings );
wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
/**
* Handles the initialization of the user settings directory and migration from previous
@ -253,7 +253,7 @@ public:
* Returns the path where color scheme files are stored
* (normally ./colors/ under the user settings path)
*/
static std::string GetColorSettingsPath();
static wxString GetColorSettingsPath();
/**
* Return the user configuration path used to store KiCad's configuration files.
@ -264,7 +264,7 @@ public:
*
* @return A string containing the config path for Kicad
*/
static std::string GetUserSettingsPath();
static wxString GetUserSettingsPath();
/**
* Parses the current KiCad build version and extracts the major and minor revision to use
@ -290,7 +290,7 @@ private:
* @param aUseEnv will prefer the base path found in the KICAD_CONFIG_DIR if found (default)
* @return A string containing the config path for Kicad
*/
static std::string calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
static wxString calculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
/**
* Compares two settings versions, like "5.99" and "6.0"

View File

@ -659,7 +659,7 @@ void BOARD_DESIGN_SETTINGS::initFromOther( const BOARD_DESIGN_SETTINGS& aOther )
}
bool BOARD_DESIGN_SETTINGS::LoadFromFile( const std::string& aDirectory )
bool BOARD_DESIGN_SETTINGS::LoadFromFile( const wxString& aDirectory )
{
bool ret = NESTED_SETTINGS::LoadFromFile( aDirectory );