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

Go to the source code of this file.

Functions

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

Helper functions to substitute paths with environmental variables.

More...
 
wxString NormalizePath (const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
 Normalizes a file path to an environmental variable, if possible. More...
 
wxString ResolveFile (const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
 Searches the default paths trying to find one with the requested file. More...
 
bool PathIsInsideProject (const wxString &aFileName, const PROJECT *aProject, wxFileName *aSubPath=nullptr)
 Checks if a given filename is within a given project directory (not whether it exists!) More...
 

Function Documentation

◆ NormalizePath() [1/2]

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

Helper functions to substitute paths with environmental variables.

Normalizes 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 substition 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 127 of file env_paths.cpp.

129 {
130  if( aProject )
131  return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
132  else
133  return NormalizePath( aFilePath, aEnvVars, "" );
134 }
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:123
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalizes a file path to an environmental variable, if possible.
Definition: env_paths.cpp:68

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

◆ NormalizePath() [2/2]

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

Normalizes 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 substition 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 68 of file env_paths.cpp.

70 {
71  wxFileName envPath;
72  wxString varName;
73  wxString remainingPath;
74  wxString normalizedFullPath;
75  int pathDepth = 0;
76 
77  if( aEnvVars )
78  {
79  for( auto& entry : *aEnvVars )
80  {
81  // Don't bother normalizing paths that don't exist or the user cannot read.
82  if( !wxFileName::DirExists( entry.second.GetValue() )
83  || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
84  continue;
85 
86  envPath.SetPath( entry.second.GetValue() );
87 
88  wxString tmp;
89  if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
90  {
91  int newDepth = envPath.GetDirs().GetCount();
92 
93  // Only use the variable if it removes more directories than the previous ones
94  if( newDepth > pathDepth )
95  {
96  pathDepth = newDepth;
97  varName = entry.first;
98  remainingPath = tmp;
99  }
100  }
101  }
102  }
103 
104  if( varName.IsEmpty() && !aProjectPath.IsEmpty()
105  && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
106  {
107  envPath.SetPath( aProjectPath );
108 
109  if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
110  varName = PROJECT_VAR_NAME;
111  }
112 
113  if( !varName.IsEmpty() )
114  {
115  normalizedFullPath = wxString::Format( "${%s}/", varName );
116 
117  if( !remainingPath.IsEmpty() )
118  normalizedFullPath += remainingPath;
119 
120  normalizedFullPath += aFilePath.GetFullName();
121  }
122 
123  return normalizedFullPath;
124 }
#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, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
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(), LIB_MANAGER::addLibrary(), PANEL_SYM_LIB_TABLE::browseLibrariesHandler(), PANEL_FP_LIB_TABLE::browseLibrariesHandler(), DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable(), and NormalizePath().

◆ PathIsInsideProject()

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

Checks 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 agains
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 182 of file env_paths.cpp.

183 {
184  wxFileName fn( aFileName );
185  wxFileName prj( aProject->GetProjectPath() );
186 
187  wxArrayString pdirs = prj.GetDirs();
188  wxArrayString fdirs = fn.GetDirs();
189 
190  if( fdirs.size() < pdirs.size() )
191  return false;
192 
193  for( size_t i = 0; i < pdirs.size(); i++ )
194  {
195  if( fdirs[i] != pdirs[i] )
196  return false;
197  }
198 
199  // Now we know that fn is inside prj
200  if( aSubPath )
201  {
202  aSubPath->Clear();
203 
204  for( size_t i = pdirs.size(); i < fdirs.size(); i++ )
205  aSubPath->AppendDir( fdirs[i] );
206  }
207 
208  return true;
209 }
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns 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 
)

Searches 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 151 of file env_paths.cpp.

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

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

Referenced by NETLIST_EXPORTER_PSPICE::Format().