]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/filename.cpp
Aqua splitter sashes.
[wxWidgets.git] / src / common / filename.cpp
index 3b45c14c03781b2a67f096b0ba0d0d08cc97be0a..ad01fde9184fd1faa239ff5c603a8767f3bd0cb1 100644 (file)
@@ -24,7 +24,7 @@
 
                 There are also UNC names of the form \\share\fullpath
 
-   wxPATH_MAC:  Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file 
+   wxPATH_MAC:  Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
                 names have the form
                     volume:dir1:...:dirN:filename
                 and the relative file names are either
@@ -32,7 +32,9 @@
                 or just
                     filename
                 (although :filename works as well).
-                :::file is not yet supported. TODO.
+                :::filename.ext is not yet supported. TODO.
+                Since the volume is just part of the file path, it is not
+                treated like a separate entity as it is done under DOS.
 
    wxPATH_VMS:  VMS native format, absolute file names have the form
                     <device>:[dir1.dir2.dir3]file.txt
@@ -365,7 +367,7 @@ wxString wxFileName::GetCwd(const wxString& volume)
     {
         SetCwd(cwdOld);
     }
-    
+
     return cwd;
 }
 
@@ -627,7 +629,7 @@ bool wxFileName::Normalize(wxPathNormalize flags,
     wxFileName curDir;
 
     format = GetFormat(format);
-    
+
     // make the path absolute
     if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute() )
     {
@@ -654,7 +656,7 @@ bool wxFileName::Normalize(wxPathNormalize flags,
             }
         }
     }
-    
+
     // handle ~ stuff under Unix only
     if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
     {
@@ -707,7 +709,7 @@ bool wxFileName::Normalize(wxPathNormalize flags,
                     return FALSE;
                 }
 
-                m_dirs.Remove(m_dirs.GetCount() - 1);
+                m_dirs.RemoveAt(m_dirs.GetCount() - 1);
                 continue;
             }
         }
@@ -743,11 +745,51 @@ bool wxFileName::Normalize(wxPathNormalize flags,
     return TRUE;
 }
 
+bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
+{
+    wxFileName fnBase(pathBase, format);
+
+    // get cwd only once - small time saving
+    wxString cwd = wxGetCwd();
+    Normalize(wxPATH_NORM_ALL, cwd, format);
+    fnBase.Normalize(wxPATH_NORM_ALL, cwd, format);
+
+    bool withCase = IsCaseSensitive(format);
+
+    // we can't do anything if the files live on different volumes
+    if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
+    {
+        // nothing done
+        return FALSE;
+    }
+
+    // same drive, so we don't need our volume
+    m_volume.clear();
+
+    // remove common directories starting at the top
+    while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
+                m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
+    {
+        m_dirs.RemoveAt(0);
+        fnBase.m_dirs.RemoveAt(0);
+    }
+
+    // add as many ".." as needed
+    size_t count = fnBase.m_dirs.GetCount();
+    for ( size_t i = 0; i < count; i++ )
+    {
+        m_dirs.Insert(wxT(".."), 0u);
+    }
+
+    // we were modified
+    return TRUE;
+}
+
 // ----------------------------------------------------------------------------
 // filename kind tests
 // ----------------------------------------------------------------------------
 
-bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format)
+bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format)
 {
     wxFileName fn1 = *this,
                fn2 = filepath;
@@ -828,12 +870,12 @@ wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
 {
     wxString sepVol;
 
-    if ( GetFormat(format) != wxPATH_UNIX )
+    if ( (GetFormat(format) == wxPATH_DOS) ||
+         (GetFormat(format) == wxPATH_VMS) )
     {
-        // so far it is the same for all systems which have it
         sepVol = wxFILE_SEP_DSK;
     }
-    //else: leave empty, no volume separators under Unix
+    //else: leave empty
 
     return sepVol;
 }
@@ -957,17 +999,19 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
     // first put the volume
     if ( !m_volume.empty() )
     {
-        // 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 // !UNC
-        {
-            fullpath << m_volume << GetVolumeSeparator(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 && m_volume.length() > 1 )
+               {
+               fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
+               }
+               else // !UNC
+               {
+               fullpath << m_volume << GetVolumeSeparator(format);
+            }
         }
     }
 
@@ -977,7 +1021,7 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
     {
         // under Mac, we must have a path separator in the beginning of the
         // relative path - otherwise it would be parsed as an absolute one
-        if ( format == wxPATH_MAC && m_volume.empty() && !m_dirs[0].empty() )
+        if ( format == wxPATH_MAC && m_dirs[0].empty() )
         {
             fullpath += wxFILE_SEP_PATH_MAC;
         }
@@ -1217,10 +1261,11 @@ void wxFileName::SplitPath(const wxString& fullpathWithVolume,
         }
     }
 
-    // do we have the volume name in the beginning?
-    wxString sepVol = GetVolumeSeparator(format);
-    if ( !sepVol.empty() )
+    // We separate the volume here
+    if ( format == wxPATH_DOS || format == wxPATH_VMS )
     {
+        wxString sepVol = GetVolumeSeparator(format);
+        
         size_t posFirstColon = fullpath.find_first_of(sepVol);
         if ( posFirstColon != wxString::npos )
         {