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

142{
143 if( aProject )
144 return NormalizePath( aFilePath, aEnvVars, aProject->GetProjectPath() );
145 else
146 return NormalizePath( aFilePath, aEnvVars, "" );
147}
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:71

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

73{
74 wxFileName envPath;
75 wxString varName;
76 wxString remainingPath;
77 wxString normalizedFullPath;
78 int pathDepth = 0;
79
80 if( aEnvVars )
81 {
82 for( const std::pair<const wxString, ENV_VAR_ITEM>& entry : *aEnvVars )
83 {
84 // Don't bother normalizing paths that don't exist or the user cannot read.
85 if( !wxFileName::DirExists( entry.second.GetValue() )
86 || !wxFileName::IsDirReadable( entry.second.GetValue() ) )
87 {
88 continue;
89 }
90
91 envPath.SetPath( entry.second.GetValue() );
92
93 wxString tmp;
94
95 if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
96 {
97 int newDepth = envPath.GetDirs().GetCount();
98
99 // Only use the variable if it removes more directories than the previous ones
100 if( newDepth > pathDepth )
101 {
102 pathDepth = newDepth;
103 varName = entry.first;
104 remainingPath = tmp;
105 }
106
107 // @fixme Shouldn't we break here if an environment variable path is found or try
108 // at least try to pick the best match?
109 }
110 }
111 }
112
113 if( varName.IsEmpty() && !aProjectPath.IsEmpty()
114 && wxFileName( aProjectPath ).IsAbsolute() && wxFileName( aFilePath ).IsAbsolute() )
115 {
116 envPath.SetPath( aProjectPath );
117
118 if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
119 varName = PROJECT_VAR_NAME;
120 }
121
122 if( varName.IsEmpty() )
123 {
124 normalizedFullPath = aFilePath.GetFullPath();
125 }
126 else
127 {
128 normalizedFullPath = wxString::Format( "${%s}/", varName );
129
130 if( !remainingPath.IsEmpty() )
131 normalizedFullPath += remainingPath;
132
133 normalizedFullPath += aFilePath.GetFullName();
134 }
135
136 return normalizedFullPath;
137}
static bool normalizeAbsolutePaths(const wxFileName &aPathA, const wxFileName &aPathB, wxString *aResultPath)
Definition: env_paths.cpp:27
#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

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

Referenced by SYMBOL_LIBRARY_MANAGER::addLibrary(), PCB_BASE_EDIT_FRAME::AddLibrary(), SYMBOL_EDIT_FRAME::addLibTableEntry(), PANEL_FP_LIB_TABLE::browseLibrariesHandler(), PANEL_SYM_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 164 of file env_paths.cpp.

166{
167 wxFileName full( aFileName );
168
169 if( full.IsAbsolute() )
170 return full.GetFullPath();
171
172 if( aProject )
173 {
174 wxFileName fn( createFilePath( aProject->GetProjectPath(), aFileName ) );
175
176 if( fn.Exists() )
177 return fn.GetFullPath();
178 }
179
180 if( aEnvVars )
181 {
182 for( const std::pair<const wxString, ENV_VAR_ITEM>& entry : *aEnvVars )
183 {
184 wxFileName fn( createFilePath( entry.second.GetValue(), aFileName ) );
185
186 if( fn.Exists() )
187 return fn.GetFullPath();
188 }
189 }
190
191 return wxEmptyString;
192}
static wxString createFilePath(const wxString &aPath, const wxString &aFileName)
Definition: env_paths.cpp:153

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

Referenced by NETLIST_EXPORTER_SPICE::writeInclude().