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>
|
2020-12-19 16:00:52 +00:00
|
|
|
* Copyright (C) 2020 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-03-07 13:05:12 +00:00
|
|
|
* @file filename_resolver.h
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
#ifndef FILENAME_RESOLVER_H
|
|
|
|
#define FILENAME_RESOLVER_H
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <wx/string.h>
|
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
class PGM_BASE;
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
struct SEARCH_PATH
|
2016-01-15 00:28:28 +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-01-15 00:28:28 +00:00
|
|
|
};
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-19 16:00:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide an extensible class to resolve 3D model paths.
|
|
|
|
*
|
|
|
|
* Initially the legacy behavior will be implemented and an incomplete path would be checked
|
|
|
|
* against the project directory or the KISYS3DMOD environment variable. In the future a
|
|
|
|
* configurable set of search paths may be specified.
|
|
|
|
*/
|
2018-03-07 13:05:12 +00:00
|
|
|
class FILENAME_RESOLVER
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-03-07 13:05:12 +00:00
|
|
|
FILENAME_RESOLVER();
|
2016-01-15 00:28:28 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Set the user's configuration directory for 3D models.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* @param aConfigDir
|
2020-12-19 16:00:52 +00:00
|
|
|
* @return true if the call succeeds (directory exists).
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
bool Set3DConfigDir( const wxString& aConfigDir );
|
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Set the current KiCad project directory as the first entry in the model path list.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
2017-06-11 05:38:09 +00:00
|
|
|
* @param[in] aProjDir current project directory
|
|
|
|
* @param[out] flgChanged optional, set to true if directory was changed
|
|
|
|
* @retval true success
|
|
|
|
* @retval false failure
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2020-12-19 16:00:52 +00:00
|
|
|
bool SetProject( PROJECT* aProject, bool* flgChanged = nullptr );
|
2020-04-05 19:51:48 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
wxString GetProjectDir( void );
|
|
|
|
|
2016-04-17 22:35:32 +00:00
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Set a pointer to the application's #PGM_BASE instance used to extract the local env vars.
|
2016-04-17 22:35:32 +00:00
|
|
|
*/
|
|
|
|
void SetProgramBase( PGM_BASE* aBase );
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Clear the current path list and substitutes the given path list and update the path
|
|
|
|
* configuration file on success.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2018-03-07 13:05:12 +00:00
|
|
|
bool UpdatePathList( std::vector< SEARCH_PATH >& aPathList );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Determines the full path of the given file name.
|
|
|
|
*
|
|
|
|
* 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 aResolvedName if the URI is valid.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
wxString ResolvePath( const wxString& aFileName );
|
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +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.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
2020-12-19 16:00:52 +00:00
|
|
|
* @param aFullPathName is an absolute path to shorten.
|
|
|
|
* @return the shortened path or \a aFullPathName.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
wxString ShortenPath( const wxString& aFullPathName );
|
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Return a pointer to the internal path list; the items in:load.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
2020-12-19 16:00:52 +00:00
|
|
|
* The list can be used to set up the list of search paths available to a 3D file browser.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
2020-12-19 16:00:52 +00:00
|
|
|
* @return pointer to the internal path list.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2018-03-07 13:05:12 +00:00
|
|
|
const std::list< SEARCH_PATH >* GetPaths( void );
|
2016-01-15 00:28:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Return true if the given name contains an alias and populates the string \a anAlias
|
|
|
|
* with the alias and \a aRelPath with the relative path.
|
2016-01-15 00:28:28 +00:00
|
|
|
*/
|
|
|
|
bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath );
|
2016-01-23 03:52:54 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Returns true if the given path is a valid aliased relative path.
|
|
|
|
*
|
2016-01-23 03:52:54 +00:00
|
|
|
* If the path contains an alias then hasAlias is set true.
|
|
|
|
*/
|
|
|
|
bool ValidateFileName( const wxString& aFileName, bool& hasAlias );
|
2016-06-12 22:46:00 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* Return a list of path environment variables local to KiCad.
|
|
|
|
*
|
|
|
|
* This list always includes KISYS3DMOD even if it is not defined locally.
|
2016-06-12 22:46:00 +00:00
|
|
|
*/
|
|
|
|
bool GetKicadPaths( std::list< wxString >& paths );
|
2020-12-19 16:00:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Build the path list using available information such as KISYS3DMOD and the 3d_path_list
|
|
|
|
* configuration file.
|
|
|
|
*
|
|
|
|
* @warning Invalid paths are silently discarded and removed from the configuration file.
|
|
|
|
*
|
|
|
|
* @return true if at least one valid path was found.
|
|
|
|
*/
|
|
|
|
bool createPathList( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that a path is valid and adds it to the search list.
|
|
|
|
*
|
|
|
|
* @param aPath is the alias set to be checked and added.
|
|
|
|
* @return true if aPath is valid.
|
|
|
|
*/
|
|
|
|
bool addPath( const SEARCH_PATH& aPath );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a list of path names from a configuration file.
|
|
|
|
*
|
|
|
|
* @return true if a file was found and contained at least one valid path.
|
|
|
|
*/
|
|
|
|
bool readPathList( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the current path list to a configuration file.
|
|
|
|
*
|
|
|
|
* @return true if the path list was not empty and was successfully written to the
|
|
|
|
* configuration file.
|
|
|
|
*/
|
|
|
|
bool writePathList( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void checkEnvVarPath( const wxString& aPath );
|
|
|
|
|
|
|
|
wxString m_configDir; // 3D configuration directory
|
|
|
|
std::list<SEARCH_PATH> m_paths; // list of base paths to search from
|
|
|
|
int m_errflags;
|
|
|
|
PGM_BASE* m_pgm;
|
|
|
|
PROJECT* m_project;
|
|
|
|
wxString m_curProjDir;
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
2018-03-07 13:05:12 +00:00
|
|
|
#endif // FILENAME_RESOLVER_H
|