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:
@@ -385,6 +385,15 @@ public:
|
|||||||
{ return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
|
{ return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
|
||||||
wxPATH_NORM_TILDE, cwd, format); }
|
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
|
// If the path is a symbolic link (Unix-only), indicate that all
|
||||||
// filesystem operations on this path should be performed on the link
|
// filesystem operations on this path should be performed on the link
|
||||||
|
@@ -370,15 +370,7 @@ protected:
|
|||||||
|
|
||||||
static wxString GetCanonicalPath(const wxFileName& path)
|
static wxString GetCanonicalPath(const wxFileName& path)
|
||||||
{
|
{
|
||||||
wxFileName path_copy = wxFileName(path);
|
return path.GetAbsolutePath();
|
||||||
if ( !path_copy.MakeAbsolute() )
|
|
||||||
{
|
|
||||||
wxFAIL_MSG(wxString::Format(wxASCII_STR("Unable to normalize path '%s'"),
|
|
||||||
path.GetFullPath()));
|
|
||||||
return wxEmptyString;
|
|
||||||
}
|
|
||||||
|
|
||||||
return path_copy.GetFullPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -633,6 +633,17 @@ public:
|
|||||||
static wxFileName FileName(const wxString& file,
|
static wxFileName FileName(const wxString& file,
|
||||||
wxPathFormat format = wxPATH_NATIVE);
|
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.
|
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
|
If the volume is empty, the program's current working directory is returned for
|
||||||
|
@@ -574,17 +574,9 @@ MyFrame::MyFrame()
|
|||||||
m_fileHistory = new wxFileHistory();
|
m_fileHistory = new wxFileHistory();
|
||||||
m_fileHistory->UseMenu(m_fileHistoryMenu);
|
m_fileHistory->UseMenu(m_fileHistoryMenu);
|
||||||
|
|
||||||
wxFileName fn( "menu.cpp" );
|
m_fileHistory->AddFileToHistory( wxFileName("menu.cpp").GetAbsolutePath() );
|
||||||
fn.MakeAbsolute();
|
m_fileHistory->AddFileToHistory( wxFileName("Makefile.in").GetAbsolutePath() );
|
||||||
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
|
m_fileHistory->AddFileToHistory( wxFileName("minimal", "minimal", "cpp").GetAbsolutePath() );
|
||||||
|
|
||||||
fn = "Makefile.in";
|
|
||||||
fn.MakeAbsolute();
|
|
||||||
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
|
|
||||||
|
|
||||||
fn.Assign("minimal", "minimal", "cpp");
|
|
||||||
fn.MakeAbsolute();
|
|
||||||
m_fileHistory->AddFileToHistory( fn.GetFullPath() );
|
|
||||||
|
|
||||||
fileMenu->AppendSubMenu(m_fileHistoryMenu, "Sample file history");
|
fileMenu->AppendSubMenu(m_fileHistoryMenu, "Sample file history");
|
||||||
#endif
|
#endif
|
||||||
|
@@ -601,8 +601,7 @@ void AppFrame::CreateMenu ()
|
|||||||
|
|
||||||
void AppFrame::FileOpen (wxString fname)
|
void AppFrame::FileOpen (wxString fname)
|
||||||
{
|
{
|
||||||
wxFileName w(fname); w.MakeAbsolute(); fname = w.GetFullPath();
|
m_edit->LoadFile (wxFileName(fname).GetAbsolutePath());
|
||||||
m_edit->LoadFile (fname);
|
|
||||||
m_edit->SelectNone();
|
m_edit->SelectNone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -748,9 +748,7 @@ void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
|
|||||||
pathlist.Add("../help");
|
pathlist.Add("../help");
|
||||||
pathlist.Add("../../../samples/help");
|
pathlist.Add("../../../samples/help");
|
||||||
|
|
||||||
wxFileName helpfile(pathlist.FindValidPath("doc.zip"));
|
wxString path = wxFileName(pathlist.FindValidPath("doc.zip")).GetAbsolutePath();
|
||||||
helpfile.MakeAbsolute();
|
|
||||||
wxString path = helpfile.GetFullPath();
|
|
||||||
//Under MSW we need to flip the slashes
|
//Under MSW we need to flip the slashes
|
||||||
path.Replace("\\", "/");
|
path.Replace("\\", "/");
|
||||||
path = "wxfs:///" + path + ";protocol=zip/doc.htm";
|
path = "wxfs:///" + path + ";protocol=zip/doc.htm";
|
||||||
|
@@ -2631,9 +2631,7 @@ static wxString EscapeFileNameCharsInURL(const char *in)
|
|||||||
// Returns the file URL for a native path
|
// Returns the file URL for a native path
|
||||||
wxString wxFileName::FileNameToURL(const wxFileName& filename)
|
wxString wxFileName::FileNameToURL(const wxFileName& filename)
|
||||||
{
|
{
|
||||||
wxFileName fn = filename;
|
wxString url = filename.GetAbsolutePath(wxString(), wxPATH_NATIVE);
|
||||||
fn.MakeAbsolute();
|
|
||||||
wxString url = fn.GetFullPath(wxPATH_NATIVE);
|
|
||||||
|
|
||||||
#ifndef __UNIX__
|
#ifndef __UNIX__
|
||||||
// unc notation, wxMSW
|
// unc notation, wxMSW
|
||||||
|
@@ -77,9 +77,7 @@ wxString wxStandardPathsBase::GetExecutablePath() const
|
|||||||
if ( path.empty() )
|
if ( path.empty() )
|
||||||
return argv0; // better than nothing
|
return argv0; // better than nothing
|
||||||
|
|
||||||
wxFileName filename(path);
|
return wxFileName(path).GetAbsolutePath();
|
||||||
filename.MakeAbsolute();
|
|
||||||
return filename.GetFullPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxStandardPaths& wxAppTraitsBase::GetStandardPaths()
|
wxStandardPaths& wxAppTraitsBase::GetStandardPaths()
|
||||||
|
@@ -429,9 +429,7 @@ void wxFileDialog::SetPath(const wxString& path)
|
|||||||
// we need an absolute path for GTK native chooser so ensure that we have
|
// 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
|
// it: use the initial directory if it was set or just CWD otherwise (this
|
||||||
// is the default behaviour if m_dir is empty)
|
// is the default behaviour if m_dir is empty)
|
||||||
wxFileName fn(path);
|
m_fc.SetPath(wxFileName(path).GetAbsolutePath(m_dir));
|
||||||
fn.MakeAbsolute(m_dir);
|
|
||||||
m_fc.SetPath(fn.GetFullPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxFileDialog::SetDirectory(const wxString& dir)
|
void wxFileDialog::SetDirectory(const wxString& dir)
|
||||||
|
@@ -817,9 +817,7 @@ wxFSFile* wxChmFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
|
|||||||
// now work on the right location
|
// now work on the right location
|
||||||
if (right.Contains(wxT("..")))
|
if (right.Contains(wxT("..")))
|
||||||
{
|
{
|
||||||
wxFileName abs(right);
|
right = wxFileName(right).GetAbsolutePath(wxT("/"));
|
||||||
abs.MakeAbsolute(wxT("/"));
|
|
||||||
right = abs.GetFullPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// a workaround for absolute links to root
|
// a workaround for absolute links to root
|
||||||
|
@@ -314,12 +314,7 @@ wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
|
|||||||
{
|
{
|
||||||
// Make the name absolute filename, because the app may
|
// Make the name absolute filename, because the app may
|
||||||
// change working directory later:
|
// change working directory later:
|
||||||
wxFileName fn(fnd);
|
fnd = wxFileName(fnd).GetAbsolutePath();
|
||||||
if (fn.IsRelative())
|
|
||||||
{
|
|
||||||
fn.MakeAbsolute();
|
|
||||||
fnd = fn.GetFullPath();
|
|
||||||
}
|
|
||||||
#if wxUSE_FILESYSTEM
|
#if wxUSE_FILESYSTEM
|
||||||
fnd = wxFileSystem::FileNameToURL(fnd);
|
fnd = wxFileSystem::FileNameToURL(fnd);
|
||||||
#endif
|
#endif
|
||||||
|
@@ -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
|
// 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
|
// 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
|
// MKINST~1 doesn't exist at all and normalizing it fails (it's possible
|
||||||
|
@@ -44,9 +44,7 @@ wxString AutoCaptureMechanism::default_dir = wxT("screenshots");
|
|||||||
/* static */
|
/* static */
|
||||||
wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath()
|
wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath()
|
||||||
{
|
{
|
||||||
wxFileName output = wxFileName::DirName(GetDefaultDirectory());
|
return wxFileName::DirName(GetDefaultDirectory()).GetAbsolutePath();
|
||||||
output.MakeAbsolute();
|
|
||||||
return output.GetFullPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */
|
/* static */
|
||||||
|
@@ -337,9 +337,7 @@ void XmlResApp::ParseParams(const wxCmdLineParser& cmdline)
|
|||||||
}
|
}
|
||||||
if (!parOutput.empty())
|
if (!parOutput.empty())
|
||||||
{
|
{
|
||||||
wxFileName fn(parOutput);
|
parOutput = wxFileName(parOutput).GetAbsolutePath();
|
||||||
fn.MakeAbsolute();
|
|
||||||
parOutput = fn.GetFullPath();
|
|
||||||
parOutputPath = wxPathOnly(parOutput);
|
parOutputPath = wxPathOnly(parOutput);
|
||||||
}
|
}
|
||||||
if (!parOutputPath) parOutputPath = wxT(".");
|
if (!parOutputPath) parOutputPath = wxT(".");
|
||||||
|
Reference in New Issue
Block a user