From 1527281edb1538a8a446b1de791fbf303deb5804 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 8 Apr 2002 16:28:50 +0000 Subject: [PATCH] fixed several bugs in Mkdir() and also modified its API to be more user friendly (based on the patch 541033 from Chris Elliott) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15033 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/filename.tex | 8 +++--- include/wx/filename.h | 10 ++++++-- src/common/filename.cpp | 51 +++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/docs/latex/wx/filename.tex b/docs/latex/wx/filename.tex index 8bc66c81c0..3c0459e648 100644 --- a/docs/latex/wx/filename.tex +++ b/docs/latex/wx/filename.tex @@ -539,15 +539,17 @@ different from the volume specified by {\it pathBase}). \membersection{wxFileName::Mkdir}\label{wxfilenamemkdir} -\func{bool}{Mkdir}{\param{int }{perm = 0777}, \param{bool }{full = FALSE}} +\func{bool}{Mkdir}{\param{int }{perm = 0777}, \param{int }{flags = $0$}} -\func{static bool}{Mkdir}{\param{const wxString\& }{dir}, \param{int }{perm = 0777}, \param{bool }{full = FALSE}} +\func{static bool}{Mkdir}{\param{const wxString\& }{dir}, \param{int }{perm = 0777}, \param{int }{flags = $0$}} \docparam{dir}{the directory to create} \docparam{parm}{the permissions for the newly created directory} -\docparam{full}{if {\tt TRUE}, will try to make each directory in the path} +\docparam{flags}{if the flags contain {\tt wxPATH\_MKDIR\_FULL} flag, +try to create each directory in the path and also don't return an error +if the target directory already exists.} \wxheading{Return value} diff --git a/include/wx/filename.h b/include/wx/filename.h index 646e239303..4e5923d5d2 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -80,6 +80,12 @@ enum wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator }; +// MkDir flags +enum +{ + wxPATH_MKDIR_FULL = 0x0001 // create directories recursively +}; + // ---------------------------------------------------------------------------- // wxFileName: encapsulates a file path // ---------------------------------------------------------------------------- @@ -236,8 +242,8 @@ public: // directory creation and removal. // if full is TRUE, will try to make each directory in the path. - bool Mkdir( int perm = 0777, bool full = FALSE); - static bool Mkdir( const wxString &dir, int perm = 0777, bool full = FALSE ); + bool Mkdir( int perm = 0777, int flags = 0); + static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 ); bool Rmdir(); static bool Rmdir( const wxString &dir ); diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 7a5340f90d..c93c314032 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -735,47 +735,48 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) // directory operations // ---------------------------------------------------------------------------- -bool wxFileName::Mkdir( int perm, bool full ) +bool wxFileName::Mkdir( int perm, int flags ) { - return wxFileName::Mkdir( GetFullPath(), perm, full ); + return wxFileName::Mkdir( GetFullPath(), perm, flags ); } -bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) +bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) { - if (full) + if ( flags & wxPATH_MKDIR_FULL ) { - wxFileName filename(dir); - wxArrayString dirs = filename.GetDirs(); - dirs.Add(filename.GetName()); + // split the path in components + wxFileName filename; + filename.AssignDir(dir); - size_t count = dirs.GetCount(); - size_t i; wxString currPath; - int noErrors = 0; - for ( i = 0; i < count; i++ ) - { - currPath += dirs[i]; + if ( filename.HasVolume()) + { + currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE); + } - if (currPath.Last() == wxT(':')) - { - // Can't create a root directory so continue to next dir + wxArrayString dirs = filename.GetDirs(); + size_t count = dirs.GetCount(); + for ( size_t i = 0; i < count; i++ ) + { + if ( i > 0 || filename.IsAbsolute() ) currPath += wxFILE_SEP_PATH; - continue; - } + currPath += dirs[i]; if (!DirExists(currPath)) + { if (!wxMkdir(currPath, perm)) - noErrors ++; - - if ( (i < (count-1)) ) - currPath += wxFILE_SEP_PATH; + { + // no need to try creating further directories + return FALSE; + } + } } - return (noErrors == 0); + return TRUE; } - else - return ::wxMkdir( dir, perm ); + + return ::wxMkdir( dir, perm ); } bool wxFileName::Rmdir() -- 2.45.2