]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/filename.cpp
wxWizardPage(Simple) can do two-phase creation now
[wxWidgets.git] / src / common / filename.cpp
index bc37751bbf05fc99be2226df95d9659c31446f73..a7409aa0c89f90c004a3ffc97040806c2031bce9 100644 (file)
@@ -82,7 +82,7 @@
 #include "wx/config.h"          // for wxExpandEnvVars
 #include "wx/utils.h"
 #include "wx/file.h"
 #include "wx/config.h"          // for wxExpandEnvVars
 #include "wx/utils.h"
 #include "wx/file.h"
-//#include "wx/dynlib.h"        // see GetLongPath below, code disabled.
+#include "wx/dynlib.h"
 
 // For GetShort/LongPathName
 #ifdef __WIN32__
 
 // For GetShort/LongPathName
 #ifdef __WIN32__
@@ -146,7 +146,7 @@ public:
         m_hFile = ::CreateFile
                     (
                      filename,                      // name
         m_hFile = ::CreateFile
                     (
                      filename,                      // name
-                     mode == Read ? GENERIC_READ    // access mask 
+                     mode == Read ? GENERIC_READ    // access mask
                                   : GENERIC_WRITE,
                      0,                             // no sharing
                      NULL,                          // no secutity attr
                                   : GENERIC_WRITE,
                      0,                             // no sharing
                      NULL,                          // no secutity attr
@@ -239,6 +239,33 @@ static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
 
 #endif // __WIN32__
 
 
 #endif // __WIN32__
 
+// return a string with the volume par
+static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format)
+{
+    wxString path;
+
+    if ( !volume.empty() )
+    {
+        format = wxFileName::GetFormat(format);
+
+        // Special Windows UNC paths hack, part 2: undo what we did in
+        // SplitPath() and make an UNC path if we have a drive which is not a
+        // single letter (hopefully the network shares can't be one letter only
+        // although I didn't find any authoritative docs on this)
+        if ( format == wxPATH_DOS && volume.length() > 1 )
+        {
+            path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume;
+        }
+        else if  ( format == wxPATH_DOS || format == wxPATH_VMS )
+        {
+            path << volume << wxFileName::GetVolumeSeparator(format);
+        }
+        // else ignore
+    }
+
+    return path;
+}
+
 // ============================================================================
 // implementation
 // ============================================================================
 // ============================================================================
 // implementation
 // ============================================================================
@@ -367,7 +394,7 @@ void wxFileName::Assign(const wxString& fullpathOrig,
     wxString fullpath = fullpathOrig;
     if ( !wxEndsWithPathSeparator(fullpath) )
     {
     wxString fullpath = fullpathOrig;
     if ( !wxEndsWithPathSeparator(fullpath) )
     {
-        fullpath += GetPathSeparators(format)[0u];
+        fullpath += GetPathSeparator(format);
     }
 
     wxString volume, path, name, ext;
     }
 
     wxString volume, path, name, ext;
@@ -388,7 +415,7 @@ void wxFileName::Assign(const wxString& fullpathOrig,
                   _T("the path shouldn't contain file name nor extension") );
 
 #else // !__WXDEBUG__
                   _T("the path shouldn't contain file name nor extension") );
 
 #else // !__WXDEBUG__
-    SplitPath(fullname, NULL /* no path */, &name, &ext, format); 
+    SplitPath(fullname, NULL /* no path */, &name, &ext, format);
     SplitPath(fullpath, &volume, &path, NULL, NULL, format);
 #endif // __WXDEBUG__/!__WXDEBUG__
 
     SplitPath(fullpath, &volume, &path, NULL, NULL, format);
 #endif // __WXDEBUG__/!__WXDEBUG__
 
@@ -710,47 +737,48 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
 // directory operations
 // ----------------------------------------------------------------------------
 
 // 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;
         wxString currPath;
-        int noErrors = 0;
-        for ( i = 0; i < count; i++ )
+        if ( filename.HasVolume())
         {
         {
-            currPath += dirs[i];
+            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;
                 currPath += wxFILE_SEP_PATH;
-                continue;
-            }
+            currPath += dirs[i];
 
             if (!DirExists(currPath))
 
             if (!DirExists(currPath))
+            {
                 if (!wxMkdir(currPath, perm))
                 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()
 }
 
 bool wxFileName::Rmdir()
@@ -955,6 +983,17 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
         m_dirs.Insert(wxT(".."), 0u);
     }
 
         m_dirs.Insert(wxT(".."), 0u);
     }
 
+    if ( format == wxPATH_UNIX || format == wxPATH_DOS )
+    {
+        // a directory made relative with respect to itself is '.' under Unix
+        // and DOS, by definition (but we don't have to insert "./" for the
+        // files)
+        if ( m_dirs.IsEmpty() && IsDir() )
+        {
+            m_dirs.Add(_T('.'));
+        }
+    }
+
     m_relative = TRUE;
 
     // we were modified
     m_relative = TRUE;
 
     // we were modified
@@ -1048,13 +1087,6 @@ bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
     return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
 }
 
     return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
 }
 
-bool wxFileName::IsWild( wxPathFormat WXUNUSED(format) )
-{
-    // FIXME: this is probably false for Mac and this is surely wrong for most
-    //        of Unix shells (think about "[...]")
-    return m_name.find_first_of(_T("*?")) != wxString::npos;
-}
-
 // ----------------------------------------------------------------------------
 // path components manipulation
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // path components manipulation
 // ----------------------------------------------------------------------------
@@ -1099,115 +1131,16 @@ wxString wxFileName::GetFullName() const
     return fullname;
 }
 
     return fullname;
 }
 
-wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
+wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
 {
     format = GetFormat( format );
 
     wxString fullpath;
 
 {
     format = GetFormat( format );
 
     wxString fullpath;
 
-    // the leading character
-    if ( format == wxPATH_MAC && m_relative )
-    {
-         fullpath += wxFILE_SEP_PATH_MAC;
-    }
-    else if ( format == wxPATH_DOS )
-    {
-         if (!m_relative)
-             fullpath += wxFILE_SEP_PATH_DOS;
-    }
-    else if ( format == wxPATH_UNIX )
-    {
-         if (!m_relative)
-             fullpath += wxFILE_SEP_PATH_UNIX;
-    }
-
-    // then concatenate all the path components using the path separator
-    size_t dirCount = m_dirs.GetCount();
-    if ( dirCount )
-    {
-        if ( format == wxPATH_VMS )
-        {
-            fullpath += wxT('[');
-        }
-
-
-        for ( size_t i = 0; i < dirCount; i++ )
-        {
-            // TODO: What to do with ".." under VMS
-
-            switch (format)
-            {
-                case wxPATH_MAC:
-                {
-                    if (m_dirs[i] == wxT("."))
-                        break;
-                    if (m_dirs[i] != wxT(".."))  // convert back from ".." to nothing
-                         fullpath += m_dirs[i];
-                    fullpath += wxT(':');
-                    break;
-                }
-                case wxPATH_DOS:
-                {
-                    fullpath += m_dirs[i];
-                    fullpath += wxT('\\');
-                    break;
-                }
-                case wxPATH_UNIX:
-                {
-                    fullpath += m_dirs[i];
-                    fullpath += wxT('/');
-                    break;
-                }
-                case wxPATH_VMS:
-                {
-                    if (m_dirs[i] != wxT(".."))  // convert back from ".." to nothing
-                        fullpath += m_dirs[i];
-                    if (i == dirCount-1)
-                        fullpath += wxT(']');
-                    else
-                        fullpath += wxT('.');
-                    break;
-                }
-                default:
-                {
-                    wxFAIL_MSG( wxT("error") );
-                }
-            }
-        }
-    }
-
-    if ( add_separator && !fullpath.empty() )
-    {
-        fullpath += GetPathSeparators(format)[0u];
-    }
-
-    return fullpath;
-}
-
-wxString wxFileName::GetFullPath( wxPathFormat format ) const
-{
-    format = GetFormat(format);
-
-    wxString fullpath;
-
-    // first put the volume
-    if ( !m_volume.empty() )
+    // return the volume with the path as well if requested
+    if ( flags & wxPATH_GET_VOLUME )
     {
     {
-        {
-            // Special Windows UNC paths hack, part 2: undo what we did in
-            // SplitPath() and make an UNC path if we have a drive which is not a
-            // single letter (hopefully the network shares can't be one letter only
-            // although I didn't find any authoritative docs on this)
-            if ( format == wxPATH_DOS && m_volume.length() > 1 )
-            {
-                fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
-            }
-            else if  ( format == wxPATH_DOS || format == wxPATH_VMS )
-            {
-                fullpath << m_volume << GetVolumeSeparator(format);
-            }
-            // else ignore
-        }
+        fullpath += wxGetVolumeString(GetVolume(), format);
     }
 
     // the leading character
     }
 
     // the leading character
@@ -1218,7 +1151,7 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
     }
     else if ( format == wxPATH_DOS )
     {
     }
     else if ( format == wxPATH_DOS )
     {
-        if ( !m_relative )
+        if (!m_relative)
             fullpath += wxFILE_SEP_PATH_DOS;
     }
     else if ( format == wxPATH_UNIX )
             fullpath += wxFILE_SEP_PATH_DOS;
     }
     else if ( format == wxPATH_UNIX )
@@ -1243,53 +1176,65 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
             fullpath += wxT('[');
         }
 
             fullpath += wxT('[');
         }
 
-
         for ( size_t i = 0; i < dirCount; i++ )
         {
         for ( size_t i = 0; i < dirCount; i++ )
         {
-            // TODO: What to do with ".." under VMS
-
             switch (format)
             {
                 case wxPATH_MAC:
             switch (format)
             {
                 case wxPATH_MAC:
-                {
-                    if (m_dirs[i] == wxT("."))
-                        break;
-                    if (m_dirs[i] != wxT(".."))  // convert back from ".." to nothing
+                    if ( m_dirs[i] == wxT(".") )
+                    {
+                        // skip appending ':', this shouldn't be done in this
+                        // case as "::" is interpreted as ".." under Unix
+                        continue;
+                    }
+
+                    // convert back from ".." to nothing
+                    if ( m_dirs[i] != wxT("..") )
                          fullpath += m_dirs[i];
                          fullpath += m_dirs[i];
-                    fullpath += wxT(':');
                     break;
                     break;
-                }
+
+                default:
+                    wxFAIL_MSG( wxT("unexpected path format") );
+                    // still fall through
+
                 case wxPATH_DOS:
                 case wxPATH_DOS:
-                {
-                    fullpath += m_dirs[i];
-                    fullpath += wxT('\\');
-                    break;
-                }
                 case wxPATH_UNIX:
                 case wxPATH_UNIX:
-                {
                     fullpath += m_dirs[i];
                     fullpath += m_dirs[i];
-                    fullpath += wxT('/');
                     break;
                     break;
-                }
+
                 case wxPATH_VMS:
                 case wxPATH_VMS:
-                {
-                    if (m_dirs[i] != wxT(".."))  // convert back from ".." to nothing
+                    // TODO: What to do with ".." under VMS
+                    // convert back from ".." to nothing
+                    if ( m_dirs[i] != wxT("..") )
                         fullpath += m_dirs[i];
                         fullpath += m_dirs[i];
-                    if (i == dirCount-1)
-                        fullpath += wxT(']');
-                    else
-                        fullpath += wxT('.');
                     break;
                     break;
-                }
-                default:
-                {
-                    wxFAIL_MSG( wxT("error") );
-                }
             }
             }
+
+            if ( i != dirCount - 1 )
+                fullpath += GetPathSeparator(format);
+        }
+
+        if ( format == wxPATH_VMS )
+        {
+            fullpath += wxT(']');
         }
     }
 
         }
     }
 
-    // finally add the file name and extension
+    if ( (flags & wxPATH_GET_SEPARATOR) && !fullpath.empty() )
+    {
+        fullpath += GetPathSeparator(format);
+    }
+
+    return fullpath;
+}
+
+wxString wxFileName::GetFullPath( wxPathFormat format ) const
+{
+    // we already have a function to get the path
+    wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR,
+                                format);
+
+    // now just add the file name and extension to it
     fullpath += GetFullName();
 
     return fullpath;
     fullpath += GetFullName();
 
     return fullpath;
@@ -1331,9 +1276,8 @@ wxString wxFileName::GetLongPath() const
 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
     bool success = FALSE;
 
 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
     bool success = FALSE;
 
-    // VZ: why was this code disabled?
-#if 0 // wxUSE_DYNAMIC_LOADER
-    typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
+#if wxUSE_DYNAMIC_LOADER
+    typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
 
     static bool s_triedToLoad = FALSE;
 
 
     static bool s_triedToLoad = FALSE;
 
@@ -1616,9 +1560,9 @@ void wxFileName::SplitPath(const wxString& fullpath,
     wxString volume;
     SplitPath(fullpath, &volume, path, name, ext, format);
 
     wxString volume;
     SplitPath(fullpath, &volume, path, name, ext, format);
 
-    if ( path && !volume.empty() )
+    if ( path )
     {
     {
-        path->Prepend(volume + GetVolumeSeparator(format));
+        path->Prepend(wxGetVolumeString(volume, format));
     }
 }
 
     }
 }
 
@@ -1768,18 +1712,18 @@ static void MacEnsureDefaultExtensionsLoaded()
     MacDefaultExtensionRecord defaults[] =
     {
       { "txt" , 'TEXT' , 'ttxt' } ,
     MacDefaultExtensionRecord defaults[] =
     {
       { "txt" , 'TEXT' , 'ttxt' } ,
-      
+
     } ;
     // we could load the pc exchange prefs here too
     } ;
     // we could load the pc exchange prefs here too
-    
+
     for ( int i = 0 ; i < WXSIZEOF( defaults ) ; ++i )
     {
       gMacDefaultExtensions.Add( defaults[i] ) ;
     for ( int i = 0 ; i < WXSIZEOF( defaults ) ; ++i )
     {
       gMacDefaultExtensions.Add( defaults[i] ) ;
-    } 
+    }
     gMacDefaultExtensionsInited = true ;
   }
 }
     gMacDefaultExtensionsInited = true ;
   }
 }
-bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) 
+bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
 {
   FInfo fndrInfo ;
   FSSpec spec ;
 {
   FInfo fndrInfo ;
   FSSpec spec ;
@@ -1793,7 +1737,7 @@ bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
   return true ;
 }
 
   return true ;
 }
 
-bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) 
+bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator )
 {
   FInfo fndrInfo ;
   FSSpec spec ;
 {
   FInfo fndrInfo ;
   FSSpec spec ;
@@ -1817,7 +1761,7 @@ bool wxFileName::MacSetDefaultTypeAndCreator()
     return false;
 }
 
     return false;
 }
 
-bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) 
+bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator )
 {
   MacEnsureDefaultExtensionsLoaded() ;
   wxString extl = ext.Lower() ;
 {
   MacEnsureDefaultExtensionsLoaded() ;
   wxString extl = ext.Lower() ;