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