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.

205 lines
6.6 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. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file filename_resolver.h
  25. * provides an extensible class to resolve 3D model paths. Initially
  26. * the legacy behavior will be implemented and an incomplete path
  27. * would be checked against the project directory or the KISYS3DMOD
  28. * environment variable. In the future a configurable set of search
  29. * paths may be specified.
  30. */
  31. #ifndef FILENAME_RESOLVER_H
  32. #define FILENAME_RESOLVER_H
  33. #include <list>
  34. #include <map>
  35. #include <vector>
  36. #include <wx/string.h>
  37. class PGM_BASE;
  38. struct SEARCH_PATH
  39. {
  40. wxString m_alias; // alias to the base path
  41. wxString m_pathvar; // base path as stored in the config file
  42. wxString m_pathexp; // expanded base path
  43. wxString m_description; // description of the aliased path
  44. };
  45. class FILENAME_RESOLVER
  46. {
  47. private:
  48. wxString m_ConfigDir; // 3D configuration directory
  49. std::list<SEARCH_PATH> m_Paths; // list of base paths to search from
  50. int m_errflags;
  51. PGM_BASE* m_pgm;
  52. PROJECT* m_project;
  53. wxString m_curProjDir;
  54. /**
  55. * Function createPathList
  56. * builds the path list using available information such as
  57. * KISYS3DMOD and the 3d_path_list configuration file. Invalid
  58. * paths are silently discarded and removed from the configuration
  59. * file.
  60. *
  61. * @return true if at least one valid path was found
  62. */
  63. bool createPathList( void );
  64. /**
  65. * Function addPath
  66. * checks that a path is valid and adds it to the search list
  67. *
  68. * @param aPath is the alias set to be checked and added
  69. * @return true if aPath is valid
  70. */
  71. bool addPath( const SEARCH_PATH& aPath );
  72. /**
  73. * Function readPathList
  74. * reads a list of path names from a configuration file
  75. *
  76. * @return true if a file was found and contained at least
  77. * one valid path
  78. */
  79. bool readPathList( void );
  80. /**
  81. * Function writePathList
  82. * writes the current path list to a configuration file
  83. *
  84. * @return true if the path list was not empty and was
  85. * successfully written to the configuration file
  86. */
  87. bool writePathList( void );
  88. /**
  89. * Function checkEnvVarPath
  90. * checks the ${ENV_VAR} component of a path and adds
  91. * it to the resolver's path list if it is not yet in
  92. * the list
  93. */
  94. void checkEnvVarPath( const wxString& aPath );
  95. public:
  96. FILENAME_RESOLVER();
  97. /**
  98. * Function Set3DConfigDir
  99. * sets the user's configuration directory
  100. * for 3D models.
  101. *
  102. * @param aConfigDir
  103. * @return true if the call succeeds (directory exists)
  104. */
  105. bool Set3DConfigDir( const wxString& aConfigDir );
  106. /**
  107. * Function SetProjectDir
  108. * sets the current KiCad project directory as the first
  109. * entry in the model path list
  110. *
  111. * @param[in] aProjDir current project directory
  112. * @param[out] flgChanged optional, set to true if directory was changed
  113. * @retval true success
  114. * @retval false failure
  115. */
  116. bool SetProject( PROJECT* aProject, bool* flgChanged = NULL );
  117. wxString GetProjectDir( void );
  118. /**
  119. * Function SetProgramBase
  120. * sets a pointer to the application's PGM_BASE instance;
  121. * the pointer is used to extract the local env vars.
  122. */
  123. void SetProgramBase( PGM_BASE* aBase );
  124. /**
  125. * Function UpdatePathList
  126. * clears the current path list and substitutes the given path
  127. * list, updating the path configuration file on success.
  128. */
  129. bool UpdatePathList( std::vector< SEARCH_PATH >& aPathList );
  130. /**
  131. * Function ResolvePath
  132. * determines the full path of the given file name. In the future
  133. * remote files may be supported, in which case it is best to
  134. * require a full URI in which case ResolvePath should check that
  135. * the URI conforms to RFC-2396 and related documents and copies
  136. * aFileName into aResolvedName if the URI is valid.
  137. */
  138. wxString ResolvePath( const wxString& aFileName );
  139. /**
  140. * Function ShortenPath
  141. * produces a relative path based on the existing
  142. * search directories or returns the same path if
  143. * the path is not a superset of an existing search path.
  144. *
  145. * @param aFullPathName is an absolute path to shorten
  146. * @return the shortened path or aFullPathName
  147. */
  148. wxString ShortenPath( const wxString& aFullPathName );
  149. /**
  150. * Function GetPaths
  151. * returns a pointer to the internal path list; the items in:load
  152. *
  153. * the list can be used to set up the list of search paths
  154. * available to a 3D file browser.
  155. *
  156. * @return pointer to the internal path list
  157. */
  158. const std::list< SEARCH_PATH >* GetPaths( void );
  159. /**
  160. * Function SplitAlias
  161. * returns true if the given name contains an alias and
  162. * populates the string anAlias with the alias and aRelPath
  163. * with the relative path.
  164. */
  165. bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath );
  166. /**
  167. * Function ValidateName
  168. * returns true if the given path is a valid aliased relative path.
  169. * If the path contains an alias then hasAlias is set true.
  170. */
  171. bool ValidateFileName( const wxString& aFileName, bool& hasAlias );
  172. /**
  173. * Function GetKicadPaths
  174. * returns a list of path environment variables local to Kicad;
  175. * this list always includes KISYS3DMOD even if it is not
  176. * defined locally.
  177. */
  178. bool GetKicadPaths( std::list< wxString >& paths );
  179. };
  180. #endif // FILENAME_RESOLVER_H