2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-06-09 05:48:49 +00:00
|
|
|
* Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
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
|
|
|
|
*/
|
|
|
|
|
2016-02-23 03:33:24 +00:00
|
|
|
#include <sstream>
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2016-01-15 00:28:28 +00:00
|
|
|
#include <sstream>
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <wx/filename.h>
|
2016-02-23 03:33:24 +00:00
|
|
|
#include <wx/log.h>
|
2016-01-15 00:28:28 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2016-04-17 22:35:32 +00:00
|
|
|
#include <pgm_base.h>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
#include "common.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
#include "3d_filename_resolver.h"
|
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
// configuration file version
|
|
|
|
#define CFGFILE_VERSION 1
|
2015-12-08 07:31:57 +00:00
|
|
|
#define S3D_RESOLVER_CONFIG wxT( "3Dresolver.cfg" )
|
|
|
|
|
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"
|
|
|
|
|
2016-04-11 03:03:39 +00:00
|
|
|
static wxCriticalSection lock3D_resolver;
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
static bool getHollerith( const std::string& aString, size_t& aIndex, wxString& aResult );
|
|
|
|
|
|
|
|
|
|
|
|
S3D_FILENAME_RESOLVER::S3D_FILENAME_RESOLVER()
|
|
|
|
{
|
|
|
|
m_errflags = 0;
|
2016-04-17 22:35:32 +00:00
|
|
|
m_pgm = NULL;
|
2016-01-15 00:28:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::Set3DConfigDir( const wxString& aConfigDir )
|
|
|
|
{
|
|
|
|
if( aConfigDir.empty() )
|
|
|
|
return false;
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName cfgdir;
|
|
|
|
|
|
|
|
if( aConfigDir.StartsWith( "${" ) || aConfigDir.StartsWith( "$(" ) )
|
|
|
|
cfgdir.Assign( ExpandEnvVarSubstitutions( aConfigDir ), "" );
|
|
|
|
else
|
|
|
|
cfgdir.Assign( aConfigDir, "" );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
cfgdir.Normalize();
|
|
|
|
|
|
|
|
if( false == cfgdir.DirExists() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_ConfigDir = cfgdir.GetPath();
|
|
|
|
createPathList();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::SetProjectDir( const wxString& aProjDir, bool* flgChanged )
|
|
|
|
{
|
|
|
|
if( aProjDir.empty() )
|
|
|
|
return false;
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName projdir;
|
|
|
|
|
|
|
|
if( aProjDir.StartsWith( "${" ) || aProjDir.StartsWith( "$(" ) )
|
|
|
|
projdir.Assign( ExpandEnvVarSubstitutions( aProjDir ), "" );
|
|
|
|
else
|
|
|
|
projdir.Assign( aProjDir, "" );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
projdir.Normalize();
|
|
|
|
|
|
|
|
if( false == projdir.DirExists() )
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( m_Paths.empty() )
|
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
S3D_ALIAS al;
|
2016-06-09 05:48:49 +00:00
|
|
|
al.m_alias = "${KIPRJMOD}";
|
|
|
|
al.m_pathvar = "${KIPRJMOD}";
|
2016-04-17 22:35:32 +00:00
|
|
|
al.m_pathexp = m_curProjDir;
|
2016-01-15 00:28:28 +00:00
|
|
|
m_Paths.push_back( al );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( flgChanged )
|
|
|
|
*flgChanged = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
if( m_Paths.front().m_pathexp.Cmp( m_curProjDir ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-04-17 22:35:32 +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
|
2016-02-23 03:33:24 +00:00
|
|
|
do {
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
|
|
|
ostr << " * [INFO] changed project dir to ";
|
|
|
|
ostr << m_Paths.front().m_pathexp.ToUTF8();
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
|
|
|
} while( 0 );
|
2015-12-08 07:31:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_FILENAME_RESOLVER::GetProjectDir( void )
|
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
return m_curProjDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S3D_FILENAME_RESOLVER::SetProgramBase( PGM_BASE* aBase )
|
|
|
|
{
|
|
|
|
m_pgm = aBase;
|
|
|
|
|
|
|
|
if( NULL == m_pgm || m_Paths.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// recreate the path list
|
|
|
|
m_Paths.clear();
|
|
|
|
createPathList();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
return;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::createPathList( void )
|
|
|
|
{
|
|
|
|
if( !m_Paths.empty() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
wxString kmod;
|
|
|
|
|
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()
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
S3D_ALIAS lpath;
|
2016-04-21 07:19:08 +00:00
|
|
|
lpath.m_alias = "${KIPRJMOD}";
|
|
|
|
lpath.m_pathvar = "${KIPRJMOD}";
|
2016-04-17 22:35:32 +00:00
|
|
|
lpath.m_pathexp = m_curProjDir;
|
2016-01-15 00:28:28 +00:00
|
|
|
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
|
|
|
{
|
2016-06-12 22:46:00 +00:00
|
|
|
for( auto i : epaths )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
2016-06-12 22:46:00 +00:00
|
|
|
wxString pathVal = ExpandEnvVarSubstitutions( i );
|
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
|
|
|
{
|
2016-06-12 22:46:00 +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, "" );
|
|
|
|
fndummy.Normalize();
|
|
|
|
lpath.m_pathexp = fndummy.GetFullPath();
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-12 22:46:00 +00:00
|
|
|
lpath.m_alias = i;
|
|
|
|
lpath.m_pathvar = i;
|
2016-04-17 22:35:32 +00:00
|
|
|
|
|
|
|
if( !lpath.m_pathexp.empty() && psep == *lpath.m_pathexp.rbegin() )
|
|
|
|
lpath.m_pathexp.erase( --lpath.m_pathexp.end() );
|
|
|
|
|
|
|
|
m_Paths.push_back( lpath );
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
if( !m_ConfigDir.empty() )
|
|
|
|
readPathList();
|
|
|
|
|
|
|
|
if( m_Paths.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-02-23 03:33:24 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, " * [3D model] search paths:\n" );
|
2016-01-15 00:28:28 +00:00
|
|
|
std::list< S3D_ALIAS >::const_iterator sPL = m_Paths.begin();
|
|
|
|
std::list< S3D_ALIAS >::const_iterator ePL = m_Paths.end();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( sPL != ePL )
|
|
|
|
{
|
2016-08-19 07:45:59 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, " + %s : '%s'\n", (*sPL).m_alias.GetData(),
|
|
|
|
(*sPL).m_pathexp.GetData() );
|
2015-12-08 07:31:57 +00:00
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
bool S3D_FILENAME_RESOLVER::UpdatePathList( std::vector< S3D_ALIAS >& aPathList )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
wxUniChar envMarker( '$' );
|
|
|
|
|
|
|
|
while( !m_Paths.empty() && envMarker != *m_Paths.back().m_alias.rbegin() )
|
2015-12-08 07:31:57 +00:00
|
|
|
m_Paths.pop_back();
|
|
|
|
|
|
|
|
size_t nI = aPathList.size();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < nI; ++i )
|
|
|
|
addPath( aPathList[i] );
|
|
|
|
|
|
|
|
return writePathList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_FILENAME_RESOLVER::ResolvePath( const wxString& aFileName )
|
|
|
|
{
|
2016-04-11 03:03:39 +00:00
|
|
|
wxCriticalSectionLocker lock( lock3D_resolver );
|
2016-06-09 05:48:49 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
if( aFileName.empty() )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
if( m_Paths.empty() )
|
|
|
|
createPathList();
|
|
|
|
|
|
|
|
// first attempt to use the name as specified:
|
2016-01-15 00:28:28 +00:00
|
|
|
wxString tname = aFileName;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// translate from KiCad's internal UNIX-like path to MSWin paths
|
|
|
|
tname.Replace( wxT( "/" ), wxT( "\\" ) );
|
|
|
|
#endif
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// Note: variable expansion must be performed using 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().
|
2016-06-12 22:46:00 +00:00
|
|
|
if( tname.StartsWith( "${" ) || tname.StartsWith( "$(" ) )
|
2016-06-09 05:48:49 +00:00
|
|
|
tname = ExpandEnvVarSubstitutions( tname );
|
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
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// in the case of absolute filenames we don't store a map item
|
|
|
|
if( !aFileName.StartsWith( "${" ) && !aFileName.StartsWith( "$(" )
|
|
|
|
&& !aFileName.StartsWith( ":" ) && tmpFN.IsAbsolute() )
|
|
|
|
{
|
|
|
|
tmpFN.Normalize();
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( tmpFN.FileExists() )
|
|
|
|
return tmpFN.GetFullPath();
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
return wxEmptyString;
|
|
|
|
}
|
2015-12-19 06:53:07 +00:00
|
|
|
|
2016-06-09 05:48: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)
|
|
|
|
if( tmpFN.FileExists() )
|
2015-12-19 06:53:07 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
tmpFN.Normalize();
|
|
|
|
tname = tmpFN.GetFullPath();
|
2015-12-19 06:53:07 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// special case: if a path begins with ${ENV_VAR} but is not in the
|
2016-06-12 22:46:00 +00:00
|
|
|
// resolver's path list then add it.
|
2016-06-09 05:48:49 +00:00
|
|
|
if( aFileName.StartsWith( "${" ) || aFileName.StartsWith( "$(" ) )
|
|
|
|
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
|
|
|
|
2016-06-09 05:48: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
|
|
|
|
if( aFileName.StartsWith( "${" ) || aFileName.StartsWith( "$(" ) )
|
|
|
|
{
|
2016-01-19 00:05:40 +00:00
|
|
|
if( !( m_errflags & ERRFLG_ENVPATH ) )
|
|
|
|
{
|
|
|
|
m_errflags |= ERRFLG_ENVPATH;
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "[3D File Resolver] No such path; ensure the environment var is defined";
|
2016-01-19 00:05:40 +00:00
|
|
|
errmsg.append( "\n" );
|
|
|
|
errmsg.append( tname );
|
2016-08-19 07:45:59 +00:00
|
|
|
wxLogMessage( errmsg );
|
2016-01-19 00:05:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
return wxEmptyString;
|
2015-12-19 06:53:07 +00:00
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
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 00:28:28 +00:00
|
|
|
std::list< S3D_ALIAS >::const_iterator sPL = m_Paths.begin();
|
|
|
|
std::list< S3D_ALIAS >::const_iterator ePL = m_Paths.end();
|
|
|
|
|
2016-01-15 03:01:21 +00:00
|
|
|
// check the path relative to the current project directory;
|
|
|
|
// note: this is not necessarily the same as the current working
|
2016-04-17 22:35:32 +00:00
|
|
|
// directory, which has already been checked. This case accounts
|
|
|
|
// for partial paths which do not contain ${KIPRJMOD}.
|
2016-06-09 05:48:49 +00:00
|
|
|
// This check is performed before checking the path relative to
|
|
|
|
// ${KISYS3DMOD} so that users can potentially override a model
|
|
|
|
// within ${KISYS3DMOD}
|
|
|
|
if( !sPL->m_pathexp.empty() && !tname.StartsWith( ":" ) )
|
2015-12-19 06:53:07 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
tmpFN.Assign( sPL->m_pathexp, "" );
|
|
|
|
wxString fullPath = tmpFN.GetPathWithSep() + tname;
|
|
|
|
|
|
|
|
if( fullPath.StartsWith( "${" ) || fullPath.StartsWith( "$(" ) )
|
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath );
|
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 );
|
|
|
|
tmpFN.Normalize();
|
|
|
|
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
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
// check the partial path relative to ${KISYS3DMOD} (legacy behavior)
|
|
|
|
if( !tname.StartsWith( ":" ) )
|
2016-01-18 23:30:20 +00:00
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName fpath;
|
|
|
|
wxString fullPath( "${KISYS3DMOD}" );
|
|
|
|
fullPath.Append( fpath.GetPathSeparator() );
|
|
|
|
fullPath.Append( tname );
|
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath );
|
|
|
|
fpath.Assign( fullPath );
|
|
|
|
|
|
|
|
if( fpath.Normalize() && 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-06-09 05:48:49 +00:00
|
|
|
// ${ENV_VAR} paths have already been checked; skip them
|
|
|
|
while( sPL != ePL && ( sPL->m_alias.StartsWith( "${" )
|
|
|
|
|| sPL->m_alias.StartsWith( "$(" ) ) )
|
|
|
|
++sPL;
|
|
|
|
|
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 ) )
|
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
// this can happen if the file was intended to be relative to
|
|
|
|
// ${KISYS3DMOD} but ${KISYS3DMOD} not set or 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";
|
2016-01-19 00:05:40 +00:00
|
|
|
errmsg.append( "\n" );
|
|
|
|
errmsg.append( tname );
|
2016-08-19 07:45:59 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, 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
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
while( sPL != ePL )
|
|
|
|
{
|
2016-02-25 03:31:59 +00:00
|
|
|
if( !sPL->m_alias.Cmp( alias ) && !sPL->m_pathexp.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
wxFileName fpath( wxFileName::DirName( sPL->m_pathexp ) );
|
|
|
|
wxString fullPath = fpath.GetPathWithSep() + relpath;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( fullPath.StartsWith( "${") || fullPath.StartsWith( "$(" ) )
|
|
|
|
fullPath = ExpandEnvVarSubstitutions( fullPath );
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( wxFileName::FileExists( fullPath ) )
|
|
|
|
{
|
|
|
|
wxFileName tmp( fullPath );
|
|
|
|
|
|
|
|
if( tmp.Normalize() )
|
|
|
|
tname = tmp.GetFullPath();
|
|
|
|
|
|
|
|
return tname;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
|
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 ) );
|
2016-08-19 07:45:59 +00:00
|
|
|
wxLogTrace( MASK_3D_RESOLVER, errmsg );
|
2016-01-19 00:05:40 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
bool S3D_FILENAME_RESOLVER::addPath( const S3D_ALIAS& aPath )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
if( aPath.m_alias.empty() || aPath.m_pathvar.empty() )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2016-04-11 03:03:39 +00:00
|
|
|
wxCriticalSectionLocker lock( lock3D_resolver );
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
S3D_ALIAS tpath = aPath;
|
2016-01-19 21:36:26 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
while( tpath.m_pathvar.EndsWith( wxT( "\\" ) ) )
|
|
|
|
tpath.m_pathvar.erase( tpath.m_pathvar.length() - 1 );
|
|
|
|
#else
|
|
|
|
while( tpath.m_pathvar.EndsWith( wxT( "/" ) ) && tpath.m_pathvar.length() > 1 )
|
|
|
|
tpath.m_pathvar.erase( tpath.m_pathvar.length() - 1 );
|
|
|
|
#endif
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxFileName path;
|
|
|
|
|
|
|
|
if( tpath.m_pathvar.StartsWith( "${" ) || tpath.m_pathvar.StartsWith( "$(" ) )
|
|
|
|
path.Assign( ExpandEnvVarSubstitutions( tpath.m_pathvar ), "" );
|
|
|
|
else
|
|
|
|
path.Assign( tpath.m_pathvar, "" );
|
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
path.Normalize();
|
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
|
|
|
{
|
2016-02-03 00:44:48 +00:00
|
|
|
// suppress the message if the missing pathvar is the
|
|
|
|
// legacy KISYS3DMOD variable
|
|
|
|
if( aPath.m_pathvar.compare( wxT( "${KISYS3DMOD}" ) ) )
|
|
|
|
{
|
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" ) );
|
|
|
|
msg.append( tpath.m_pathvar );
|
2016-04-21 07:19:08 +00:00
|
|
|
wxMessageBox( msg, _( "3D model search path" ) );
|
2016-02-03 00:44:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 00:28:28 +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
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
tpath.m_pathexp = path.GetFullPath();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
wxString pname = path.GetPath();
|
2016-01-19 21:36:26 +00:00
|
|
|
std::list< S3D_ALIAS >::iterator sPL = m_Paths.begin();
|
|
|
|
std::list< S3D_ALIAS >::iterator ePL = m_Paths.end();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( sPL != ePL )
|
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
if( !tpath.m_alias.Cmp( sPL->m_alias ) )
|
|
|
|
{
|
2016-01-19 00:05:40 +00:00
|
|
|
wxString msg = _( "Alias: " );
|
2016-01-15 00:28:28 +00:00
|
|
|
msg.append( tpath.m_alias );
|
|
|
|
msg.append( wxT( "\n" ) );
|
2016-01-19 00:05:40 +00:00
|
|
|
msg.append( _( "This path: " ) );
|
2016-01-15 00:28:28 +00:00
|
|
|
msg.append( tpath.m_pathvar );
|
|
|
|
msg.append( wxT( "\n" ) );
|
2016-01-19 00:05:40 +00:00
|
|
|
msg.append( _( "Existing path: " ) );
|
2016-01-15 00:28:28 +00:00
|
|
|
msg.append( sPL->m_pathvar );
|
2016-01-19 00:05:40 +00:00
|
|
|
wxMessageBox( msg, _( "Bad alias (duplicate name)" ) );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
m_Paths.push_back( tpath );
|
2015-12-08 07:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::readPathList( void )
|
|
|
|
{
|
|
|
|
if( m_ConfigDir.empty() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "3D configuration directory is unknown";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8();
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFileName cfgpath( m_ConfigDir, S3D_RESOLVER_CONFIG );
|
|
|
|
cfgpath.Normalize();
|
|
|
|
wxString cfgname = cfgpath.GetFullPath();
|
|
|
|
|
|
|
|
size_t nitems = m_Paths.size();
|
|
|
|
|
|
|
|
std::ifstream cfgFile;
|
|
|
|
std::string cfgLine;
|
|
|
|
|
|
|
|
if( !wxFileName::Exists( cfgname ) )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "no 3D configuration file";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << " '";
|
|
|
|
ostr << cfgname.ToUTF8() << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgFile.open( cfgname.ToUTF8() );
|
|
|
|
|
|
|
|
if( !cfgFile.is_open() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "Could not open configuration file";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lineno = 0;
|
2016-01-15 00:28:28 +00:00
|
|
|
S3D_ALIAS al;
|
|
|
|
size_t idx;
|
2016-01-19 21:36:26 +00:00
|
|
|
int vnum = 0; // version number
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( cfgFile.good() )
|
|
|
|
{
|
|
|
|
cfgLine.clear();
|
|
|
|
std::getline( cfgFile, cfgLine );
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
if( cfgLine.empty() )
|
|
|
|
{
|
|
|
|
if( cfgFile.eof() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-05 10:32:22 +00:00
|
|
|
if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
|
2016-01-19 21:36:26 +00:00
|
|
|
{
|
|
|
|
// extract the version number and parse accordingly
|
|
|
|
if( cfgLine.size() > 2 )
|
|
|
|
{
|
|
|
|
std::istringstream istr;
|
|
|
|
istr.str( cfgLine.substr( 2 ) );
|
|
|
|
istr >> vnum;
|
|
|
|
}
|
2016-01-19 22:07:09 +00:00
|
|
|
|
|
|
|
continue;
|
2016-01-19 21:36:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
idx = 0;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( !getHollerith( cfgLine, idx, al.m_alias ) )
|
|
|
|
continue;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-18 08:53:35 +00:00
|
|
|
// never add on KISYS3DMOD from a config file
|
|
|
|
if( !al.m_alias.Cmp( wxT( "KISYS3DMOD" ) ) )
|
|
|
|
continue;
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( !getHollerith( cfgLine, idx, al.m_pathvar ) )
|
|
|
|
continue;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
if( !getHollerith( cfgLine, idx, al.m_description ) )
|
|
|
|
continue;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
addPath( al );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfgFile.close();
|
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
if( vnum < CFGFILE_VERSION )
|
|
|
|
writePathList();
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
if( m_Paths.size() != nitems )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::writePathList( void )
|
|
|
|
{
|
|
|
|
if( m_ConfigDir.empty() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-01-12 06:09:04 +00:00
|
|
|
wxString errmsg = _( "3D configuration directory is unknown" );
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8();
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-19 00:05:40 +00:00
|
|
|
wxMessageBox( errmsg, _( "Write 3D search path list" ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return false;
|
2016-01-18 08:53:35 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
// skip all ${ENV_VAR} alias names
|
|
|
|
std::list< S3D_ALIAS >::const_iterator sPL = m_Paths.begin();
|
|
|
|
std::list< S3D_ALIAS >::const_iterator ePL = m_Paths.end();
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
while( sPL != ePL && ( sPL->m_alias.StartsWith( "${" )
|
|
|
|
|| sPL->m_alias.StartsWith( "$(" ) ) )
|
2016-04-17 22:35:32 +00:00
|
|
|
++sPL;
|
|
|
|
|
2015-12-14 07:41:15 +00:00
|
|
|
wxFileName cfgpath( m_ConfigDir, S3D_RESOLVER_CONFIG );
|
|
|
|
wxString cfgname = cfgpath.GetFullPath();
|
2015-12-08 07:31:57 +00:00
|
|
|
std::ofstream cfgFile;
|
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
if( sPL == ePL )
|
2016-01-18 08:53:35 +00:00
|
|
|
{
|
|
|
|
wxMessageDialog md( NULL,
|
2016-01-19 00:05:40 +00:00
|
|
|
_( "3D search path list is empty;\ncontinue to write empty file?" ),
|
|
|
|
_( "Write 3D search path list" ), wxYES_NO );
|
2016-01-18 08:53:35 +00:00
|
|
|
|
|
|
|
if( md.ShowModal() == wxID_YES )
|
|
|
|
{
|
|
|
|
cfgFile.open( cfgname.ToUTF8(), std::ios_base::trunc );
|
|
|
|
|
|
|
|
if( !cfgFile.is_open() )
|
|
|
|
{
|
2016-01-19 00:05:40 +00:00
|
|
|
wxMessageBox( _( "Could not open configuration file" ),
|
|
|
|
_( "Write 3D search path list" ) );
|
2016-01-18 08:53:35 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
cfgFile << "#V" << CFGFILE_VERSION << "\n";
|
2016-01-18 08:53:35 +00:00
|
|
|
cfgFile.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
cfgFile.open( cfgname.ToUTF8(), std::ios_base::trunc );
|
|
|
|
|
|
|
|
if( !cfgFile.is_open() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-08 12:11:05 +00:00
|
|
|
wxString errmsg = _( "Could not open configuration file" );
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-04-08 12:11:05 +00:00
|
|
|
wxMessageBox( errmsg, _( "Write 3D search path list" ) );
|
2016-01-18 08:53:35 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:36:26 +00:00
|
|
|
cfgFile << "#V" << CFGFILE_VERSION << "\n";
|
2016-01-15 00:28:28 +00:00
|
|
|
std::string tstr;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( sPL != ePL )
|
|
|
|
{
|
2016-01-15 00:28:28 +00:00
|
|
|
tstr = sPL->m_alias.ToUTF8();
|
|
|
|
cfgFile << "\"" << tstr.size() << ":" << tstr << "\",";
|
|
|
|
tstr = sPL->m_pathvar.ToUTF8();
|
|
|
|
cfgFile << "\"" << tstr.size() << ":" << tstr << "\",";
|
|
|
|
tstr = sPL->m_description.ToUTF8();
|
|
|
|
cfgFile << "\"" << tstr.size() << ":" << tstr << "\"\n";
|
2015-12-08 07:31:57 +00:00
|
|
|
++sPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bad = cfgFile.bad();
|
|
|
|
cfgFile.close();
|
|
|
|
|
|
|
|
if( bad )
|
2016-01-18 08:53:35 +00:00
|
|
|
{
|
2016-01-19 00:05:40 +00:00
|
|
|
wxMessageBox( _( "Problems writing configuration file" ),
|
|
|
|
_( "Write 3D search path list" ) );
|
2016-01-18 08:53:35 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
2016-01-18 08:53:35 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
void S3D_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
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( aPath.StartsWith( "$(" ) )
|
|
|
|
useParen = true;
|
|
|
|
else if( !aPath.StartsWith( "${" ) )
|
|
|
|
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 )
|
|
|
|
pEnd = aPath.find( ")" );
|
|
|
|
else
|
|
|
|
pEnd = aPath.find( "}" );
|
|
|
|
|
|
|
|
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
|
2016-06-13 12:19:47 +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
|
|
|
{
|
2016-06-09 05:48:49 +00:00
|
|
|
if( sPL->m_alias == envar )
|
|
|
|
return;
|
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
if( !sPL->m_alias.StartsWith( "${" ) )
|
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
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
S3D_ALIAS lpath;
|
|
|
|
lpath.m_alias = envar;
|
|
|
|
lpath.m_pathvar = lpath.m_alias;
|
|
|
|
wxFileName tmpFN;
|
2016-04-17 22:35:32 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( lpath.m_alias.StartsWith( "${" ) || lpath.m_alias.StartsWith( "$(" ) )
|
|
|
|
tmpFN.Assign( ExpandEnvVarSubstitutions( lpath.m_alias ), "" );
|
|
|
|
else
|
|
|
|
tmpFN.Assign( lpath.m_alias, "" );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
wxUniChar psep = tmpFN.GetPathSeparator();
|
|
|
|
tmpFN.Normalize();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( !tmpFN.DirExists() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
lpath.m_pathexp = tmpFN.GetFullPath();
|
|
|
|
|
|
|
|
if( !lpath.m_pathexp.empty() && psep == *lpath.m_pathexp.rbegin() )
|
|
|
|
lpath.m_pathexp.erase( --lpath.m_pathexp.end() );
|
|
|
|
|
|
|
|
if( lpath.m_pathexp.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_Paths.insert( sPL, lpath );
|
|
|
|
return;
|
2016-04-17 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString S3D_FILENAME_RESOLVER::ShortenPath( const wxString& aFullPathName )
|
|
|
|
{
|
|
|
|
wxString fname = aFullPathName;
|
|
|
|
|
|
|
|
if( m_Paths.empty() )
|
|
|
|
createPathList();
|
|
|
|
|
|
|
|
wxCriticalSectionLocker lock( lock3D_resolver );
|
|
|
|
std::list< S3D_ALIAS >::const_iterator sL = m_Paths.begin();
|
|
|
|
std::list< S3D_ALIAS >::const_iterator eL = m_Paths.end();
|
|
|
|
size_t idx;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
while( sL != eL )
|
|
|
|
{
|
2016-04-17 22:35:32 +00:00
|
|
|
// undefined paths do not participate in the
|
|
|
|
// file name shortening procedure
|
|
|
|
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
|
|
|
|
if( sL->m_alias.StartsWith( "${" ) || sL->m_alias.StartsWith( "$(" ) )
|
|
|
|
{
|
|
|
|
wxString tpath = ExpandEnvVarSubstitutions( sL->m_alias );
|
|
|
|
|
|
|
|
if( tpath.empty() )
|
|
|
|
{
|
|
|
|
++sL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpath.Assign( tpath, wxT( "" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fpath.Assign( sL->m_pathexp, wxT( "" ) );
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
if( std::string::npos != idx && 0 == idx )
|
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
|
|
|
|
2016-06-09 05:48:49 +00:00
|
|
|
if( sL->m_alias.StartsWith( "${" ) || sL->m_alias.StartsWith( "$(" ) )
|
2016-04-17 22:35:32 +00:00
|
|
|
{
|
|
|
|
// old style ENV_VAR
|
|
|
|
tname = sL->m_alias;
|
|
|
|
tname.Append( "/" );
|
|
|
|
tname.append( fname );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// new style alias
|
|
|
|
tname = ":";
|
|
|
|
tname.append( sL->m_alias );
|
|
|
|
tname.append( ":" );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 00:28:28 +00:00
|
|
|
const std::list< S3D_ALIAS >* S3D_FILENAME_RESOLVER::GetPaths( void )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return &m_Paths;
|
|
|
|
}
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::SplitAlias( const wxString& aFileName,
|
|
|
|
wxString& anAlias, wxString& aRelPath )
|
|
|
|
{
|
|
|
|
anAlias.clear();
|
|
|
|
aRelPath.clear();
|
|
|
|
|
|
|
|
if( !aFileName.StartsWith( wxT( ":" ) ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t tagpos = aFileName.find( wxT( ":" ), 1 );
|
|
|
|
|
|
|
|
if( wxString::npos == tagpos || 1 == tagpos )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( tagpos + 1 >= aFileName.length() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
anAlias = aFileName.substr( 1, tagpos - 1 );
|
|
|
|
aRelPath = aFileName.substr( tagpos + 1 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool getHollerith( const std::string& aString, size_t& aIndex, wxString& aResult )
|
|
|
|
{
|
|
|
|
aResult.clear();
|
|
|
|
|
2016-02-29 23:08:19 +00:00
|
|
|
if( aIndex >= aString.size() )
|
2016-01-15 00:28:28 +00:00
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "bad Hollerith string on line";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t i2 = aString.find( '"', aIndex );
|
|
|
|
|
|
|
|
if( std::string::npos == i2 )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "missing opening quote mark in config file";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++i2;
|
|
|
|
|
|
|
|
if( i2 >= aString.size() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "invalid entry (unexpected end of line)";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string tnum;
|
|
|
|
|
|
|
|
while( aString[i2] >= '0' && aString[i2] <= '9' )
|
|
|
|
tnum.append( 1, aString[i2++] );
|
|
|
|
|
|
|
|
if( tnum.empty() || aString[i2++] != ':' )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "bad Hollerith string on line";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::istringstream istr;
|
|
|
|
istr.str( tnum );
|
|
|
|
size_t nchars;
|
|
|
|
istr >> nchars;
|
|
|
|
|
|
|
|
if( (i2 + nchars) >= aString.size() )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-21 07:19:08 +00:00
|
|
|
wxString errmsg = "invalid entry (unexpected end of line)";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( nchars > 0 )
|
|
|
|
{
|
|
|
|
aResult = aString.substr( i2, nchars );
|
|
|
|
i2 += nchars;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aString[i2] != '"' )
|
|
|
|
{
|
2016-02-23 03:33:24 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
|
2016-04-08 12:11:05 +00:00
|
|
|
wxString errmsg = "missing closing quote mark in config file";
|
2016-02-23 03:33:24 +00:00
|
|
|
ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
|
|
|
|
wxLogTrace( MASK_3D_RESOLVER, "%s\n", ostr.str().c_str() );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aIndex = i2 + 1;
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::ValidateFileName( const wxString& aFileName, bool& hasAlias )
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
size_t pos0 = aFileName.find( ':' );
|
|
|
|
|
|
|
|
// ensure that the file separators suit the current platform
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
filename.Replace( wxT( "/" ), wxT( "\\" ) );
|
|
|
|
|
|
|
|
// if we see the :\ pattern then it must be a drive designator
|
|
|
|
if( pos0 != wxString::npos )
|
|
|
|
{
|
2017-05-28 16:56:49 +00:00
|
|
|
size_t pos1 = filename.find( wxT( ":\\" ) );
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
if( pos1 != wxString::npos && ( pos1 != pos0 || pos1 != 1 ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// if we have a drive designator then we have no alias
|
|
|
|
if( pos1 != wxString::npos )
|
|
|
|
pos0 = wxString::npos;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
filename.Replace( wxT( "\\" ), wxT( "/" ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// names may not end with ':'
|
|
|
|
if( pos0 == aFileName.length() -1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( pos0 != wxString::npos )
|
|
|
|
{
|
|
|
|
// ensure the alias component is not empty
|
|
|
|
if( pos0 == 0 )
|
|
|
|
return false;
|
|
|
|
|
2016-05-30 08:22:19 +00:00
|
|
|
wxString lpath = filename.substr( 0, pos0 );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-12 22:46:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool S3D_FILENAME_RESOLVER::GetKicadPaths( std::list< wxString >& paths )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if( mS->first == wxString( "KICAD_PTEMPLATES" )
|
|
|
|
|| mS->first == wxString( "KIGITHUB" )
|
|
|
|
|| mS->first == wxString( "KISYSMOD" ) )
|
|
|
|
{
|
|
|
|
++mS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( wxString::npos != mS->second.GetValue().find( wxString( "://" ) ) )
|
|
|
|
{
|
|
|
|
++mS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString tmp( "${" );
|
|
|
|
tmp.Append( mS->first );
|
|
|
|
tmp.Append( "}" );
|
|
|
|
paths.push_back( tmp );
|
|
|
|
|
|
|
|
if( tmp == "${KISYS3DMOD}" )
|
|
|
|
hasKisys3D = true;
|
|
|
|
|
|
|
|
++mS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !hasKisys3D )
|
|
|
|
paths.push_back( "${KISYS3DMOD}" );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|