diff --git a/include/wx/filename.h b/include/wx/filename.h index 3906804521..f592996548 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -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 diff --git a/include/wx/fswatcher.h b/include/wx/fswatcher.h index 5870c6cb35..2030ee5434 100644 --- a/include/wx/fswatcher.h +++ b/include/wx/fswatcher.h @@ -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(); } diff --git a/interface/wx/filename.h b/interface/wx/filename.h index 3c4842a126..7dc34abdac 100644 --- a/interface/wx/filename.h +++ b/interface/wx/filename.h @@ -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 diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index f325fbf07c..6387705fc4 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -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 diff --git a/samples/stc/stctest.cpp b/samples/stc/stctest.cpp index be31fbacaf..de078dba04 100644 --- a/samples/stc/stctest.cpp +++ b/samples/stc/stctest.cpp @@ -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(); } diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index c8d585a2de..0d9148c5d5 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -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"; diff --git a/src/common/filename.cpp b/src/common/filename.cpp index f54144cd9b..d31d3a5f35 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -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 diff --git a/src/common/stdpbase.cpp b/src/common/stdpbase.cpp index 5ba2a9c80f..e78fd0c28e 100644 --- a/src/common/stdpbase.cpp +++ b/src/common/stdpbase.cpp @@ -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() diff --git a/src/gtk/filedlg.cpp b/src/gtk/filedlg.cpp index 6ab3fcae7c..c68c7c7256 100644 --- a/src/gtk/filedlg.cpp +++ b/src/gtk/filedlg.cpp @@ -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) diff --git a/src/html/chm.cpp b/src/html/chm.cpp index 2d77d88f95..7927d282d6 100644 --- a/src/html/chm.cpp +++ b/src/html/chm.cpp @@ -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 diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 65918e929f..046b0004c4 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -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 diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 4e59855b26..3786cdc442 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -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 diff --git a/utils/screenshotgen/src/autocapture.cpp b/utils/screenshotgen/src/autocapture.cpp index 427c26af8a..275b428fb2 100644 --- a/utils/screenshotgen/src/autocapture.cpp +++ b/utils/screenshotgen/src/autocapture.cpp @@ -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 */ diff --git a/utils/wxrc/wxrc.cpp b/utils/wxrc/wxrc.cpp index 1269d7ce67..348525f13c 100644 --- a/utils/wxrc/wxrc.cpp +++ b/utils/wxrc/wxrc.cpp @@ -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(".");