From 33cf082c411da845c6b9bc01e57f0bd7264bebfd Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Sun, 19 Nov 2017 11:37:27 +0100 Subject: [PATCH] Fixed library path resolution in Spice netlist exporter SEARCH_STACK is a deprecated method for getting the list of paths where one could look for a file. Instead it tries the project path and environmental variables. --- common/env_paths.cpp | 40 +++++++++++++++++++ .../netlist_exporter_pspice.cpp | 5 ++- .../netlist_exporter_pspice.h | 10 ++--- eeschema/sim/netlist_exporter_pspice_sim.h | 4 +- eeschema/sim/sim_plot_frame.cpp | 2 +- include/env_paths.h | 14 ++++++- 6 files changed, 64 insertions(+), 11 deletions(-) diff --git a/common/env_paths.cpp b/common/env_paths.cpp index 11e959bf94..97b658066a 100644 --- a/common/env_paths.cpp +++ b/common/env_paths.cpp @@ -110,3 +110,43 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars return normalizedFullPath; } + + +// Create file path by appending path and file name. This approach allows the filename +// to contain a relative path, whereas wxFileName::SetPath() would replace the +// relative path +static wxString createFilePath( const wxString& aPath, const wxString& aFileName ) +{ + wxString path( aPath ); + + if( !path.EndsWith( wxFileName::GetPathSeparator() ) ) + path.Append( wxFileName::GetPathSeparator() ); + + return path + aFileName; +} + + +wxString ResolveFile( const wxString& aFileName, const ENV_VAR_MAP* aEnvVars, + const PROJECT* aProject ) +{ + if( aProject ) + { + wxFileName fn( createFilePath( aProject->GetProjectPath(), aFileName ) ); + + if( fn.Exists() ) + return fn.GetFullPath(); + } + + if( aEnvVars ) + { + for( auto& entry : *aEnvVars ) + { + wxFileName fn( createFilePath( entry.second.GetValue(), aFileName ) ); + + if( fn.Exists() ) + return fn.GetFullPath(); + } + } + + return wxEmptyString; +} diff --git a/eeschema/netlist_exporters/netlist_exporter_pspice.cpp b/eeschema/netlist_exporters/netlist_exporter_pspice.cpp index badb91e98c..be6d533f39 100644 --- a/eeschema/netlist_exporters/netlist_exporter_pspice.cpp +++ b/eeschema/netlist_exporters/netlist_exporter_pspice.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -72,10 +73,10 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* aFormatter, unsigned aCtl { wxString full_path; - if( ( aCtl & NET_ADJUST_INCLUDE_PATHS ) && m_paths ) + if( ( aCtl & NET_ADJUST_INCLUDE_PATHS ) ) { // Look for the library in known search locations - full_path = m_paths->FindValidPath( lib ); + full_path = ResolveFile( lib, &Pgm().GetLocalEnvVariables(), m_project ); if( full_path.IsEmpty() ) { diff --git a/eeschema/netlist_exporters/netlist_exporter_pspice.h b/eeschema/netlist_exporters/netlist_exporter_pspice.h index 5392357319..1fc42b032f 100644 --- a/eeschema/netlist_exporters/netlist_exporter_pspice.h +++ b/eeschema/netlist_exporters/netlist_exporter_pspice.h @@ -30,7 +30,7 @@ #include #include -class SEARCH_STACK; +class PROJECT; /// Flags for Spice netlist generation (can be combined) enum SPICE_NETLIST_OPTIONS { @@ -99,9 +99,9 @@ struct SPICE_ITEM class NETLIST_EXPORTER_PSPICE : public NETLIST_EXPORTER { public: - NETLIST_EXPORTER_PSPICE( NETLIST_OBJECT_LIST* aMasterList, SEARCH_STACK* aPaths = NULL ) : + NETLIST_EXPORTER_PSPICE( NETLIST_OBJECT_LIST* aMasterList, PROJECT* aProject = NULL ) : NETLIST_EXPORTER( aMasterList ), - m_paths( aPaths ) + m_project( aProject ) { } @@ -228,8 +228,8 @@ private: ///> List of items representing schematic components in the Spice world SPICE_ITEM_LIST m_spiceItems; - ///> Paths to be searched for included Spice libraries - SEARCH_STACK* m_paths; + ///> Project object to fetch its settings (e.g. paths) + PROJECT* m_project; // Component fields that are processed during netlist export & simulation static const std::vector m_spiceFields; diff --git a/eeschema/sim/netlist_exporter_pspice_sim.h b/eeschema/sim/netlist_exporter_pspice_sim.h index f9544a76f7..e93e88a2c1 100644 --- a/eeschema/sim/netlist_exporter_pspice_sim.h +++ b/eeschema/sim/netlist_exporter_pspice_sim.h @@ -36,8 +36,8 @@ class NETLIST_EXPORTER_PSPICE_SIM : public NETLIST_EXPORTER_PSPICE { public: - NETLIST_EXPORTER_PSPICE_SIM( NETLIST_OBJECT_LIST* aMasterList ) : - NETLIST_EXPORTER_PSPICE( aMasterList ) + NETLIST_EXPORTER_PSPICE_SIM( NETLIST_OBJECT_LIST* aMasterList, PROJECT* aProject = nullptr ) : + NETLIST_EXPORTER_PSPICE( aMasterList, aProject ) { } diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index f8c5a5bd2f..c7cb7bd806 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -459,7 +459,7 @@ void SIM_PLOT_FRAME::removePlot( const wxString& aPlotName, bool aErase ) void SIM_PLOT_FRAME::updateNetlistExporter() { - m_exporter.reset( new NETLIST_EXPORTER_PSPICE_SIM( m_schematicFrame->BuildNetListBase() ) ); + m_exporter.reset( new NETLIST_EXPORTER_PSPICE_SIM( m_schematicFrame->BuildNetListBase(), &Prj() ) ); } diff --git a/include/env_paths.h b/include/env_paths.h index f2eab67c09..bac92ffcac 100644 --- a/include/env_paths.h +++ b/include/env_paths.h @@ -29,7 +29,7 @@ /** * Normalizes a file path to an environmental variable, if possible. - * + * * @param aFilePath is the full file path (path and file name) to be normalized. * @param aEnvVars is an optional map of environmental variables to try substition with. * @param aProject is an optional project, to normalize the file path to the project path. @@ -39,4 +39,16 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars, const PROJECT* aProject ); +/** + * Searches the default paths trying to find one with the requested file. + * + * @param aFileName is the name of the searched file. It might be a relative path. + * @param aEnvVars is an optional map of environmental variables that can contain paths. + * @param aProject is an optional project, to check the project path. + * @return Full path (apth and file name) if the file was found in one of the paths, otherwise + * an empty string. +*/ +wxString ResolveFile( const wxString& aFileName, const ENV_VAR_MAP* aEnvVars, + const PROJECT* aProject ); + #endif /* ENV_PATHS_H */