]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/filename.cpp
fixed line endings
[wxWidgets.git] / src / common / filename.cpp
index 46ce327c19c6ccc23d1607e9a00733cdd40d46b0..12bc12daa7303212c2306ce9279615690af33116 100644 (file)
 // headers
 // ----------------------------------------------------------------------------
 
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
-#pragma implementation "filename.h"
-#endif
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 #endif
 
 #ifndef WX_PRECOMP
-#include "wx/intl.h"
-#include "wx/log.h"
-#include "wx/file.h"
+    #include "wx/dynarray.h"
+    #include "wx/intl.h"
+    #include "wx/log.h"
+    #include "wx/file.h"
+    #include "wx/utils.h"
 #endif
 
 #include "wx/filename.h"
 #include "wx/tokenzr.h"
 #include "wx/config.h"          // for wxExpandEnvVars
-#include "wx/utils.h"
 #include "wx/file.h"
 #include "wx/dynlib.h"
 
@@ -452,7 +449,8 @@ void wxFileName::Assign(const wxString& fullpathOrig,
                   _T("the path shouldn't contain file name nor extension") );
 
 #else // !__WXDEBUG__
-    SplitPath(fullname, NULL /* no path */, &name, &ext, &hasExt, format);
+    SplitPath(fullname, NULL /* no volume */, NULL /* no path */,
+                        &name, &ext, &hasExt, format);
     SplitPath(fullpath, &volume, &path, NULL, NULL, format);
 #endif // __WXDEBUG__/!__WXDEBUG__
 
@@ -521,7 +519,7 @@ bool wxFileName::FileExists( const wxString &file )
 
 bool wxFileName::DirExists() const
 {
-    return wxFileName::DirExists( GetFullPath() );
+    return wxFileName::DirExists( GetPath() );
 }
 
 bool wxFileName::DirExists( const wxString &dir )
@@ -564,7 +562,7 @@ wxString wxFileName::GetCwd(const wxString& volume)
 
 bool wxFileName::SetCwd()
 {
-    return wxFileName::SetCwd( GetFullPath() );
+    return wxFileName::SetCwd( GetPath() );
 }
 
 bool wxFileName::SetCwd( const wxString &cwd )
@@ -607,17 +605,31 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
     // use the directory specified by the prefix
     SplitPath(prefix, &dir, &name, NULL /* extension */);
 
+    if (dir.empty())
+    {
+        dir = wxGetenv(_T("TMPDIR"));
+        if (dir.empty())
+        {
+            dir = wxGetenv(_T("TMP"));
+            if (dir.empty())
+            {
+                dir = wxGetenv(_T("TEMP"));
+            }
+        }
+    }
+
 #if defined(__WXWINCE__)
     if (dir.empty())
     {
         // FIXME. Create \temp dir?
-        dir = wxT("\\");
+        if (DirExists(wxT("\\temp")))
+            dir = wxT("\\temp");
     }
-    path = dir + wxT("\\") + prefix;
+    path = dir + wxT("\\") + name;
     int i = 1;
     while (FileExists(path))
     {
-        path = dir + wxT("\\") + prefix ;
+        path = dir + wxT("\\") + name ;
         path << i;
         i ++;
     }
@@ -654,25 +666,14 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
 #else // !Windows
     if ( dir.empty() )
     {
-#if defined(__WXMAC__) && !defined(__DARWIN__)
-        dir = wxMacFindFolder(  (short) kOnSystemDisk, kTemporaryFolderType, kCreateFolder ) ;
-#else // !Mac
-        dir = wxGetenv(_T("TMP"));
-        if ( dir.empty() )
-        {
-            dir = wxGetenv(_T("TEMP"));
-        }
-
-        if ( dir.empty() )
-        {
-            // default
-            #if defined(__DOS__) || defined(__OS2__)
-                dir = _T(".");
-            #else
-                dir = _T("/tmp");
-            #endif
-        }
-#endif // Mac/!Mac
+        // default
+#if defined(__DOS__) || defined(__OS2__)
+        dir = _T(".");
+#elif defined(__WXMAC__)
+        dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
+#else
+        dir = _T("/tmp");
+#endif
     }
 
     path = dir;
@@ -753,9 +754,6 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
     path = pathTry;
 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
 
-    if ( !path.empty() )
-    {
-    }
 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
 
 #endif // Windows/!Windows
@@ -801,7 +799,7 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
 
 bool wxFileName::Mkdir( int perm, int flags )
 {
-    return wxFileName::Mkdir( GetFullPath(), perm, flags );
+    return wxFileName::Mkdir(GetPath(), perm, flags);
 }
 
 bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
@@ -852,7 +850,7 @@ bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
 
 bool wxFileName::Rmdir()
 {
-    return wxFileName::Rmdir( GetFullPath() );
+    return wxFileName::Rmdir( GetPath() );
 }
 
 bool wxFileName::Rmdir( const wxString &dir )
@@ -1055,7 +1053,7 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targe
     bool success = false;
 
     // Assume it's not a shortcut if it doesn't end with lnk
-    if (ext.Lower() != wxT("lnk"))
+    if (ext.CmpNoCase(wxT("lnk"))!=0)
         return false;
 
     // create a ShellLink object
@@ -1242,8 +1240,11 @@ wxString wxFileName::GetForbiddenChars(wxPathFormat format)
 }
 
 /* static */
-wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
+wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format))
 {
+#ifdef __WXWINCE__
+    return wxEmptyString;
+#else
     wxString sepVol;
 
     if ( (GetFormat(format) == wxPATH_DOS) ||
@@ -1254,6 +1255,7 @@ wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
     //else: leave empty
 
     return sepVol;
+#endif
 }
 
 /* static */
@@ -1452,7 +1454,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
                 }
 
                 // convert back from ".." to nothing
-                if ( m_dirs[i] != wxT("..") )
+                if ( !m_dirs[i].IsSameAs(wxT("..")) )
                      fullpath += m_dirs[i];
                 break;
 
@@ -1469,7 +1471,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
                 // TODO: What to do with ".." under VMS
 
                 // convert back from ".." to nothing
-                if ( m_dirs[i] != wxT("..") )
+                if ( !m_dirs[i].IsSameAs(wxT("..")) )
                     fullpath += m_dirs[i];
                 break;
         }
@@ -1501,27 +1503,26 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
 // Return the short form of the path (returns identity on non-Windows platforms)
 wxString wxFileName::GetShortPath() const
 {
-#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
     wxString path(GetFullPath());
-    wxString pathOut;
+
+#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
     DWORD sz = ::GetShortPathName(path, NULL, 0);
-    bool ok = sz != 0;
-    if ( ok )
+    if ( sz != 0 )
     {
-        ok = ::GetShortPathName
+        wxString pathOut;
+        if ( ::GetShortPathName
                (
                 path,
                 wxStringBuffer(pathOut, sz),
                 sz
-               ) != 0;
+               ) != 0 )
+        {
+            return pathOut;
+        }
     }
-    if (ok)
-        return pathOut;
+#endif // Windows
 
     return path;
-#else
-    return GetFullPath();
-#endif
 }
 
 // Return the long form of the path (returns identity on non-Windows platforms)
@@ -1531,116 +1532,115 @@ wxString wxFileName::GetLongPath() const
              path = GetFullPath();
 
 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
-    bool success = false;
 
 #if wxUSE_DYNAMIC_LOADER
     typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
 
-    static bool s_triedToLoad = false;
-
-    if ( !s_triedToLoad )
+    // this is MT-safe as in the worst case we're going to resolve the function
+    // twice -- but as the result is the same in both threads, it's ok
+    static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
+    if ( !s_pfnGetLongPathName )
     {
-        // suppress the errors about missing GetLongPathName[AW]
-        wxLogNull noLog;
+        static bool s_triedToLoad = false;
 
-        s_triedToLoad = true;
-        wxDynamicLibrary dllKernel(_T("kernel32"));
-        if ( dllKernel.IsLoaded() )
+        if ( !s_triedToLoad )
         {
-            // may succeed or fail depending on the Windows version
-            static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
-#ifdef _UNICODE
-            s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW"));
-#else
-            s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA"));
-#endif
+            s_triedToLoad = true;
 
-            if ( s_pfnGetLongPathName )
-            {
-                DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
-                bool ok = dwSize > 0;
+            wxDynamicLibrary dllKernel(_T("kernel32"));
 
-                if ( ok )
-                {
-                    DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
-                    ok = sz != 0;
-                    if ( ok )
-                    {
-                        ok = (*s_pfnGetLongPathName)
-                                (
-                                path,
-                                wxStringBuffer(pathOut, sz),
-                                sz
-                                ) != 0;
-                        success = true;
-                    }
-                }
+            const wxChar* GetLongPathName = _T("GetLongPathName")
+#if wxUSE_UNICODE
+                              _T("W");
+#else // ANSI
+                              _T("A");
+#endif // Unicode/ANSI
+
+            if ( dllKernel.HasSymbol(GetLongPathName) )
+            {
+                s_pfnGetLongPathName = (GET_LONG_PATH_NAME)
+                    dllKernel.GetSymbol(GetLongPathName);
             }
+
+            // note that kernel32.dll can be unloaded, it stays in memory
+            // anyhow as all Win32 programs link to it and so it's safe to call
+            // GetLongPathName() even after unloading it
         }
     }
 
-    if (success)
-        return pathOut;
+    if ( s_pfnGetLongPathName )
+    {
+        DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
+        if ( dwSize > 0 )
+        {
+            if ( (*s_pfnGetLongPathName)
+                 (
+                  path,
+                  wxStringBuffer(pathOut, dwSize),
+                  dwSize
+                 ) != 0 )
+            {
+                return pathOut;
+            }
+        }
+    }
 #endif // wxUSE_DYNAMIC_LOADER
 
-    if (!success)
-    {
-        // The OS didn't support GetLongPathName, or some other error.
-        // We need to call FindFirstFile on each component in turn.
+    // The OS didn't support GetLongPathName, or some other error.
+    // We need to call FindFirstFile on each component in turn.
 
-        WIN32_FIND_DATA findFileData;
-        HANDLE hFind;
+    WIN32_FIND_DATA findFileData;
+    HANDLE hFind;
 
-        if ( HasVolume() )
-            pathOut = GetVolume() +
-                      GetVolumeSeparator(wxPATH_DOS) +
-                      GetPathSeparator(wxPATH_DOS);
-        else
-            pathOut = wxEmptyString;
+    if ( HasVolume() )
+        pathOut = GetVolume() +
+                  GetVolumeSeparator(wxPATH_DOS) +
+                  GetPathSeparator(wxPATH_DOS);
+    else
+        pathOut = wxEmptyString;
 
-        wxArrayString dirs = GetDirs();
-        dirs.Add(GetFullName());
+    wxArrayString dirs = GetDirs();
+    dirs.Add(GetFullName());
 
-        wxString tmpPath;
+    wxString tmpPath;
 
-        size_t count = dirs.GetCount();
-        for ( size_t i = 0; i < count; i++ )
-        {
-            // We're using pathOut to collect the long-name path, but using a
-            // temporary for appending the last path component which may be
-            // short-name
-            tmpPath = pathOut + dirs[i];
+    size_t count = dirs.GetCount();
+    for ( size_t i = 0; i < count; i++ )
+    {
+        // We're using pathOut to collect the long-name path, but using a
+        // temporary for appending the last path component which may be
+        // short-name
+        tmpPath = pathOut + dirs[i];
 
-            if ( tmpPath.empty() )
-                continue;
+        if ( tmpPath.empty() )
+            continue;
 
-            // can't see this being necessary? MF
-            if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
-            {
-                // Can't pass a drive and root dir to FindFirstFile,
-                // so continue to next dir
-                tmpPath += wxFILE_SEP_PATH;
-                pathOut = tmpPath;
-                continue;
-            }
+        // can't see this being necessary? MF
+        if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
+        {
+            // Can't pass a drive and root dir to FindFirstFile,
+            // so continue to next dir
+            tmpPath += wxFILE_SEP_PATH;
+            pathOut = tmpPath;
+            continue;
+        }
 
-            hFind = ::FindFirstFile(tmpPath, &findFileData);
-            if (hFind == INVALID_HANDLE_VALUE)
-            {
-                // Error: most likely reason is that path doesn't exist, so
-                // append any unprocessed parts and return
-                for ( i += 1; i < count; i++ )
-                    tmpPath += wxFILE_SEP_PATH + dirs[i];
+        hFind = ::FindFirstFile(tmpPath, &findFileData);
+        if (hFind == INVALID_HANDLE_VALUE)
+        {
+            // Error: most likely reason is that path doesn't exist, so
+            // append any unprocessed parts and return
+            for ( i += 1; i < count; i++ )
+                tmpPath += wxFILE_SEP_PATH + dirs[i];
 
-                return tmpPath;
-            }
+            return tmpPath;
+        }
 
-            pathOut += findFileData.cFileName;
-            if ( (i < (count-1)) )
-                pathOut += wxFILE_SEP_PATH;
+        pathOut += findFileData.cFileName;
+        if ( (i < (count-1)) )
+            pathOut += wxFILE_SEP_PATH;
 
-            ::FindClose(hFind);
-        }
+        ::FindClose(hFind);
     }
 #else // !Win32
     pathOut = path;
@@ -1895,6 +1895,8 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess,
         }
     }
 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
+    wxUnusedVar(dtCreate);
+
     if ( !dtAccess && !dtMod )
     {
         // can't modify the creation time anyhow, don't try
@@ -1911,6 +1913,9 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess,
         return true;
     }
 #else // other platform
+    wxUnusedVar(dtAccess);
+    wxUnusedVar(dtMod);
+    wxUnusedVar(dtCreate);
 #endif // platforms
 
     wxLogSysError(_("Failed to modify file times for '%s'"),
@@ -1987,7 +1992,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess,
 
         return true;
     }
-#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
+#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
     wxStructStat stBuf;
     if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
     {
@@ -2001,6 +2006,9 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess,
         return true;
     }
 #else // other platform
+    wxUnusedVar(dtAccess);
+    wxUnusedVar(dtMod);
+    wxUnusedVar(dtCreate);
 #endif // platforms
 
     wxLogSysError(_("Failed to retrieve file times for '%s'"),
@@ -2040,7 +2048,6 @@ public :
   OSType m_creator ;
 }  ;
 
-#include "wx/dynarray.h"
 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ;
 
 bool gMacDefaultExtensionsInited = false ;