Formatting.

This commit is contained in:
Jeff Young 2024-01-31 12:18:01 +00:00
parent 7f29ac39d8
commit 445ed380b8
4 changed files with 101 additions and 99 deletions

View File

@ -100,7 +100,7 @@ JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation,
JSON_SETTINGS::~JSON_SETTINGS() JSON_SETTINGS::~JSON_SETTINGS()
{ {
for( auto param: m_params ) for( PARAM_BASE* param: m_params )
delete param; delete param;
m_params.clear(); m_params.clear();
@ -136,7 +136,7 @@ JSON_SETTINGS_INTERNALS* JSON_SETTINGS::Internals()
void JSON_SETTINGS::Load() void JSON_SETTINGS::Load()
{ {
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
{ {
try try
{ {
@ -163,68 +163,76 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
LOCALE_IO locale; LOCALE_IO locale;
auto migrateFromLegacy = [&] ( wxFileName& aPath ) { auto migrateFromLegacy =
// Backup and restore during migration so that the original can be mutated if convenient [&] ( wxFileName& aPath )
bool backed_up = false;
wxFileName temp;
if( aPath.IsDirWritable() )
{
temp.AssignTempFileName( aPath.GetFullPath() );
if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
{ {
wxLogTrace( traceSettings, wxT( "%s: could not create temp file for migration" ), // Backup and restore during migration so that the original can be mutated if
GetFullFilename() ); // convenient
} bool backed_up = false;
else wxFileName temp;
backed_up = true;
}
// Silence popups if legacy file is read-only if( aPath.IsDirWritable() )
wxLogNull doNotLog; {
temp.AssignTempFileName( aPath.GetFullPath() );
wxConfigBase::DontCreateOnDemand(); if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), aPath.GetFullPath() ); {
wxLogTrace( traceSettings,
wxT( "%s: could not create temp file for migration" ),
GetFullFilename() );
}
else
{
backed_up = true;
}
}
// If migrate fails or is not implemented, fall back to built-in defaults that were // Silence popups if legacy file is read-only
// already loaded above wxLogNull doNotLog;
if( !MigrateFromLegacy( cfg.get() ) )
{
success = false;
wxLogTrace( traceSettings,
wxT( "%s: migrated; not all settings were found in legacy file" ),
GetFullFilename() );
}
else
{
success = true;
wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ),
GetFullFilename() );
}
if( backed_up ) wxConfigBase::DontCreateOnDemand();
{ auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ),
cfg.reset(); aPath.GetFullPath() );
if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) ) // If migrate fails or is not implemented, fall back to built-in defaults that
{ // were already loaded above
wxLogTrace( traceSettings, if( !MigrateFromLegacy( cfg.get() ) )
wxT( "migrate; copy temp file %s to %s failed" ), {
temp.GetFullPath(), aPath.GetFullPath() ); success = false;
} wxLogTrace( traceSettings,
wxT( "%s: migrated; not all settings were found in legacy file" ),
GetFullFilename() );
}
else
{
success = true;
wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ),
GetFullFilename() );
}
if( !wxRemoveFile( temp.GetFullPath() ) ) if( backed_up )
{ {
wxLogTrace( traceSettings, cfg.reset();
wxT( "migrate; failed to remove temp file %s" ),
temp.GetFullPath() );
}
}
// Either way, we want to clean up the old file afterwards if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) )
legacy_migrated = true; {
}; wxLogTrace( traceSettings,
wxT( "migrate; copy temp file %s to %s failed" ),
temp.GetFullPath(),
aPath.GetFullPath() );
}
if( !wxRemoveFile( temp.GetFullPath() ) )
{
wxLogTrace( traceSettings,
wxT( "migrate; failed to remove temp file %s" ),
temp.GetFullPath() );
}
}
// Either way, we want to clean up the old file afterwards
legacy_migrated = true;
};
wxFileName path; wxFileName path;
@ -298,7 +306,7 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
if( filever >= 0 && filever < m_schemaVersion ) if( filever >= 0 && filever < m_schemaVersion )
{ {
wxLogTrace( traceSettings, wxT( "%s: attempting migration from version " wxLogTrace( traceSettings, wxT( "%s: attempting migration from version "
"%d to %d" ), "%d to %d" ),
GetFullFilename(), filever, m_schemaVersion ); GetFullFilename(), filever, m_schemaVersion );
if( Migrate() ) if( Migrate() )
@ -339,7 +347,7 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
Load(); Load();
// And finally load any nested settings // And finally load any nested settings
for( auto settings : m_nested_settings ) for( NESTED_SETTINGS* settings : m_nested_settings )
settings->LoadFromFile(); settings->LoadFromFile();
wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(), wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(),
@ -367,7 +375,7 @@ bool JSON_SETTINGS::Store()
{ {
bool modified = false; bool modified = false;
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
{ {
modified |= !param->MatchesFile( this ); modified |= !param->MatchesFile( this );
param->Store( this ); param->Store( this );
@ -379,7 +387,7 @@ bool JSON_SETTINGS::Store()
void JSON_SETTINGS::ResetToDefaults() void JSON_SETTINGS::ResetToDefaults()
{ {
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
param->SetDefault(); param->SetDefault();
} }
@ -432,7 +440,7 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
bool modified = false; bool modified = false;
for( auto settings : m_nested_settings ) for( NESTED_SETTINGS* settings : m_nested_settings )
modified |= settings->SaveToFile(); modified |= settings->SaveToFile();
modified |= Store(); modified |= Store();
@ -832,8 +840,9 @@ void JSON_SETTINGS::ReleaseNestedSettings( NESTED_SETTINGS* aSettings )
return; return;
auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(), auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(),
[&aSettings]( const JSON_SETTINGS* aPtr ) { [&aSettings]( const JSON_SETTINGS* aPtr )
return aPtr == aSettings; {
return aPtr == aSettings;
} ); } );
if( it != m_nested_settings.end() ) if( it != m_nested_settings.end() )

View File

@ -311,13 +311,13 @@ void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing )
m_ptr->clear(); m_ptr->clear();
for( const auto& el : js->items() ) for( const auto& el : js->items() )
{
( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>(); ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
}
} }
} }
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
}
} }

View File

@ -116,7 +116,7 @@ public:
/** /**
* Calls Store() and then writes the contents of the JSON document to a file * 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 * @param aDirectory is the directory to save to, including trailing separator
c * @return true if the file was saved * @return true if the file was saved
*/ */
virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ); virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2020 Jon Evans <jon@craftyjon.com> * Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2020-2024 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software: you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@ -18,8 +18,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _PARAMETERS_H #ifndef PARAMETERS_H
#define _PARAMETERS_H #define PARAMETERS_H
#include <set> #include <set>
#include <string> #include <string>
@ -69,13 +69,8 @@ public:
const std::string& GetJsonPath() const { return m_path; } const std::string& GetJsonPath() const { return m_path; }
protected: protected:
/** std::string m_path; ///< Address of the param in the json files
* the string used to store the param in json files bool m_readOnly; ///< Indicates param pointer should never be overwritten
*/
std::string m_path;
///! True if the parameter pointer should never be overwritten
bool m_readOnly;
}; };
@ -121,7 +116,9 @@ public:
*m_ptr = val; *m_ptr = val;
} }
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
}
} }
void Store( JSON_SETTINGS* aSettings ) const override void Store( JSON_SETTINGS* aSettings ) const override
@ -148,13 +145,13 @@ public:
} }
private: private:
ValueType m_min; ValueType m_min;
ValueType m_max; ValueType m_max;
bool m_use_minmax; bool m_use_minmax;
protected: protected:
ValueType* m_ptr; ValueType* m_ptr;
ValueType m_default; ValueType m_default;
}; };
/** /**
@ -240,7 +237,9 @@ public:
} }
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
}
} }
void Store( JSON_SETTINGS* aSettings ) const override void Store( JSON_SETTINGS* aSettings ) const override
@ -316,10 +315,8 @@ public:
bool MatchesFile( JSON_SETTINGS* aSettings ) const override; bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
private: private:
ValueType m_default; ValueType m_default;
std::function<ValueType()> m_getter;
std::function<ValueType()> m_getter;
std::function<void( ValueType )> m_setter; std::function<void( ValueType )> m_setter;
}; };
@ -404,11 +401,11 @@ public:
private: private:
ValueType* m_ptr; ValueType* m_ptr;
ValueType m_default; ValueType m_default;
ValueType m_min; ValueType m_min;
ValueType m_max; ValueType m_max;
bool m_use_minmax; bool m_use_minmax;
double m_scale; double m_scale;
}; };
template<typename Type> template<typename Type>
@ -442,8 +439,7 @@ public:
protected: protected:
std::vector<Type>* m_ptr; std::vector<Type>* m_ptr;
std::vector<Type> m_default;
std::vector<Type> m_default;
}; };
template<typename Type> template<typename Type>
@ -477,8 +473,7 @@ public:
protected: protected:
std::set<Type>* m_ptr; std::set<Type>* m_ptr;
std::set<Type> m_default;
std::set<Type> m_default;
}; };
/** /**
@ -568,8 +563,7 @@ public:
private: private:
std::map<std::string, Value>* m_ptr; std::map<std::string, Value>* m_ptr;
std::map<std::string, Value> m_default;
std::map<std::string, Value> m_default;
}; };
@ -580,8 +574,8 @@ class PARAM_WXSTRING_MAP : public PARAM_BASE
{ {
public: public:
PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr, PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr,
std::initializer_list<std::pair<const wxString, wxString>> aDefault, std::initializer_list<std::pair<const wxString, wxString>> aDefault,
bool aReadOnly = false ) : bool aReadOnly = false ) :
PARAM_BASE( aJsonPath, aReadOnly ), PARAM_BASE( aJsonPath, aReadOnly ),
m_ptr( aPtr ), m_ptr( aPtr ),
m_default( aDefault ) m_default( aDefault )
@ -600,8 +594,7 @@ public:
private: private:
std::map<wxString, wxString>* m_ptr; std::map<wxString, wxString>* m_ptr;
std::map<wxString, wxString> m_default;
std::map<wxString, wxString> m_default;
}; };
#endif #endif