From: Vadim Zeitlin Date: Sun, 16 Apr 2006 21:14:00 +0000 (+0000) Subject: added overwrite parameter to wxRenameFile (patch 1467041) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/57e988b8ef1b1a845ee088a04d860c1ef79235ca added overwrite parameter to wxRenameFile (patch 1467041) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38760 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index 7addfe5654..0164792561 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -1215,10 +1215,14 @@ Removes \arg{file}, returning true if successful. \membersection{::wxRenameFile}\label{wxrenamefile} -\func{bool}{wxRenameFile}{\param{const wxString\& }{file1}, \param{const wxString\& }{file2}} +\func{bool}{wxRenameFile}{\param{const wxString\& }{file1}, \param{const wxString\& }{file2}, \param{bool overwrite = true}} Renames \arg{file1} to \arg{file2}, returning true if successful. +If \arg{overwrite} parameter is true (default), the destination file is +overwritten if it exists, but if \arg{overwrite} is false, the functions fails +in this case. + \membersection{::wxRmdir}\label{wxrmdir} diff --git a/include/wx/filefn.h b/include/wx/filefn.h index 4189de1bf1..3351115ba7 100644 --- a/include/wx/filefn.h +++ b/include/wx/filefn.h @@ -436,7 +436,7 @@ WXDLLIMPEXP_BASE bool wxCopyFile(const wxString& file1, const wxString& file2, WXDLLIMPEXP_BASE bool wxRemoveFile(const wxString& file); // Rename file -WXDLLIMPEXP_BASE bool wxRenameFile(const wxString& file1, const wxString& file2); +WXDLLIMPEXP_BASE bool wxRenameFile(const wxString& file1, const wxString& file2, bool overwrite = true); // Get current working directory. #if WXWIN_COMPATIBILITY_2_6 diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index 5168a4cff5..4bf2c7cdd5 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -1106,8 +1106,19 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) } bool -wxRenameFile (const wxString& file1, const wxString& file2) +wxRenameFile(const wxString& file1, const wxString& file2, bool overwrite) { + if ( !overwrite && wxFileExists(file2) ) + { + wxLogSysError + ( + _("Failed to rename the file '%s' to '%s' because the destination file already exists."), + file1.c_str(), file2.c_str() + ); + + return false; + } + #if !defined(__WXWINCE__) && !defined(__WXPALMOS__) // Normal system call if ( wxRename (file1, file2) == 0 ) @@ -1115,7 +1126,7 @@ wxRenameFile (const wxString& file1, const wxString& file2) #endif // Try to copy - if (wxCopyFile(file1, file2)) { + if (wxCopyFile(file1, file2, overwrite)) { wxRemoveFile(file1); return true; }