2016-09-02 10:08:40 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2021-07-29 11:10:58 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-09-02 10:08:40 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file 3d_resolver.h
|
|
|
|
* provides an extensible class to resolve 3D model paths.
|
|
|
|
* Derived from 3d_filename_resolver.h,cpp and modified for
|
|
|
|
* use in stand-alone utilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RESOLVER_3D_H
|
|
|
|
#define RESOLVER_3D_H
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <wx/string.h>
|
|
|
|
|
|
|
|
namespace S3D
|
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
struct rsort_wxString
|
|
|
|
{
|
|
|
|
bool operator() (const wxString& strA, const wxString& strB ) const
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
// sort a wxString using the reverse character order; for 3d model
|
|
|
|
// filenames this will typically be a much faster operation than
|
|
|
|
// a normal alphabetic sort
|
|
|
|
wxString::const_reverse_iterator sA = strA.rbegin();
|
|
|
|
wxString::const_reverse_iterator eA = strA.rend();
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
wxString::const_reverse_iterator sB = strB.rbegin();
|
|
|
|
wxString::const_reverse_iterator eB = strB.rend();
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( strA.empty() )
|
|
|
|
{
|
2016-09-02 10:08:40 +00:00
|
|
|
if( strB.empty() )
|
|
|
|
return false;
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
// note: this rule implies that a null string is first in the sort order
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strB.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while( sA != eA && sB != eB )
|
|
|
|
{
|
|
|
|
if( (*sA) == (*sB) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
++sA;
|
|
|
|
++sB;
|
|
|
|
continue;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( (*sA) < (*sB) )
|
|
|
|
return true;
|
|
|
|
else
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
if( sB == eB )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
}; // end NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
class KICADPCB;
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
struct SEARCH_PATH
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
wxString m_Alias; // alias to the base path
|
|
|
|
wxString m_Pathvar; // base path as stored in the config file
|
|
|
|
wxString m_Pathexp; // expanded base path
|
|
|
|
wxString m_Description; // description of the aliased path
|
2016-09-02 10:08:40 +00:00
|
|
|
};
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
class S3D_RESOLVER
|
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
public:
|
|
|
|
S3D_RESOLVER();
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Set the user's configuration directory for 3D models.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
2021-07-29 11:10:58 +00:00
|
|
|
* @param aConfigDir
|
|
|
|
* @return true if the call succeeds (directory exists).
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool Set3DConfigDir( const wxString& aConfigDir );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Determine the full path of the given file name.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
2021-07-29 11:10:58 +00:00
|
|
|
* In the future remote files may be supported, in which case it is best to require a full
|
|
|
|
* URI in which case #ResolvePath should check that the URI conforms to RFC-2396 and related
|
|
|
|
* documents and copies \a aFileName into the resolved name if the URI is valid.
|
2021-09-21 16:51:59 +00:00
|
|
|
*
|
|
|
|
* If the file is not found, \a aSearchedPaths will contain the paths that were searched.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-09-21 16:51:59 +00:00
|
|
|
wxString ResolvePath( const wxString& aFileName, std::vector<wxString>& aSearchedPaths );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Produce a relative path based on the existing search directories or returns the same path
|
|
|
|
* if the path is not a superset of an existing search path.
|
|
|
|
*
|
|
|
|
* @param aFullPathName is an absolute path to shorten.
|
|
|
|
* @return the shortened path or aFullPathName.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
wxString ShortenPath( const wxString& aFullPathName );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Return a pointer to the internal path list.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
2021-07-29 11:10:58 +00:00
|
|
|
* The list can be used to set up the list of search paths available to a 3D file browser.
|
|
|
|
*
|
|
|
|
* @return the search path list.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
const std::list< SEARCH_PATH >* GetPaths( void );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Return true if the given name contains an alias and populates the string with the alias
|
|
|
|
* and the relative path.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* If the path contains an alias then \a hasAlias is set true.
|
|
|
|
*
|
|
|
|
* @return true if the given path is a valid aliased relative path.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool ValidateFileName( const wxString& aFileName, bool& hasAlias );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
private:
|
2016-09-02 10:08:40 +00:00
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Build the path list using available information such as KICAD6_3DMODEL_DIR and the
|
|
|
|
* 3d_path_list configuration file.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
2021-07-29 11:10:58 +00:00
|
|
|
* Invalid paths are silently discarded and removed from the configuration file.
|
|
|
|
*
|
|
|
|
* @return true if at least one valid path was found
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool createPathList( void );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Check that \a aPath is valid and adds it to the search list.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
2021-07-29 11:10:58 +00:00
|
|
|
* @param aPath is the alias set to be checked and added.
|
|
|
|
* @return true if \a aPath is valid.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool addPath( const SEARCH_PATH& aPath );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Read a list of path names from a configuration file.
|
|
|
|
*
|
|
|
|
* @return true if a file was found and contained at least one valid path.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
bool readPathList( void );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-29 11:10:58 +00:00
|
|
|
* Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if it
|
|
|
|
* is not yet in the list.
|
2016-09-02 10:08:40 +00:00
|
|
|
*/
|
2021-07-29 11:10:58 +00:00
|
|
|
void checkEnvVarPath( const wxString& aPath );
|
|
|
|
|
|
|
|
wxString expandVars( const wxString& aPath );
|
|
|
|
|
|
|
|
std::list< SEARCH_PATH > m_Paths; ///< List of base search paths.
|
|
|
|
|
|
|
|
///< Mapping of (short) file names to resolved names.
|
|
|
|
std::map< wxString, wxString, S3D::rsort_wxString > m_NameMap;
|
|
|
|
int m_errflags;
|
|
|
|
wxString m_curProjDir;
|
|
|
|
|
|
|
|
///< Environment variables.
|
|
|
|
std::map< wxString, wxString > m_EnvVars;
|
2016-09-02 10:08:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RESOLVER_3D_H
|