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.

179 lines
6.1 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. class EMBEDDED_FILES;
  36. struct SEARCH_PATH
  37. {
  38. wxString m_Alias; // alias to the base path
  39. wxString m_Pathvar; // base path as stored in the config file
  40. wxString m_Pathexp; // expanded base path
  41. wxString m_Description; // description of the aliased path
  42. };
  43. /**
  44. * Provide an extensible class to resolve 3D model paths.
  45. *
  46. * Initially the legacy behavior will be implemented and an incomplete path would be checked
  47. * against the project directory or the KICAD7_3DMODEL_DIR environment variable. In the future a
  48. * configurable set of search paths may be specified.
  49. */
  50. class FILENAME_RESOLVER
  51. {
  52. public:
  53. FILENAME_RESOLVER();
  54. /**
  55. * Set the user's configuration directory for 3D models.
  56. *
  57. * @param aConfigDir
  58. * @return true if the call succeeds (directory exists).
  59. */
  60. bool Set3DConfigDir( const wxString& aConfigDir );
  61. /**
  62. * Set the current KiCad project directory as the first entry in the model path list.
  63. *
  64. * @param[in] aProjDir current project directory
  65. * @param[out] flgChanged optional, set to true if directory was changed
  66. * @retval true success
  67. * @retval false failure
  68. */
  69. bool SetProject( PROJECT* aProject, bool* flgChanged = nullptr );
  70. wxString GetProjectDir() const;
  71. /**
  72. * Set a pointer to the application's #PGM_BASE instance used to extract the local env vars.
  73. */
  74. void SetProgramBase( PGM_BASE* aBase );
  75. /**
  76. * Clear the current path list and substitutes the given path list and update the path
  77. * configuration file on success.
  78. */
  79. bool UpdatePathList( const std::vector<SEARCH_PATH>& aPathList );
  80. /**
  81. * Determines the full path of the given file name.
  82. *
  83. * In the future remote files may be supported, in which case it is best to require a full
  84. * URI in which case ResolvePath should check that the URI conforms to RFC-2396 and related
  85. * documents and copies \a aFileName into aResolvedName if the URI is valid.
  86. *
  87. * @param aFileName The configured file path to resolve
  88. * @param aWorkingPath The current working path for relative path resolutions
  89. * @param aFiles The embedded files object to use for embedded file resolution
  90. */
  91. wxString ResolvePath( const wxString& aFileName, const wxString& aWorkingPath,
  92. const EMBEDDED_FILES* aFiles );
  93. /**
  94. * Produce a relative path based on the existing search directories or returns the same path
  95. * if the path is not a superset of an existing search path.
  96. *
  97. * @param aFullPathName is an absolute path to shorten.
  98. * @return the shortened path or \a aFullPathName.
  99. */
  100. wxString ShortenPath( const wxString& aFullPathName );
  101. /**
  102. * Return a pointer to the internal path list; the items in:load.
  103. *
  104. * The list can be used to set up the list of search paths available to a 3D file browser.
  105. *
  106. * @return pointer to the internal path list.
  107. */
  108. const std::list<SEARCH_PATH>* GetPaths() const;
  109. /**
  110. * Return true if the given name contains an alias and populates the string \a anAlias
  111. * with the alias and \a aRelPath with the relative path.
  112. */
  113. bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath ) const;
  114. /**
  115. * Returns true if the given path is a valid aliased relative path.
  116. *
  117. * If the path contains an alias then hasAlias is set true.
  118. */
  119. bool ValidateFileName( const wxString& aFileName, bool& hasAlias ) const;
  120. /**
  121. * Return a list of path environment variables local to KiCad.
  122. *
  123. * This list always includes KICAD7_3DMODEL_DIR even if it is not defined locally.
  124. */
  125. bool GetKicadPaths( std::list< wxString >& paths ) const;
  126. private:
  127. /**
  128. * Build the path list using available information such as KICAD7_3DMODEL_DIR and the 3d_path_list
  129. * configuration file.
  130. *
  131. * @warning Invalid paths are silently discarded and removed from the configuration file.
  132. *
  133. * @return true if at least one valid path was found.
  134. */
  135. bool createPathList( void );
  136. /**
  137. * Check that a path is valid and adds it to the search list.
  138. *
  139. * @param aPath is the alias set to be checked and added.
  140. * @return true if aPath is valid.
  141. */
  142. bool addPath( const SEARCH_PATH& aPath );
  143. /**
  144. * Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if
  145. * it is not yet in the list.
  146. */
  147. void checkEnvVarPath( const wxString& aPath );
  148. wxString m_configDir; // 3D configuration directory
  149. std::list<SEARCH_PATH> m_paths; // list of base paths to search from
  150. int m_errflags;
  151. PGM_BASE* m_pgm;
  152. PROJECT* m_project;
  153. wxString m_curProjDir;
  154. };
  155. #endif // FILENAME_RESOLVER_H