Add convenient wxFileName::GetAbsolutePath() wrapper and use it

This wrapper simply combines the calls to MakeAbsolute() and
GetFullPath(), but using it results in shorter and more clear code, so
it seems to be worth having.
This commit is contained in:
Vadim Zeitlin
2021-07-11 14:48:57 +01:00
parent 3539a81a8a
commit f37d449208
14 changed files with 38 additions and 49 deletions

View File

@@ -385,6 +385,15 @@ public:
{ return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
wxPATH_NORM_TILDE, cwd, format); }
// Convenient helper for returning the absolute path corresponding to
// the given one.
wxString GetAbsolutePath(const wxString& cwd = wxEmptyString,
wxPathFormat format = wxPATH_NATIVE) const
{
wxFileName fn(*this);
fn.MakeAbsolute(cwd, format);
return fn.GetFullPath();
}
// If the path is a symbolic link (Unix-only), indicate that all
// filesystem operations on this path should be performed on the link

View File

@@ -370,15 +370,7 @@ protected:
static wxString GetCanonicalPath(const wxFileName& path)
{
wxFileName path_copy = wxFileName(path);
if ( !path_copy.MakeAbsolute() )
{
wxFAIL_MSG(wxString::Format(wxASCII_STR("Unable to normalize path '%s'"),
path.GetFullPath()));
return wxEmptyString;
}
return path_copy.GetFullPath();
return path.GetAbsolutePath();
}

View File

@@ -633,6 +633,17 @@ public:
static wxFileName FileName(const wxString& file,
wxPathFormat format = wxPATH_NATIVE);
/**
Returns full absolute path for this file.
This is just a convenient shortcut using MakeAbsolute() and
GetFullPath() internally.
@since 3.1.6
*/
wxString GetAbsolutePath(const wxString& cwd = wxEmptyString,
wxPathFormat format = wxPATH_NATIVE) const;
/**
Retrieves the value of the current working directory on the specified volume.
If the volume is empty, the program's current working directory is returned for

View File

@@ -574,17 +574,9 @@ MyFrame::MyFrame()
m_fileHistory = new wxFileHistory();
m_fileHistory->UseMenu(m_fileHistoryMenu);
wxFileName fn( "menu.cpp" );
fn.MakeAbsolute();
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
fn = "Makefile.in";
fn.MakeAbsolute();
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
fn.Assign("minimal", "minimal", "cpp");
fn.MakeAbsolute();
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
m_fileHistory->AddFileToHistory( wxFileName("menu.cpp").GetAbsolutePath() );
m_fileHistory->AddFileToHistory( wxFileName("Makefile.in").GetAbsolutePath() );
m_fileHistory->AddFileToHistory( wxFileName("minimal", "minimal", "cpp").GetAbsolutePath() );
fileMenu->AppendSubMenu(m_fileHistoryMenu, "Sample file history");
#endif

View File

@@ -601,8 +601,7 @@ void AppFrame::CreateMenu ()
void AppFrame::FileOpen (wxString fname)
{
wxFileName w(fname); w.MakeAbsolute(); fname = w.GetFullPath();
m_edit->LoadFile (fname);
m_edit->LoadFile (wxFileName(fname).GetAbsolutePath());
m_edit->SelectNone();
}

View File

@@ -748,9 +748,7 @@ void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
pathlist.Add("../help");
pathlist.Add("../../../samples/help");
wxFileName helpfile(pathlist.FindValidPath("doc.zip"));
helpfile.MakeAbsolute();
wxString path = helpfile.GetFullPath();
wxString path = wxFileName(pathlist.FindValidPath("doc.zip")).GetAbsolutePath();
//Under MSW we need to flip the slashes
path.Replace("\\", "/");
path = "wxfs:///" + path + ";protocol=zip/doc.htm";

View File

@@ -2631,9 +2631,7 @@ static wxString EscapeFileNameCharsInURL(const char *in)
// Returns the file URL for a native path
wxString wxFileName::FileNameToURL(const wxFileName& filename)
{
wxFileName fn = filename;
fn.MakeAbsolute();
wxString url = fn.GetFullPath(wxPATH_NATIVE);
wxString url = filename.GetAbsolutePath(wxString(), wxPATH_NATIVE);
#ifndef __UNIX__
// unc notation, wxMSW

View File

@@ -77,9 +77,7 @@ wxString wxStandardPathsBase::GetExecutablePath() const
if ( path.empty() )
return argv0; // better than nothing
wxFileName filename(path);
filename.MakeAbsolute();
return filename.GetFullPath();
return wxFileName(path).GetAbsolutePath();
}
wxStandardPaths& wxAppTraitsBase::GetStandardPaths()

View File

@@ -429,9 +429,7 @@ void wxFileDialog::SetPath(const wxString& path)
// we need an absolute path for GTK native chooser so ensure that we have
// it: use the initial directory if it was set or just CWD otherwise (this
// is the default behaviour if m_dir is empty)
wxFileName fn(path);
fn.MakeAbsolute(m_dir);
m_fc.SetPath(fn.GetFullPath());
m_fc.SetPath(wxFileName(path).GetAbsolutePath(m_dir));
}
void wxFileDialog::SetDirectory(const wxString& dir)

View File

@@ -817,9 +817,7 @@ wxFSFile* wxChmFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
// now work on the right location
if (right.Contains(wxT("..")))
{
wxFileName abs(right);
abs.MakeAbsolute(wxT("/"));
right = abs.GetFullPath();
right = wxFileName(right).GetAbsolutePath(wxT("/"));
}
// a workaround for absolute links to root

View File

@@ -314,12 +314,7 @@ wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
{
// Make the name absolute filename, because the app may
// change working directory later:
wxFileName fn(fnd);
if (fn.IsRelative())
{
fn.MakeAbsolute();
fnd = fn.GetFullPath();
}
fnd = wxFileName(fnd).GetAbsolutePath();
#if wxUSE_FILESYSTEM
fnd = wxFileSystem::FileNameToURL(fnd);
#endif

View File

@@ -356,6 +356,11 @@ TEST_CASE("wxFileName::Normalize", "[filename]")
);
}
// Check that paths are made absolute, but environment variables are not
// expanded in them.
CHECK( wxFileName(pathWithEnvVar).GetAbsolutePath()
== wxFileName(wxGetCwd() + "/" + pathWithEnvVar).GetFullPath() );
// MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
// names generation is not disabled for this system as otherwise the file
// MKINST~1 doesn't exist at all and normalizing it fails (it's possible

View File

@@ -44,9 +44,7 @@ wxString AutoCaptureMechanism::default_dir = wxT("screenshots");
/* static */
wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath()
{
wxFileName output = wxFileName::DirName(GetDefaultDirectory());
output.MakeAbsolute();
return output.GetFullPath();
return wxFileName::DirName(GetDefaultDirectory()).GetAbsolutePath();
}
/* static */

View File

@@ -337,9 +337,7 @@ void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
}
if (!parOutput.empty())
{
wxFileName fn(parOutput);
fn.MakeAbsolute();
parOutput = fn.GetFullPath();
parOutput = wxFileName(parOutput).GetAbsolutePath();
parOutputPath = wxPathOnly(parOutput);
}
if (!parOutputPath) parOutputPath = wxT(".");