KiCad PCB EDA Suite
env_paths.h File Reference
#include <wx/string.h>
#include <settings/environment.h>

Go to the source code of this file.

Functions

wxString NormalizePath (const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
 Normalize a file path to an environmental variable, if possible. More...
 
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 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...
 

Function Documentation

◆ NormalizePath() [1/2]

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

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 the input path if the path could not be normalized.

Definition at line 132 of file env_paths.cpp.

134 {
135  if( aProject )
136  return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
137  else
138  return NormalizePath( aFilePath, aEnvVars, "" );
139 }
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
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:69

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

◆ NormalizePath() [2/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 the input path if the path could not be normalized.

Definition at line 69 of file env_paths.cpp.

71 {
72  wxFileName envPath;
73  wxString varName;
74  wxString remainingPath;
75  wxString normalizedFullPath;
76  int pathDepth = 0;
77 
78  if( aEnvVars )
79  {
80  for( auto& entry : *aEnvVars )
81  {
82  // Don't bother normalizing paths that don't exist or the user cannot read.
83  if( !wxFileName::DirExists( entry.second.GetValue() )
84  || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
85  continue;
86 
87  envPath.SetPath( entry.second.GetValue() );
88 
89  wxString tmp;
90  if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
91  {
92  int newDepth = envPath.GetDirs().GetCount();
93 
94  // Only use the variable if it removes more directories than the previous ones
95  if( newDepth > pathDepth )
96  {
97  pathDepth = newDepth;
98  varName = entry.first;
99  remainingPath = tmp;
100  }
101  }
102  }
103  }
104 
105  if( varName.IsEmpty() && !aProjectPath.IsEmpty()
106  && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
107  {
108  envPath.SetPath( aProjectPath );
109 
110  if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
111  varName = PROJECT_VAR_NAME;
112  }
113 
114  if( varName.IsEmpty() )
115  {
116  normalizedFullPath = aFilePath.GetFullPath();
117  }
118  else
119  {
120  normalizedFullPath = wxString::Format( "${%s}/", varName );
121 
122  if( !remainingPath.IsEmpty() )
123  normalizedFullPath += remainingPath;
124 
125  normalizedFullPath += aFilePath.GetFullName();
126  }
127 
128  return normalizedFullPath;
129 }
#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:27

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(), DIALOG_PAGES_SETTINGS::OnWksFileSelection(), and SYMBOL_EDIT_FRAME::replaceLibTableEntry().

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

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

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

Referenced by NETLIST_EXPORTER_PSPICE::Format().