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)
 Normalizes 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)
 Searches the default paths trying to find one with the requested file. More...
 

Function Documentation

◆ createFilePath()

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

Definition at line 140 of file env_paths.cpp.

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

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.

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

Referenced by NormalizePath().

◆ NormalizePath() [1/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().

◆ NormalizePath() [2/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:102
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().

◆ 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:102
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().