2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-12-21 00:14:00 +00:00
|
|
|
* Copyright (C) 2015-2020 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2023-01-19 00:18:11 +00:00
|
|
|
* Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* 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 Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
2020-01-15 23:42:16 +00:00
|
|
|
#include <mutex>
|
2016-01-15 00:28:28 +00:00
|
|
|
#include <sstream>
|
2020-01-15 23:42:16 +00:00
|
|
|
|
2016-02-23 03:33:24 +00:00
|
|
|
#include <wx/log.h>
|
2016-04-17 22:35:32 +00:00
|
|
|
#include <pgm_base.h>
|
2018-05-21 10:09:49 +00:00
|
|
|
#include <trace_helpers.h>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2023-01-19 00:18:11 +00:00
|
|
|
#include <common.h>
|
2024-01-16 22:20:45 +00:00
|
|
|
#include <env_vars.h>
|
2023-01-19 00:18:11 +00:00
|
|
|
#include <filename_resolver.h>
|
|
|
|
#include <confirm.h>
|
2022-08-14 17:36:40 +00:00
|
|
|
#include <wx_filename.h>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
// configuration file version
|
|
|
|
#define CFGFILE_VERSION 1
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
// flag bits used to track different one-off messages to users
|
2016-01-19 00:05:40 +00:00
|
|
|
#define ERRFLG_ALIAS (1)
|
2016-01-15 00:28:28 +00:00
|
|
|
#define ERRFLG_RELPATH (2)
|
2016-01-19 00:05:40 +00:00
|
|
|
#define ERRFLG_ENVPATH (4)
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-02-23 03:33:24 +00:00
|
|
|
#define MASK_3D_RESOLVER "3D_RESOLVER"
|
|
|
|
|
2020-01-15 23:42:16 +00:00
|
|
|
static std::mutex mutex_resolver;
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
FILENAME_RESOLVER::FILENAME_RESOLVER() :
|
|
|
|
m_pgm( nullptr ),
|
|
|
|
m_project( nullptr )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
|
|
|
m_errflags = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
bool FILENAME_RESOLVER::Set3DConfigDir( const wxString& aConfigDir )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( aConfigDir.empty() )
|
|
|
|
return false;
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
wxFileName cfgdir( ExpandEnvVarSubstitutions( aConfigDir, m_project ), "" );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2022-08-14 17:36:40 +00:00
|
|
|
cfgdir.Normalize( FN_NORMALIZE_FLAGS );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
if( !cfgdir.DirExists() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
m_configDir = cfgdir.GetPath();
|
2015-12-08 07:31:57 +00:00
|
|
|
createPathList();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
bool FILENAME_RESOLVER::SetProject( PROJECT* aProject, bool* flgChanged )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-04-05 19:51:48 +00:00
|
|
|
m_project = aProject;
|
|
|
|
|
|
|
|
if( !aProject )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
wxFileName projdir( ExpandEnvVarSubstitutions( aProject->GetProjectPath(), aProject ), "" );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2022-08-14 17:36:40 +00:00
|
|
|
projdir.Normalize( FN_NORMALIZE_FLAGS );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
if( !projdir.DirExists() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
m_curProjDir = projdir.GetPath();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( flgChanged )
|
|
|
|
*flgChanged = false;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( m_paths.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2018-03-07 13:05:12 +00:00
|
|
|
SEARCH_PATH al;
|
2023-01-17 00:07:41 +00:00
|
|
|
al.m_Alias = wxS( "${KIPRJMOD}" );
|
|
|
|
al.m_Pathvar = wxS( "${KIPRJMOD}" );
|
2020-11-16 11:16:44 +00:00
|
|
|
al.m_Pathexp = m_curProjDir;
|
|
|
|
m_paths.push_back( al );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( flgChanged )
|
|
|
|
*flgChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-22 10:27:44 +00:00
|
|
|
if( m_paths.front().m_Pathexp != m_curProjDir )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
m_paths.front().m_Pathexp = m_curProjDir;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( flgChanged )
|
|
|
|
*flgChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2020-04-05 19:51:48 +00:00
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
ostr << " * [INFO] changed project dir to ";
|
2020-11-16 11:16:44 +00:00
|
|
|
ostr << m_paths.front().m_Pathexp.ToUTF8();
|
2016-02-23 03:33:24 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2020-04-05 19:51:48 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
wxString FILENAME_RESOLVER::GetProjectDir() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
return m_curProjDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
void FILENAME_RESOLVER::SetProgramBase( PGM_BASE* aBase )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
|
|
|
m_pgm = aBase;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( !m_pgm || m_paths.empty() )
|
2016-04-17 22:35:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// recreate the path list
|
2020-11-16 11:16:44 +00:00
|
|
|
m_paths.clear();
|
2016-04-17 22:35:32 +00:00
|
|
|
createPathList();
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
bool FILENAME_RESOLVER::createPathList()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
if( !m_paths.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
|
2016-02-25 03:31:59 +00:00
|
|
|
// add an entry for the default search path; at this point
|
|
|
|
// we cannot set a sensible default so we use an empty string.
|
2015-12-08 07:31:57 +00:00
|
|
|
// the user may change this later with a call to SetProjectDir()
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
SEARCH_PATH lpath;
|
2023-01-17 00:07:41 +00:00
|
|
|
lpath.m_Alias = wxS( "${KIPRJMOD}" );
|
|
|
|
lpath.m_Pathvar = wxS( "${KIPRJMOD}" );
|
2020-11-16 11:16:44 +00:00
|
|
|
lpath.m_Pathexp = m_curProjDir;
|
|
|
|
m_paths.push_back( lpath );
|
2016-04-17 22:35:32 +00:00
|
|
|
wxFileName fndummy;
|
|
|
|
wxUniChar psep = fndummy.GetPathSeparator();
|
2016-06-12 22:46:00 +00:00
|
|
|
std::list< wxString > epaths;
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-06-12 22:46:00 +00:00
|
|
|
if( GetKicadPaths( epaths ) )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
2022-09-26 01:08:51 +00:00
|
|
|
for( const wxString& currPath : epaths )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
2022-09-26 01:08:51 +00:00
|
|
|
wxString currPathVarFormat = currPath;
|
|
|
|
currPathVarFormat.Prepend( wxS( "${" ) );
|
|
|
|
currPathVarFormat.Append( wxS( "}" ) );
|
|
|
|
|
|
|
|
wxString pathVal = ExpandEnvVarSubstitutions( currPathVarFormat, m_project );
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-06-12 22:46:00 +00:00
|
|
|
if( pathVal.empty() )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
lpath.m_Pathexp.clear();
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
2016-06-12 22:46:00 +00:00
|
|
|
else
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
2016-06-12 22:46:00 +00:00
|
|
|
fndummy.Assign( pathVal, "" );
|
2022-08-14 17:36:40 +00:00
|
|
|
fndummy.Normalize( FN_NORMALIZE_FLAGS );
|
2020-11-16 11:16:44 +00:00
|
|
|
lpath.m_Pathexp = fndummy.GetFullPath();
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2022-09-26 01:08:51 +00:00
|
|
|
lpath.m_Alias = currPath;
|
|
|
|
lpath.m_Pathvar = currPath;
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( !lpath.m_Pathexp.empty() && psep == *lpath.m_Pathexp.rbegin() )
|
|
|
|
lpath.m_Pathexp.erase( --lpath.m_Pathexp.end() );
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2022-09-26 01:08:51 +00:00
|
|
|
// we add it first with the alias set to the non-variable format
|
|
|
|
m_paths.push_back( lpath );
|
|
|
|
|
|
|
|
// now add it with the "new variable format ${VAR}"
|
|
|
|
lpath.m_Alias = currPathVarFormat;
|
2020-11-16 11:16:44 +00:00
|
|
|
m_paths.push_back( lpath );
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( m_paths.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2023-01-17 00:07:41 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, wxS( " * [3D model] search paths:\n" ) );
|
2020-11-16 11:16:44 +00:00
|
|
|
std::list< SEARCH_PATH >::const_iterator sPL = m_paths.begin();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
while( sPL != m_paths.end() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2023-01-17 00:07:41 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, wxS( " + %s : '%s'\n" ), (*sPL).m_Alias.GetData(),
|
2020-11-16 11:16:44 +00:00
|
|
|
(*sPL).m_Pathexp.GetData() );
|
2015-12-08 07:31:57 +00:00
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
bool FILENAME_RESOLVER::UpdatePathList( const std::vector< SEARCH_PATH >& aPathList )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
wxUniChar envMarker( '$' );
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
while( !m_paths.empty() && envMarker != *m_paths.back().m_Alias.rbegin() )
|
|
|
|
m_paths.pop_back();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
for( const SEARCH_PATH& path : aPathList )
|
|
|
|
addPath( path );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2022-09-26 01:08:51 +00:00
|
|
|
return true;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-26 02:54:08 +00:00
|
|
|
wxString FILENAME_RESOLVER::ResolvePath( const wxString& aFileName, const wxString& aWorkingPath )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-01-15 23:42:16 +00:00
|
|
|
std::lock_guard<std::mutex> lock( mutex_resolver );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
if( aFileName.empty() )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( m_paths.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
createPathList();
|
|
|
|
|
|
|
|
// first attempt to use the name as specified:
|
2016-01-15 00:28:28 +00:00
|
|
|
wxString tname = aFileName;
|
|
|
|
|
2021-09-08 21:19:49 +00:00
|
|
|
// Note: variable expansion must preferably be performed via a threadsafe wrapper for the
|
|
|
|
// getenv() system call. If we allow the wxFileName::Normalize() routine to perform expansion
|
|
|
|
// then we will have a race condition since wxWidgets does not assure a threadsafe wrapper
|
|
|
|
// for getenv().
|
2020-04-05 19:51:48 +00:00
|
|
|
tname = ExpandEnvVarSubstitutions( tname, m_project );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName tmpFN( tname );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2021-09-08 21:19:49 +00:00
|
|
|
// this case covers full paths, leading expanded vars, and paths relative to the current
|
|
|
|
// working directory (which is not necessarily the current project directory)
|
2016-06-09 05:48:49 +00:00
|
|
|
if( tmpFN.FileExists() )
|
2015-12-19 06:53:07 +00:00
|
|
|
{
|
2022-08-14 17:36:40 +00:00
|
|
|
tmpFN.Normalize( FN_NORMALIZE_FLAGS );
|
2016-06-09 05:48:49 +00:00
|
|
|
tname = tmpFN.GetFullPath();
|
2015-12-19 06:53:07 +00:00
|
|
|
|
2021-09-08 21:19:49 +00:00
|
|
|
// special case: if a path begins with ${ENV_VAR} but is not in the resolver's path list
|
|
|
|
// then add it.
|
2023-01-17 00:07:41 +00:00
|
|
|
if( aFileName.StartsWith( wxS( "${" ) ) || aFileName.StartsWith( wxS( "$(" ) ) )
|
2016-06-09 05:48:49 +00:00
|
|
|
checkEnvVarPath( aFileName );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
return tname;
|
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2021-09-08 21:19:49 +00:00
|
|
|
// if a path begins with ${ENV_VAR}/$(ENV_VAR) and is not resolved then the file either does
|
|
|
|
// not exist or the ENV_VAR is not defined
|
2018-05-21 10:09:49 +00:00
|
|
|
if( aFileName.StartsWith( "${" ) || aFileName.StartsWith( "$(" ) )
|
|
|
|
{
|
|
|
|
if( !( m_errflags & ERRFLG_ENVPATH ) )
|
|
|
|
{
|
|
|
|
m_errflags |= ERRFLG_ENVPATH;
|
|
|
|
wxString errmsg = "[3D File Resolver] No such path; ensure the environment var is defined";
|
|
|
|
errmsg.append( "\n" );
|
|
|
|
errmsg.append( tname );
|
|
|
|
errmsg.append( "\n" );
|
|
|
|
wxLogTrace( tracePathsAndFiles, errmsg );
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// at this point aFileName is:
|
|
|
|
// a. an aliased shortened name or
|
|
|
|
// b. cannot be determined
|
|
|
|
|
2016-01-15 03:01:21 +00:00
|
|
|
// check the path relative to the current project directory;
|
2021-09-08 21:19:49 +00:00
|
|
|
// NB: this is not necessarily the same as the current working directory, which has already
|
|
|
|
// been checked. This case accounts for partial paths which do not contain ${KIPRJMOD}.
|
2023-01-22 18:38:02 +00:00
|
|
|
// This check is performed before checking the path relative to ${KICAD7_3DMODEL_DIR} so that
|
|
|
|
// users can potentially override a model within ${KICAD7_3DMODEL_DIR}.
|
2021-09-08 21:19:49 +00:00
|
|
|
if( !m_paths.begin()->m_Pathexp.empty() && !tname.StartsWith( ":" ) )
|
2015-12-19 06:53:07 +00:00
|
|
|
{
|
2021-09-08 21:19:49 +00:00
|
|
|
tmpFN.Assign( m_paths.begin()->m_Pathexp, "" );
|
2016-06-09 05:48:49 +00:00
|
|
|
wxString fullPath = tmpFN.GetPathWithSep() + tname;
|
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( wxFileName::FileExists( fullPath ) )
|
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
tmpFN.Assign( fullPath );
|
2022-08-14 17:36:40 +00:00
|
|
|
tmpFN.Normalize( FN_NORMALIZE_FLAGS );
|
2016-06-09 05:48:49 +00:00
|
|
|
tname = tmpFN.GetFullPath();
|
2016-01-15 00:28:28 +00:00
|
|
|
return tname;
|
|
|
|
}
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2016-02-25 03:31:59 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2022-09-26 02:54:08 +00:00
|
|
|
// check path relative to search path
|
|
|
|
if( !aWorkingPath.IsEmpty() && !tname.StartsWith( ":" ) )
|
|
|
|
{
|
|
|
|
wxString tmp = aWorkingPath;
|
|
|
|
tmp.Append( tmpFN.GetPathSeparator() );
|
|
|
|
tmp.Append( tname );
|
|
|
|
tmpFN.Assign( tmp );
|
|
|
|
|
|
|
|
if( tmpFN.MakeAbsolute() && tmpFN.FileExists() )
|
|
|
|
{
|
|
|
|
tname = tmpFN.GetFullPath();
|
|
|
|
return tname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 18:38:02 +00:00
|
|
|
// check the partial path relative to ${KICAD7_3DMODEL_DIR} (legacy behavior)
|
2023-01-17 00:07:41 +00:00
|
|
|
if( !tname.StartsWith( wxS( ":" ) ) )
|
2016-01-18 23:30:20 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName fpath;
|
2024-01-16 22:20:45 +00:00
|
|
|
wxString fullPath( wxString::Format( wxS( "${%s}" ),
|
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) ) );
|
2016-06-09 05:48:49 +00:00
|
|
|
fullPath.Append( fpath.GetPathSeparator() );
|
|
|
|
fullPath.Append( tname );
|
2020-04-05 19:51:48 +00:00
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
|
2016-06-09 05:48:49 +00:00
|
|
|
fpath.Assign( fullPath );
|
|
|
|
|
2022-08-14 17:36:40 +00:00
|
|
|
if( fpath.Normalize( FN_NORMALIZE_FLAGS ) && fpath.FileExists() )
|
2016-01-18 23:30:20 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
tname = fpath.GetFullPath();
|
|
|
|
return tname;
|
2016-01-18 23:30:20 +00:00
|
|
|
}
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-01-18 23:30:20 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
// at this point the filename must contain an alias or else it is invalid
|
2016-01-15 00:28:28 +00:00
|
|
|
wxString alias; // the alias portion of the short filename
|
|
|
|
wxString relpath; // the path relative to the alias
|
|
|
|
|
2016-01-18 23:30:20 +00:00
|
|
|
if( !SplitAlias( tname, alias, relpath ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-19 00:05:40 +00:00
|
|
|
if( !( m_errflags & ERRFLG_RELPATH ) )
|
|
|
|
{
|
2023-01-22 18:38:02 +00:00
|
|
|
// this can happen if the file was intended to be relative to ${KICAD7_3DMODEL_DIR}
|
|
|
|
// but ${KICAD7_3DMODEL_DIR} is not set or is incorrect.
|
2016-01-19 00:05:40 +00:00
|
|
|
m_errflags |= ERRFLG_RELPATH;
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "[3D File Resolver] No such path";
|
2023-01-17 00:07:41 +00:00
|
|
|
errmsg.append( wxS( "\n" ) );
|
2016-01-19 00:05:40 +00:00
|
|
|
errmsg.append( tname );
|
2023-01-17 00:07:41 +00:00
|
|
|
errmsg.append( wxS( "\n" ) );
|
2018-05-21 10:09:49 +00:00
|
|
|
wxLogTrace( tracePathsAndFiles, errmsg );
|
2016-01-19 00:05:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
return wxEmptyString;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-09-08 21:19:49 +00:00
|
|
|
for( const SEARCH_PATH& path : m_paths )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
2021-09-08 21:19:49 +00:00
|
|
|
// ${ENV_VAR} paths have already been checked; skip them
|
2023-01-17 00:07:41 +00:00
|
|
|
if( path.m_Alias.StartsWith( wxS( "${" ) ) || path.m_Alias.StartsWith( wxS( "$(" ) ) )
|
2021-09-08 21:19:49 +00:00
|
|
|
continue;
|
|
|
|
|
2021-09-22 10:27:44 +00:00
|
|
|
if( path.m_Alias == alias && !path.m_Pathexp.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-09-08 21:19:49 +00:00
|
|
|
wxFileName fpath( wxFileName::DirName( path.m_Pathexp ) );
|
2016-01-15 00:28:28 +00:00
|
|
|
wxString fullPath = fpath.GetPathWithSep() + relpath;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath, m_project );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( wxFileName::FileExists( fullPath ) )
|
|
|
|
{
|
2021-09-21 11:33:36 +00:00
|
|
|
tname = fullPath;
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
wxFileName tmp( fullPath );
|
|
|
|
|
2022-08-14 17:36:40 +00:00
|
|
|
if( tmp.Normalize( FN_NORMALIZE_FLAGS ) )
|
2016-01-15 00:28:28 +00:00
|
|
|
tname = tmp.GetFullPath();
|
|
|
|
|
|
|
|
return tname;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 00:05:40 +00:00
|
|
|
if( !( m_errflags & ERRFLG_ALIAS ) )
|
|
|
|
{
|
|
|
|
m_errflags |= ERRFLG_ALIAS;
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "[3D File Resolver] No such path; ensure the path alias is defined";
|
2016-01-19 00:05:40 +00:00
|
|
|
errmsg.append( "\n" );
|
2016-01-23 05:09:42 +00:00
|
|
|
errmsg.append( tname.substr( 1 ) );
|
2018-05-21 10:09:49 +00:00
|
|
|
errmsg.append( "\n" );
|
|
|
|
wxLogTrace( tracePathsAndFiles, errmsg );
|
2016-01-19 00:05:40 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
bool FILENAME_RESOLVER::addPath( const SEARCH_PATH& aPath )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
if( aPath.m_Alias.empty() || aPath.m_Pathvar.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2020-01-15 23:42:16 +00:00
|
|
|
std::lock_guard<std::mutex> lock( mutex_resolver );
|
2016-04-11 03:03:39 +00:00
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
SEARCH_PATH tpath = aPath;
|
2016-01-19 21:36:26 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-11-16 11:16:44 +00:00
|
|
|
while( tpath.m_Pathvar.EndsWith( wxT( "\\" ) ) )
|
2020-11-17 17:35:27 +00:00
|
|
|
tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
|
2016-01-19 21:36:26 +00:00
|
|
|
#else
|
2020-11-16 11:16:44 +00:00
|
|
|
while( tpath.m_Pathvar.EndsWith( wxT( "/" ) ) && tpath.m_Pathvar.length() > 1 )
|
|
|
|
tpath.m_Pathvar.erase( tpath.m_Pathvar.length() - 1 );
|
2016-01-19 21:36:26 +00:00
|
|
|
#endif
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
wxFileName path( ExpandEnvVarSubstitutions( tpath.m_Pathvar, m_project ), "" );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2022-08-14 17:36:40 +00:00
|
|
|
path.Normalize( FN_NORMALIZE_FLAGS );
|
2016-01-12 06:09:04 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( !path.DirExists() )
|
2016-01-19 21:36:26 +00:00
|
|
|
{
|
2024-01-16 22:20:45 +00:00
|
|
|
wxString versionedPath = wxString::Format( wxS( "${%s}" ),
|
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
|
|
|
|
|
|
|
|
if( aPath.m_Pathvar == versionedPath
|
2023-01-17 00:07:41 +00:00
|
|
|
|| aPath.m_Pathvar == wxS( "${KIPRJMOD}" ) || aPath.m_Pathvar == wxS( "$(KIPRJMOD)" )
|
|
|
|
|| aPath.m_Pathvar == wxS( "${KISYS3DMOD}" ) || aPath.m_Pathvar == wxS( "$(KISYS3DMOD)" ) )
|
2021-09-22 10:27:44 +00:00
|
|
|
{
|
|
|
|
// suppress the message if the missing pathvar is a system variable
|
|
|
|
}
|
|
|
|
else
|
2016-02-03 00:44:48 +00:00
|
|
|
{
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString msg = _( "The given path does not exist" );
|
2016-02-03 00:44:48 +00:00
|
|
|
msg.append( wxT( "\n" ) );
|
2020-11-16 11:16:44 +00:00
|
|
|
msg.append( tpath.m_Pathvar );
|
2023-01-19 00:18:11 +00:00
|
|
|
DisplayErrorMessage( nullptr, msg );
|
2016-02-03 00:44:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
tpath.m_Pathexp.clear();
|
2016-01-19 21:36:26 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
else
|
2016-01-19 21:36:26 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
tpath.m_Pathexp = path.GetFullPath();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
while( tpath.m_Pathexp.EndsWith( wxT( "\\" ) ) )
|
|
|
|
tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
|
|
|
|
#else
|
|
|
|
while( tpath.m_Pathexp.EndsWith( wxT( "/" ) ) && tpath.m_Pathexp.length() > 1 )
|
|
|
|
tpath.m_Pathexp.erase( tpath.m_Pathexp.length() - 1 );
|
|
|
|
#endif
|
2016-01-19 21:36:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
std::list< SEARCH_PATH >::iterator sPL = m_paths.begin();
|
|
|
|
std::list< SEARCH_PATH >::iterator ePL = m_paths.end();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( sPL != ePL )
|
|
|
|
{
|
2021-09-22 10:27:44 +00:00
|
|
|
if( tpath.m_Alias == sPL->m_Alias )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
2023-01-19 11:01:22 +00:00
|
|
|
wxString msg = _( "Alias: " );
|
2020-11-16 11:16:44 +00:00
|
|
|
msg.append( tpath.m_Alias );
|
2016-01-15 00:28:28 +00:00
|
|
|
msg.append( wxT( "\n" ) );
|
2020-11-10 20:31:52 +00:00
|
|
|
msg.append( _( "This path:" ) + wxS( " " ) );
|
2020-11-16 11:16:44 +00:00
|
|
|
msg.append( tpath.m_Pathvar );
|
2016-01-15 00:28:28 +00:00
|
|
|
msg.append( wxT( "\n" ) );
|
2020-11-10 20:31:52 +00:00
|
|
|
msg.append( _( "Existing path:" ) + wxS( " " ) );
|
2020-11-16 11:16:44 +00:00
|
|
|
msg.append( sPL->m_Pathvar );
|
2023-01-19 00:53:16 +00:00
|
|
|
DisplayErrorMessage( nullptr, _( "Bad alias (duplicate name)" ), msg );
|
2016-01-15 00:28:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
m_paths.push_back( tpath );
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
void FILENAME_RESOLVER::checkEnvVarPath( const wxString& aPath )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
bool useParen = false;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2023-01-17 00:07:41 +00:00
|
|
|
if( aPath.StartsWith( wxS( "$(" ) ) )
|
2016-06-09 05:48:49 +00:00
|
|
|
useParen = true;
|
2023-01-17 00:07:41 +00:00
|
|
|
else if( !aPath.StartsWith( wxS( "${" ) ) )
|
2016-06-09 05:48:49 +00:00
|
|
|
return;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
size_t pEnd;
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( useParen )
|
2023-01-17 00:07:41 +00:00
|
|
|
pEnd = aPath.find( wxS( ")" ) );
|
2016-06-09 05:48:49 +00:00
|
|
|
else
|
2023-01-17 00:07:41 +00:00
|
|
|
pEnd = aPath.find( wxS( "}" ) );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
|
|
|
if( pEnd == wxString::npos )
|
|
|
|
return;
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxString envar = aPath.substr( 0, pEnd + 1 );
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// check if the alias exists; if not then add it to the end of the
|
|
|
|
// env var section of the path list
|
2020-11-16 11:16:44 +00:00
|
|
|
auto sPL = m_paths.begin();
|
|
|
|
auto ePL = m_paths.end();
|
2016-04-17 22:35:32 +00:00
|
|
|
|
|
|
|
while( sPL != ePL )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
if( sPL->m_Alias == envar )
|
2016-06-09 05:48:49 +00:00
|
|
|
return;
|
|
|
|
|
2023-01-17 00:07:41 +00:00
|
|
|
if( !sPL->m_Alias.StartsWith( wxS( "${" ) ) )
|
2016-06-09 05:48:49 +00:00
|
|
|
break;
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
++sPL;
|
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
SEARCH_PATH lpath;
|
2020-11-16 11:16:44 +00:00
|
|
|
lpath.m_Alias = envar;
|
|
|
|
lpath.m_Pathvar = lpath.m_Alias;
|
|
|
|
wxFileName tmpFN( ExpandEnvVarSubstitutions( lpath.m_Alias, m_project ), "" );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxUniChar psep = tmpFN.GetPathSeparator();
|
2022-08-14 17:36:40 +00:00
|
|
|
tmpFN.Normalize( FN_NORMALIZE_FLAGS );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( !tmpFN.DirExists() )
|
|
|
|
return;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
lpath.m_Pathexp = tmpFN.GetFullPath();
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( !lpath.m_Pathexp.empty() && psep == *lpath.m_Pathexp.rbegin() )
|
|
|
|
lpath.m_Pathexp.erase( --lpath.m_Pathexp.end() );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( lpath.m_Pathexp.empty() )
|
2016-06-09 05:48:49 +00:00
|
|
|
return;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
m_paths.insert( sPL, lpath );
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
wxString FILENAME_RESOLVER::ShortenPath( const wxString& aFullPathName )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
|
|
|
wxString fname = aFullPathName;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
if( m_paths.empty() )
|
2016-04-17 22:35:32 +00:00
|
|
|
createPathList();
|
|
|
|
|
2020-01-15 23:42:16 +00:00
|
|
|
std::lock_guard<std::mutex> lock( mutex_resolver );
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
std::list< SEARCH_PATH >::const_iterator sL = m_paths.begin();
|
2016-04-17 22:35:32 +00:00
|
|
|
size_t idx;
|
|
|
|
|
2020-11-16 11:16:44 +00:00
|
|
|
while( sL != m_paths.end() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
// undefined paths do not participate in the
|
|
|
|
// file name shortening procedure
|
2020-11-16 11:16:44 +00:00
|
|
|
if( sL->m_Pathexp.empty() )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
|
|
|
++sL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:46:00 +00:00
|
|
|
wxFileName fpath;
|
|
|
|
|
|
|
|
// in the case of aliases, ensure that we use the most recent definition
|
2023-01-17 00:07:41 +00:00
|
|
|
if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
|
2016-06-12 22:46:00 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
wxString tpath = ExpandEnvVarSubstitutions( sL->m_Alias, m_project );
|
2016-06-12 22:46:00 +00:00
|
|
|
|
|
|
|
if( tpath.empty() )
|
|
|
|
{
|
|
|
|
++sL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpath.Assign( tpath, wxT( "" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
fpath.Assign( sL->m_Pathexp, wxT( "" ) );
|
2016-06-12 22:46:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
wxString fps = fpath.GetPathWithSep();
|
2016-01-15 00:28:28 +00:00
|
|
|
wxString tname;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
idx = fname.find( fps );
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
if( idx == 0 )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
fname = fname.substr( fps.size() );
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// ensure only the '/' separator is used in the internal name
|
2015-12-08 07:31:57 +00:00
|
|
|
fname.Replace( wxT( "\\" ), wxT( "/" ) );
|
2016-01-15 00:28:28 +00:00
|
|
|
#endif
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2023-01-17 00:07:41 +00:00
|
|
|
if( sL->m_Alias.StartsWith( wxS( "${" ) ) || sL->m_Alias.StartsWith( wxS( "$(" ) ) )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
|
|
|
// old style ENV_VAR
|
2020-11-16 11:16:44 +00:00
|
|
|
tname = sL->m_Alias;
|
2023-01-17 00:07:41 +00:00
|
|
|
tname.Append( wxS( "/" ) );
|
2016-04-17 22:35:32 +00:00
|
|
|
tname.append( fname );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// new style alias
|
2023-09-03 20:52:29 +00:00
|
|
|
tname = "${";
|
2020-11-16 11:16:44 +00:00
|
|
|
tname.append( sL->m_Alias );
|
2023-09-03 20:52:29 +00:00
|
|
|
tname.append( wxS( "}/" ) );
|
2016-04-17 22:35:32 +00:00
|
|
|
tname.append( fname );
|
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return tname;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++sL;
|
|
|
|
}
|
|
|
|
|
2016-01-11 05:24:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// it is strange to convert an MSWin full path to use the
|
|
|
|
// UNIX separator but this is done for consistency and can
|
|
|
|
// be helpful even when transferring project files from
|
|
|
|
// MSWin to *NIX.
|
|
|
|
fname.Replace( wxT( "\\" ), wxT( "/" ) );
|
|
|
|
#endif
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
const std::list< SEARCH_PATH >* FILENAME_RESOLVER::GetPaths() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
return &m_paths;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
bool FILENAME_RESOLVER::SplitAlias( const wxString& aFileName,
|
2021-03-06 09:27:41 +00:00
|
|
|
wxString& anAlias, wxString& aRelPath ) const
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
|
|
|
anAlias.clear();
|
|
|
|
aRelPath.clear();
|
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
size_t searchStart = 0;
|
|
|
|
|
|
|
|
if( aFileName.StartsWith( wxT( ":" ) ) )
|
|
|
|
searchStart = 1;
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
size_t tagpos = aFileName.find( wxT( ":" ), searchStart );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
if( tagpos == wxString::npos || tagpos == searchStart )
|
2016-01-15 00:28:28 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( tagpos + 1 >= aFileName.length() )
|
|
|
|
return false;
|
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
anAlias = aFileName.substr( searchStart, tagpos - searchStart );
|
2016-01-15 00:28:28 +00:00
|
|
|
aRelPath = aFileName.substr( tagpos + 1 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
bool FILENAME_RESOLVER::ValidateFileName( const wxString& aFileName, bool& hasAlias ) const
|
2016-01-23 03:52:54 +00:00
|
|
|
{
|
|
|
|
// Rules:
|
|
|
|
// 1. The generic form of an aliased 3D relative path is:
|
|
|
|
// ALIAS:relative/path
|
|
|
|
// 2. ALIAS is a UTF string excluding wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" )
|
|
|
|
// 3. The relative path must be a valid relative path for the platform
|
|
|
|
hasAlias = false;
|
|
|
|
|
|
|
|
if( aFileName.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxString filename = aFileName;
|
2021-09-06 13:24:23 +00:00
|
|
|
wxString lpath;
|
2021-09-06 14:01:55 +00:00
|
|
|
size_t aliasStart = aFileName.StartsWith( ':' ) ? 1 : 0;
|
2021-09-06 13:24:23 +00:00
|
|
|
size_t aliasEnd = aFileName.find( ':', aliasStart );
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
// ensure that the file separators suit the current platform
|
2021-09-06 13:24:23 +00:00
|
|
|
#ifdef __WINDOWS__
|
2016-01-23 03:52:54 +00:00
|
|
|
filename.Replace( wxT( "/" ), wxT( "\\" ) );
|
|
|
|
|
|
|
|
// if we see the :\ pattern then it must be a drive designator
|
2021-09-06 13:24:23 +00:00
|
|
|
if( aliasEnd != wxString::npos )
|
2016-01-23 03:52:54 +00:00
|
|
|
{
|
2017-05-28 16:56:49 +00:00
|
|
|
size_t pos1 = filename.find( wxT( ":\\" ) );
|
2016-01-23 03:52:54 +00:00
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
if( pos1 != wxString::npos && ( pos1 != aliasEnd || pos1 != 1 ) )
|
2016-01-23 03:52:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// if we have a drive designator then we have no alias
|
|
|
|
if( pos1 != wxString::npos )
|
2021-09-06 13:24:23 +00:00
|
|
|
aliasEnd = wxString::npos;
|
2016-01-23 03:52:54 +00:00
|
|
|
}
|
2021-09-06 13:24:23 +00:00
|
|
|
#else
|
2016-01-23 03:52:54 +00:00
|
|
|
filename.Replace( wxT( "\\" ), wxT( "/" ) );
|
2021-09-06 13:24:23 +00:00
|
|
|
#endif
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
// names may not end with ':'
|
2021-09-06 13:24:23 +00:00
|
|
|
if( aliasEnd == aFileName.length() -1 )
|
2016-01-23 03:52:54 +00:00
|
|
|
return false;
|
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
if( aliasEnd != wxString::npos )
|
2016-01-23 03:52:54 +00:00
|
|
|
{
|
|
|
|
// ensure the alias component is not empty
|
2021-09-06 13:24:23 +00:00
|
|
|
if( aliasEnd == aliasStart )
|
2016-01-23 03:52:54 +00:00
|
|
|
return false;
|
|
|
|
|
2021-09-06 13:24:23 +00:00
|
|
|
lpath = filename.substr( aliasStart, aliasEnd );
|
2016-01-23 03:52:54 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// check the alias for restricted characters
|
2016-01-23 03:52:54 +00:00
|
|
|
if( wxString::npos != lpath.find_first_of( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
hasAlias = true;
|
2021-09-06 13:24:23 +00:00
|
|
|
lpath = aFileName.substr( aliasEnd + 1 );
|
2016-01-23 03:52:54 +00:00
|
|
|
}
|
2021-09-06 13:24:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lpath = aFileName;
|
|
|
|
|
|
|
|
// in the case of ${ENV_VAR}|$(ENV_VAR)/path, strip the
|
|
|
|
// environment string before testing
|
|
|
|
aliasEnd = wxString::npos;
|
|
|
|
|
2023-01-17 00:07:41 +00:00
|
|
|
if( aFileName.StartsWith( wxS( "${" ) ) )
|
2021-09-06 13:24:23 +00:00
|
|
|
aliasEnd = aFileName.find( '}' );
|
2023-01-17 00:07:41 +00:00
|
|
|
else if( aFileName.StartsWith( wxS( "$(" ) ) )
|
2021-09-06 13:24:23 +00:00
|
|
|
aliasEnd = aFileName.find( ')' );
|
|
|
|
|
|
|
|
if( aliasEnd != wxString::npos )
|
|
|
|
lpath = aFileName.substr( aliasEnd + 1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-18 16:17:54 +00:00
|
|
|
// Test for forbidden chars in filenames. Should be wxFileName::GetForbiddenChars()
|
|
|
|
// On MSW, the list returned by wxFileName::GetForbiddenChars() contains separators
|
2021-09-19 06:55:37 +00:00
|
|
|
// '\'and '/' used here because lpath can be a full path.
|
2021-09-18 16:17:54 +00:00
|
|
|
// So remove separators
|
|
|
|
wxString lpath_no_sep = lpath;
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
lpath_no_sep.Replace( "/", " " );
|
|
|
|
lpath_no_sep.Replace( "\\", " " );
|
2021-09-19 06:55:37 +00:00
|
|
|
|
|
|
|
// A disk identifier is allowed, and therefore remove its separator
|
|
|
|
if( lpath_no_sep.Length() > 1 && lpath_no_sep[1] == ':' )
|
|
|
|
lpath_no_sep[1] = ' ';
|
2021-09-18 16:17:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( wxString::npos != lpath_no_sep.find_first_of( wxFileName::GetForbiddenChars() ) )
|
2021-09-06 13:24:23 +00:00
|
|
|
return false;
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-12 22:46:00 +00:00
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
bool FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths ) const
|
2016-06-12 22:46:00 +00:00
|
|
|
{
|
|
|
|
paths.clear();
|
|
|
|
|
|
|
|
if( !m_pgm )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool hasKisys3D = false;
|
|
|
|
|
|
|
|
|
|
|
|
// iterate over the list of internally defined ENV VARs
|
|
|
|
// and add them to the paths list
|
|
|
|
ENV_VAR_MAP_CITER mS = m_pgm->GetLocalEnvVariables().begin();
|
|
|
|
ENV_VAR_MAP_CITER mE = m_pgm->GetLocalEnvVariables().end();
|
|
|
|
|
|
|
|
while( mS != mE )
|
|
|
|
{
|
|
|
|
// filter out URLs, template directories, and known system paths
|
2022-09-26 01:08:51 +00:00
|
|
|
if( mS->first == wxS( "KICAD_PTEMPLATES" )
|
2023-01-22 18:38:02 +00:00
|
|
|
|| mS->first.Matches( wxS( "KICAD*_FOOTPRINT_DIR") ) )
|
2016-06-12 22:46:00 +00:00
|
|
|
{
|
|
|
|
++mS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:08:51 +00:00
|
|
|
if( wxString::npos != mS->second.GetValue().find( wxS( "://" ) ) )
|
2016-06-12 22:46:00 +00:00
|
|
|
{
|
|
|
|
++mS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-26 01:08:51 +00:00
|
|
|
//also add the path without the ${} to act as legacy alias support for older files
|
|
|
|
paths.push_back( mS->first );
|
2016-06-12 22:46:00 +00:00
|
|
|
|
2023-01-22 18:38:02 +00:00
|
|
|
if( mS->first.Matches( wxS("KICAD*_3DMODEL_DIR") ) )
|
2016-06-12 22:46:00 +00:00
|
|
|
hasKisys3D = true;
|
|
|
|
|
|
|
|
++mS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !hasKisys3D )
|
2024-01-16 22:20:45 +00:00
|
|
|
paths.emplace_back( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
|
2016-06-12 22:46:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|