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...
 

Function Documentation

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

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

116 {
117  if( aProject )
118  return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
119  else
120  return NormalizePath( aFilePath, aEnvVars, "" );
121 }
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:67
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 67 of file env_paths.cpp.

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().

69 {
70  wxFileName envPath;
71  wxString tmp, varName, normalizedFullPath;
72 
73  if( aEnvVars )
74  {
75  for( auto& entry : *aEnvVars )
76  {
77  // Don't bother normalizing paths that don't exist or the user cannot read.
78  if( !wxFileName::DirExists( entry.second.GetValue() )
79  || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
80  continue;
81 
82  envPath.SetPath( entry.second.GetValue() );
83 
84  if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
85  {
86  varName = entry.first;
87  break;
88  }
89  }
90  }
91 
92  if( varName.IsEmpty() && !aProjectPath.IsEmpty()
93  && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
94  {
95  envPath.SetPath( aProjectPath );
96 
97  if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
98  varName = PROJECT_VAR_NAME;
99  }
100 
101  if( !varName.IsEmpty() )
102  {
103  normalizedFullPath = wxString::Format( "${%s}/", varName );
104 
105  if( !tmp.IsEmpty() )
106  normalizedFullPath += tmp;
107 
108  normalizedFullPath += aFilePath.GetFullName();
109  }
110 
111  return normalizedFullPath;
112 }
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:37
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
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 138 of file env_paths.cpp.

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

Referenced by NETLIST_EXPORTER_PSPICE::Format().

140 {
141  wxFileName full( aFileName );
142 
143  if( full.IsAbsolute() )
144  return full.GetFullPath();
145 
146  if( aProject )
147  {
148  wxFileName fn( createFilePath( aProject->GetProjectPath(), aFileName ) );
149 
150  if( fn.Exists() )
151  return fn.GetFullPath();
152  }
153 
154  if( aEnvVars )
155  {
156  for( auto& entry : *aEnvVars )
157  {
158  wxFileName fn( createFilePath( entry.second.GetValue(), aFileName ) );
159 
160  if( fn.Exists() )
161  return fn.GetFullPath();
162  }
163  }
164 
165  return wxEmptyString;
166 }
static wxString createFilePath(const wxString &aPath, const wxString &aFileName)
Definition: env_paths.cpp:127
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102