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.

180 lines
6.3 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 The 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 configuration 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( const 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. * Determine 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 aEmbeddedFilesStack is a list of pointers to the embedded files list. They will
  90. * be searched from the front of the list.
  91. */
  92. wxString ResolvePath( const wxString& aFileName, const wxString& aWorkingPath,
  93. std::vector<const EMBEDDED_FILES*> aEmbeddedFilesStack );
  94. /**
  95. * Produce a relative path based on the existing search directories or returns the same path
  96. * if the path is not a superset of an existing search path.
  97. *
  98. * @param aFullPathName is an absolute path to shorten.
  99. * @return the shortened path or \a aFullPathName.
  100. */
  101. wxString ShortenPath( const wxString& aFullPathName );
  102. /**
  103. * Return a pointer to the internal path list; the items in:load.
  104. *
  105. * The list can be used to set up the list of search paths available to a 3D file browser.
  106. *
  107. * @return pointer to the internal path list.
  108. */
  109. const std::list<SEARCH_PATH>* GetPaths() const;
  110. /**
  111. * Return true if the given name contains an alias and populates the string \a anAlias
  112. * with the alias and \a aRelPath with the relative path.
  113. */
  114. bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath ) const;
  115. /**
  116. * Return true if the given path is a valid aliased relative path.
  117. *
  118. * If the path contains an alias then hasAlias is set true.
  119. */
  120. bool ValidateFileName( const wxString& aFileName, bool& hasAlias ) const;
  121. /**
  122. * Return a list of path environment variables local to KiCad.
  123. *
  124. * This list always includes KICAD7_3DMODEL_DIR even if it is not defined locally.
  125. */
  126. bool GetKicadPaths( std::list< wxString >& paths ) const;
  127. private:
  128. /**
  129. * Build the path list using available information such as KICAD7_3DMODEL_DIR and the
  130. * 3d_path_list configuration file.
  131. *
  132. * @warning Invalid paths are silently discarded and removed from the configuration file.
  133. *
  134. * @return true if at least one valid path was found.
  135. */
  136. bool createPathList( void );
  137. /**
  138. * Check that a path is valid and adds it to the search list.
  139. *
  140. * @param aPath is the alias set to be checked and added.
  141. * @return true if aPath is valid.
  142. */
  143. bool addPath( const SEARCH_PATH& aPath );
  144. /**
  145. * Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if
  146. * it is not yet in the list.
  147. */
  148. void checkEnvVarPath( const wxString& aPath );
  149. wxString m_configDir; ///< 3D configuration directory.
  150. std::list<SEARCH_PATH> m_paths; ///< List of base paths to search from.
  151. int m_errflags;
  152. PGM_BASE* m_pgm;
  153. const PROJECT* m_project;
  154. wxString m_curProjDir;
  155. };
  156. #endif // FILENAME_RESOLVER_H