KiCad PCB EDA Suite
env_paths.cpp File Reference
#include <env_paths.h>

Go to the source code of this file.

Functions

static bool normalizeAbsolutePaths (const wxFileName &aPathA, const wxFileName &aPathB, wxString *aResultPath)
 
wxString NormalizePath (const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
 Normalize a file path to an environmental variable, if possible. More...
 
wxString NormalizePath (const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
 < Helper functions to substitute paths with environmental variables. More...
 
static wxString createFilePath (const wxString &aPath, const wxString &aFileName)
 
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. More...
 
bool PathIsInsideProject (const wxString &aFileName, const PROJECT *aProject, wxFileName *aSubPath)
 Check if a given filename is within a given project directory (not whether it exists!) More...
 

Function Documentation

◆ createFilePath()

static wxString createFilePath ( const wxString &  aPath,
const wxString &  aFileName 
)
static

Definition at line 139 of file env_paths.cpp.

140 {
141  wxString path( aPath );
142 
143  if( !path.EndsWith( wxFileName::GetPathSeparator() ) )
144  path.Append( wxFileName::GetPathSeparator() );
145 
146  return path + aFileName;
147 }

References path.

Referenced by ResolveFile().

◆ normalizeAbsolutePaths()

static bool normalizeAbsolutePaths ( const wxFileName &  aPathA,
const wxFileName &  aPathB,
wxString *  aResultPath 
)
static

Definition at line 25 of file env_paths.cpp.

27 {
28  wxCHECK_MSG( aPathA.IsAbsolute(), false, aPathA.GetPath() + " is not an absolute path." );
29  wxCHECK_MSG( aPathB.IsAbsolute(), false, aPathB.GetPath() + " is not an absolute path." );
30 
31  if( aPathA.GetPath() == aPathB.GetPath() )
32  return true;
33 
34  if( ( aPathA.GetDirCount() > aPathB.GetDirCount() )
35  || ( aPathA.HasVolume() && !aPathB.HasVolume() )
36  || ( !aPathA.HasVolume() && aPathB.HasVolume() )
37  || ( ( aPathA.HasVolume() && aPathB.HasVolume() )
38  && ( aPathA.GetVolume() != aPathB.GetVolume() ) ) )
39  return false;
40 
41  wxArrayString aDirs = aPathA.GetDirs();
42  wxArrayString bDirs = aPathB.GetDirs();
43 
44  size_t i = 0;
45 
46  while( i < aDirs.GetCount() )
47  {
48  if( aDirs[i] != bDirs[i] )
49  return false;
50 
51  i++;
52  }
53 
54  if( aResultPath )
55  {
56  while( i < bDirs.GetCount() )
57  {
58  *aResultPath += bDirs[i] + wxT( "/" );
59  i++;
60  }
61  }
62 
63  return true;
64 }

Referenced by NormalizePath().

◆ NormalizePath() [1/2]

wxString NormalizePath ( const wxFileName &  aFilePath,
const ENV_VAR_MAP aEnvVars,
const wxString &  aProjectPath 
)

Normalize a file path to an environmental variable, if possible.

Parameters
aFilePathis the full file path (path and file name) to be normalized.
aEnvVarsis an optional map of environmental variables to try substitution with.
aProjectPathis an optional string to normalize the file path to the project path.
Returns
Normalized full file path (path and file name) if succeeded or empty string if the path could not be normalized.

Definition at line 67 of file env_paths.cpp.

69 {
70  wxFileName envPath;
71  wxString varName;
72  wxString remainingPath;
73  wxString normalizedFullPath;
74  int pathDepth = 0;
75 
76  if( aEnvVars )
77  {
78  for( auto& entry : *aEnvVars )
79  {
80  // Don't bother normalizing paths that don't exist or the user cannot read.
81  if( !wxFileName::DirExists( entry.second.GetValue() )
82  || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
83  continue;
84 
85  envPath.SetPath( entry.second.GetValue() );
86 
87  wxString tmp;
88  if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
89  {
90  int newDepth = envPath.GetDirs().GetCount();
91 
92  // Only use the variable if it removes more directories than the previous ones
93  if( newDepth > pathDepth )
94  {
95  pathDepth = newDepth;
96  varName = entry.first;
97  remainingPath = tmp;
98  }
99  }
100  }
101  }
102 
103  if( varName.IsEmpty() && !aProjectPath.IsEmpty()
104  && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
105  {
106  envPath.SetPath( aProjectPath );
107 
108  if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
109  varName = PROJECT_VAR_NAME;
110  }
111 
112  if( !varName.IsEmpty() )
113  {
114  normalizedFullPath = wxString::Format( "${%s}/", varName );
115 
116  if( !remainingPath.IsEmpty() )
117  normalizedFullPath += remainingPath;
118 
119  normalizedFullPath += aFilePath.GetFullName();
120  }
121 
122  return normalizedFullPath;
123 }
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
static bool normalizeAbsolutePaths(const wxFileName &aPathA, const wxFileName &aPathB, wxString *aResultPath)
Definition: env_paths.cpp:25

References Format(), normalizeAbsolutePaths(), and PROJECT_VAR_NAME.

Referenced by PCB_BASE_EDIT_FRAME::AddLibrary(), SYMBOL_LIBRARY_MANAGER::addLibrary(), SYMBOL_EDIT_FRAME::addLibTableEntry(), PANEL_SYM_LIB_TABLE::browseLibrariesHandler(), PANEL_FP_LIB_TABLE::browseLibrariesHandler(), DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable(), NormalizePath(), TEXT_BUTTON_FILE_BROWSER::OnButtonClick(), PANEL_SYM_LIB_TABLE::onConvertLegacyLibraries(), and SYMBOL_EDIT_FRAME::replaceLibTableEntry().

◆ NormalizePath() [2/2]

wxString NormalizePath ( const wxFileName &  aFilePath,
const ENV_VAR_MAP aEnvVars,
const PROJECT aProject 
)

< Helper functions to substitute paths with environmental variables.

Normalize a file path to an environmental variable, if possible.

Parameters
aFilePathis the full file path (path and file name) to be normalized.
aEnvVarsis an optional map of environmental variables to try substitution with.
aProjectis an optional project, to normalize the file path to the project path.
Returns
Normalized full file path (path and file name) if succeeded or empty string if the path could not be normalized.

Definition at line 126 of file env_paths.cpp.

128 {
129  if( aProject )
130  return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
131  else
132  return NormalizePath( aFilePath, aEnvVars, "" );
133 }
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:123
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:67

References PROJECT::GetProjectPath(), and NormalizePath().

◆ PathIsInsideProject()

bool PathIsInsideProject ( const wxString &  aFileName,
const PROJECT aProject,
wxFileName *  aSubPath = nullptr 
)

Check if a given filename is within a given project directory (not whether it exists!)

Parameters
aFileNameis the absolute path to check
aProjectis the project to test against
aSubPathwill be filled with the relative path to the file inside the project (if any)
Returns
true if aFileName's path is inside aProject's path

Definition at line 181 of file env_paths.cpp.

182 {
183  wxFileName fn( aFileName );
184  wxFileName prj( aProject->GetProjectPath() );
185 
186  wxArrayString pdirs = prj.GetDirs();
187  wxArrayString fdirs = fn.GetDirs();
188 
189  if( fdirs.size() < pdirs.size() )
190  return false;
191 
192  for( size_t i = 0; i < pdirs.size(); i++ )
193  {
194  if( fdirs[i] != pdirs[i] )
195  return false;
196  }
197 
198  // Now we know that fn is inside prj
199  if( aSubPath )
200  {
201  aSubPath->Clear();
202 
203  for( size_t i = pdirs.size(); i < fdirs.size(); i++ )
204  aSubPath->AppendDir( fdirs[i] );
205  }
206 
207  return true;
208 }
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:123

References PROJECT::GetProjectPath().

◆ ResolveFile()

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.

Parameters
aFileNameis the name of the searched file. It might be a relative path.
aEnvVarsis an optional map of environmental variables that can contain paths.
aProjectis an optional project, to check the project path.
Returns
Full path (apth and file name) if the file was found in one of the paths, otherwise an empty string.

Definition at line 150 of file env_paths.cpp.

152 {
153  wxFileName full( aFileName );
154 
155  if( full.IsAbsolute() )
156  return full.GetFullPath();
157 
158  if( aProject )
159  {
160  wxFileName fn( createFilePath( aProject->GetProjectPath(), aFileName ) );
161 
162  if( fn.Exists() )
163  return fn.GetFullPath();
164  }
165 
166  if( aEnvVars )
167  {
168  for( auto& entry : *aEnvVars )
169  {
170  wxFileName fn( createFilePath( entry.second.GetValue(), aFileName ) );
171 
172  if( fn.Exists() )
173  return fn.GetFullPath();
174  }
175  }
176 
177  return wxEmptyString;
178 }
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:123
static wxString createFilePath(const wxString &aPath, const wxString &aFileName)
Definition: env_paths.cpp:139

References createFilePath(), and PROJECT::GetProjectPath().

Referenced by NETLIST_EXPORTER_PSPICE::Format().