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

View File

@ -2,7 +2,7 @@
* 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-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
* 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/>.
*/
#ifndef _PARAMETERS_H
#define _PARAMETERS_H
#ifndef PARAMETERS_H
#define PARAMETERS_H
#include <set>
#include <string>
@ -69,13 +69,8 @@ public:
const std::string& GetJsonPath() const { return m_path; }
protected:
/**
* the string used to store the param in json files
*/
std::string m_path;
///! True if the parameter pointer should never be overwritten
bool m_readOnly;
std::string m_path; ///< Address of the param in the json files
bool m_readOnly; ///< Indicates param pointer should never be overwritten
};
@ -121,7 +116,9 @@ public:
*m_ptr = val;
}
else if( aResetIfMissing )
{
*m_ptr = m_default;
}
}
void Store( JSON_SETTINGS* aSettings ) const override
@ -148,13 +145,13 @@ public:
}
private:
ValueType m_min;
ValueType m_max;
bool m_use_minmax;
ValueType m_min;
ValueType m_max;
bool m_use_minmax;
protected:
ValueType* m_ptr;
ValueType m_default;
ValueType m_default;
};
/**
@ -240,7 +237,9 @@ public:
}
else if( aResetIfMissing )
{
*m_ptr = m_default;
}
}
void Store( JSON_SETTINGS* aSettings ) const override
@ -316,10 +315,8 @@ public:
bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
private:
ValueType m_default;
std::function<ValueType()> m_getter;
ValueType m_default;
std::function<ValueType()> m_getter;
std::function<void( ValueType )> m_setter;
};
@ -404,11 +401,11 @@ public:
private:
ValueType* m_ptr;
ValueType m_default;
ValueType m_min;
ValueType m_max;
bool m_use_minmax;
double m_scale;
ValueType m_default;
ValueType m_min;
ValueType m_max;
bool m_use_minmax;
double m_scale;
};
template<typename Type>
@ -442,8 +439,7 @@ public:
protected:
std::vector<Type>* m_ptr;
std::vector<Type> m_default;
std::vector<Type> m_default;
};
template<typename Type>
@ -477,8 +473,7 @@ public:
protected:
std::set<Type>* m_ptr;
std::set<Type> m_default;
std::set<Type> m_default;
};
/**
@ -568,8 +563,7 @@ public:
private:
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:
PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr,
std::initializer_list<std::pair<const wxString, wxString>> aDefault,
bool aReadOnly = false ) :
std::initializer_list<std::pair<const wxString, wxString>> aDefault,
bool aReadOnly = false ) :
PARAM_BASE( aJsonPath, aReadOnly ),
m_ptr( aPtr ),
m_default( aDefault )
@ -600,8 +594,7 @@ public:
private:
std::map<wxString, wxString>* m_ptr;
std::map<wxString, wxString> m_default;
std::map<wxString, wxString> m_default;
};
#endif