KiCad PCB EDA Suite
Loading...
Searching...
No Matches
env_paths.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 Wayne Stambaugh <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2017 CERN
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <env_paths.h>
24#include <project.h>
25#include <wx/filename.h>
26
27static bool normalizeAbsolutePaths( const wxFileName& aPathA, const wxFileName& aPathB,
28 wxString* aResultPath )
29{
30 wxCHECK_MSG( aPathA.IsAbsolute(), false,
31 aPathA.GetPath() + wxS( " is not an absolute path." ) );
32 wxCHECK_MSG( aPathB.IsAbsolute(), false,
33 aPathB.GetPath() + wxS( " is not an absolute path." ) );
34
35 if( aPathA.GetPath() == aPathB.GetPath() )
36 return true;
37
38 // Not sure all of volume checks are necessary since wxFileName::GetVolume() returns
39 // an empty string if the path has no volume.
40 if( ( aPathA.GetDirCount() > aPathB.GetDirCount() )
41 || ( aPathA.HasVolume() && !aPathB.HasVolume() )
42 || ( !aPathA.HasVolume() && aPathB.HasVolume() )
43 || ( ( aPathA.HasVolume() && aPathB.HasVolume() )
44 && ( aPathA.GetVolume().CmpNoCase( aPathB.GetVolume() ) != 0 ) ) )
45 return false;
46
47 wxArrayString aDirs = aPathA.GetDirs();
48 wxArrayString bDirs = aPathB.GetDirs();
49
50 size_t i = 0;
51
52 while( i < aDirs.GetCount() )
53 {
54 if( aDirs[i] != bDirs[i] )
55 return false;
56
57 i++;
58 }
59
60 if( aResultPath )
61 {
62 while( i < bDirs.GetCount() )
63 {
64 *aResultPath += bDirs[i] + wxT( "/" );
65 i++;
66 }
67 }
68
69 return true;
70}
71
72
73wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
74 const wxString& aProjectPath )
75{
76 wxFileName envPath;
77 wxString varName;
78 wxString remainingPath;
79 wxString normalizedFullPath;
80 int pathDepth = 0;
81
82 if( aEnvVars )
83 {
84 for( const std::pair<const wxString, ENV_VAR_ITEM>& entry : *aEnvVars )
85 {
86 // Don't bother normalizing paths that don't exist or the user cannot read.
87 if( !wxFileName::DirExists( entry.second.GetValue() )
88 || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
89 {
90 continue;
91 }
92
93 envPath.SetPath( entry.second.GetValue() );
94
95 wxString tmp;
96
97 if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
98 {
99 int newDepth = envPath.GetDirs().GetCount();
100
101 // Only use the variable if it removes more directories than the previous ones
102 if( newDepth > pathDepth )
103 {
104 pathDepth = newDepth;
105 varName = entry.first;
106 remainingPath = tmp;
107 }
108
109 // @fixme Shouldn't we break here if an environment variable path is found or try
110 // at least try to pick the best match?
111 }
112 }
113 }
114
115 if( varName.IsEmpty() && !aProjectPath.IsEmpty()
116 && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
117 {
118 envPath.SetPath( aProjectPath );
119
120 if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
121 varName = PROJECT_VAR_NAME;
122 }
123
124 if( varName.IsEmpty() )
125 {
126 normalizedFullPath = aFilePath.GetFullPath();
127 }
128 else
129 {
130 normalizedFullPath = wxString::Format( "${%s}/", varName );
131
132 if( !remainingPath.IsEmpty() )
133 normalizedFullPath += remainingPath;
134
135 normalizedFullPath += aFilePath.GetFullName();
136 }
137
138 return normalizedFullPath;
139}
140
141
142wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
143 const PROJECT* aProject )
144{
145 if( aProject )
146 return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
147 else
148 return NormalizePath( aFilePath, aEnvVars, "" );
149}
150
151
152// Create file path by appending path and file name. This approach allows the filename
153// to contain a relative path, whereas wxFileName::SetPath() would replace the
154// relative path
155static wxString createFilePath( const wxString& aPath, const wxString& aFileName )
156{
157 wxString path( aPath );
158
159 if( !path.EndsWith( wxFileName::GetPathSeparator() ) )
160 path.Append( wxFileName::GetPathSeparator() );
161
162 return path + aFileName;
163}
164
165
166wxString ResolveFile( const wxString& aFileName, const ENV_VAR_MAP* aEnvVars,
167 const PROJECT* aProject )
168{
169 wxFileName full( aFileName );
170
171 if( full.IsAbsolute() )
172 return full.GetFullPath();
173
174 if( aProject )
175 {
176 wxFileName fn( createFilePath( aProject->GetProjectPath(), aFileName ) );
177
178 if( fn.Exists() )
179 return fn.GetFullPath();
180 }
181
182 if( aEnvVars )
183 {
184 for( const std::pair<const wxString, ENV_VAR_ITEM>& entry : *aEnvVars )
185 {
186 wxFileName fn( createFilePath( entry.second.GetValue(), aFileName ) );
187
188 if( fn.Exists() )
189 return fn.GetFullPath();
190 }
191 }
192
193 return wxEmptyString;
194}
195
196
197bool PathIsInsideProject( const wxString& aFileName, const PROJECT* aProject, wxFileName* aSubPath )
198{
199 wxFileName fn( aFileName );
200 wxFileName prj( aProject->GetProjectPath() );
201
202 wxArrayString pdirs = prj.GetDirs();
203 wxArrayString fdirs = fn.GetDirs();
204
205 if( fdirs.size() < pdirs.size() )
206 return false;
207
208 for( size_t i = 0; i < pdirs.size(); i++ )
209 {
210 if( fdirs[i] != pdirs[i] )
211 return false;
212 }
213
214 // Now we know that fn is inside prj
215 if( aSubPath )
216 {
217 aSubPath->Clear();
218
219 for( size_t i = pdirs.size(); i < fdirs.size(); i++ )
220 aSubPath->AppendDir( fdirs[i] );
221 }
222
223 return true;
224}
Container for project specific data.
Definition: project.h:64
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:146
static bool normalizeAbsolutePaths(const wxFileName &aPathA, const wxFileName &aPathB, wxString *aResultPath)
Definition: env_paths.cpp:27
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition: env_paths.cpp:73
wxString ResolveFile(const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
Search the default paths trying to find one with the requested file.
Definition: env_paths.cpp:166
static wxString createFilePath(const wxString &aPath, const wxString &aFileName)
Definition: env_paths.cpp:155
bool PathIsInsideProject(const wxString &aFileName, const PROJECT *aProject, wxFileName *aSubPath)
Definition: env_paths.cpp:197
Helper functions to substitute paths with environmental variables.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:40