You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file filename_resolver.h
  26. */
  27. #ifndef FILENAME_RESOLVER_H
  28. #define FILENAME_RESOLVER_H
  29. #include <list>
  30. #include <map>
  31. #include <vector>
  32. #include <wx/string.h>
  33. class PROJECT;
  34. class PGM_BASE;
  35. struct SEARCH_PATH
  36. {
  37. wxString m_Alias; // alias to the base path
  38. wxString m_Pathvar; // base path as stored in the config file
  39. wxString m_Pathexp; // expanded base path
  40. wxString m_Description; // description of the aliased path
  41. };
  42. /**
  43. * Provide an extensible class to resolve 3D model paths.
  44. *
  45. * Initially the legacy behavior will be implemented and an incomplete path would be checked
  46. * against the project directory or the KICAD7_3DMODEL_DIR environment variable. In the future a
  47. * configurable set of search paths may be specified.
  48. */
  49. class FILENAME_RESOLVER
  50. {
  51. public:
  52. FILENAME_RESOLVER();
  53. /**
  54. * Set the user's configuration directory for 3D models.
  55. *
  56. * @param aConfigDir
  57. * @return true if the call succeeds (directory exists).
  58. */
  59. bool Set3DConfigDir( const wxString& aConfigDir );
  60. /**
  61. * Set the current KiCad project directory as the first entry in the model path list.
  62. *
  63. * @param[in] aProjDir current project directory
  64. * @param[out] flgChanged optional, set to true if directory was changed
  65. * @retval true success
  66. * @retval false failure
  67. */
  68. bool SetProject( PROJECT* aProject, bool* flgChanged = nullptr );
  69. wxString GetProjectDir() const;
  70. /**
  71. * Set a pointer to the application's #PGM_BASE instance used to extract the local env vars.
  72. */
  73. void SetProgramBase( PGM_BASE* aBase );
  74. /**
  75. * Clear the current path list and substitutes the given path list and update the path
  76. * configuration file on success.
  77. */
  78. bool UpdatePathList( const std::vector<SEARCH_PATH>& aPathList );
  79. /**
  80. * Determines the full path of the given file name.
  81. *
  82. * In the future remote files may be supported, in which case it is best to require a full
  83. * URI in which case ResolvePath should check that the URI conforms to RFC-2396 and related
  84. * documents and copies \a aFileName into aResolvedName if the URI is valid.
  85. *
  86. * @param aFileName The configured file path to resolve
  87. * @param aWorkingPath The current working path for relative path resolutions
  88. */
  89. wxString ResolvePath( const wxString& aFileName, const wxString& aWorkingPath );
  90. /**
  91. * Produce a relative path based on the existing search directories or returns the same path
  92. * if the path is not a superset of an existing search path.
  93. *
  94. * @param aFullPathName is an absolute path to shorten.
  95. * @return the shortened path or \a aFullPathName.
  96. */
  97. wxString ShortenPath( const wxString& aFullPathName );
  98. /**
  99. * Return a pointer to the internal path list; the items in:load.
  100. *
  101. * The list can be used to set up the list of search paths available to a 3D file browser.
  102. *
  103. * @return pointer to the internal path list.
  104. */
  105. const std::list<SEARCH_PATH>* GetPaths() const;
  106. /**
  107. * Return true if the given name contains an alias and populates the string \a anAlias
  108. * with the alias and \a aRelPath with the relative path.
  109. */
  110. bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath ) const;
  111. /**
  112. * Returns true if the given path is a valid aliased relative path.
  113. *
  114. * If the path contains an alias then hasAlias is set true.
  115. */
  116. bool ValidateFileName( const wxString& aFileName, bool& hasAlias ) const;
  117. /**
  118. * Return a list of path environment variables local to KiCad.
  119. *
  120. * This list always includes KICAD7_3DMODEL_DIR even if it is not defined locally.
  121. */
  122. bool GetKicadPaths( std::list< wxString >& paths ) const;
  123. private:
  124. /**
  125. * Build the path list using available information such as KICAD7_3DMODEL_DIR and the 3d_path_list
  126. * configuration file.
  127. *
  128. * @warning Invalid paths are silently discarded and removed from the configuration file.
  129. *
  130. * @return true if at least one valid path was found.
  131. */
  132. bool createPathList( void );
  133. /**
  134. * Check that a path is valid and adds it to the search list.
  135. *
  136. * @param aPath is the alias set to be checked and added.
  137. * @return true if aPath is valid.
  138. */
  139. bool addPath( const SEARCH_PATH& aPath );
  140. /**
  141. * Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if
  142. * it is not yet in the list.
  143. */
  144. void checkEnvVarPath( const wxString& aPath );
  145. wxString m_configDir; // 3D configuration directory
  146. std::list<SEARCH_PATH> m_paths; // list of base paths to search from
  147. int m_errflags;
  148. PGM_BASE* m_pgm;
  149. PROJECT* m_project;
  150. wxString m_curProjDir;
  151. };
  152. #endif // FILENAME_RESOLVER_H