]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/filename.cpp
replacing old core graphics with new graphics context implementation
[wxWidgets.git] / src / common / filename.cpp
index d1766a8179453f8f503c5c886f29d6c6a2b12785..b4ea82e56a960adea19bb2f8e4d83f6a000657de 100644 (file)
 #endif
 
 #ifndef WX_PRECOMP
-#include "wx/intl.h"
-#include "wx/log.h"
-#include "wx/file.h"
+    #ifdef __WXMSW__
+        #include "wx/msw/wrapwin.h" // For GetShort/LongPathName
+    #endif
+    #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"
 
-// For GetShort/LongPathName
-#ifdef __WIN32__
-#include "wx/msw/wrapwin.h"
-#if defined(__MINGW32__)
-#include "wx/msw/gccpriv.h"
-#endif
+#if defined(__WIN32__) && defined(__MINGW32__)
+    #include "wx/msw/gccpriv.h"
 #endif
 
 #ifdef __WXWINCE__
 #define MAX_PATH _MAX_PATH
 #endif
 
+
+wxULongLong wxInvalidSize = (unsigned)-1;
+
+
 // ----------------------------------------------------------------------------
 // private classes
 // ----------------------------------------------------------------------------
@@ -518,7 +522,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 )
@@ -561,7 +565,7 @@ wxString wxFileName::GetCwd(const wxString& volume)
 
 bool wxFileName::SetCwd()
 {
-    return wxFileName::SetCwd( GetFullPath() );
+    return wxFileName::SetCwd( GetPath() );
 }
 
 bool wxFileName::SetCwd( const wxString &cwd )
@@ -604,11 +608,25 @@ 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("\\") + name;
     int i = 1;
@@ -651,25 +669,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;
@@ -750,9 +757,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
@@ -798,7 +802,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 )
@@ -849,7 +853,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 )
@@ -1042,7 +1046,9 @@ bool wxFileName::Normalize(int flags,
 #include <shlguid.h>
 #endif
 
-bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targetFilename, wxString* arguments)
+bool wxFileName::GetShortcutTarget(const wxString& shortcutPath,
+                                   wxString& targetFilename,
+                                   wxString* arguments)
 {
     wxString path, file, ext;
     wxSplitPath(shortcutPath, & path, & file, & ext);
@@ -1071,6 +1077,8 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targe
                                 MAX_PATH);
 
             hres = ppf->Load(wsz, 0);
+            ppf->Release();
+
             if (SUCCEEDED(hres))
             {
                 wxChar buf[2048];
@@ -1091,11 +1099,13 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targe
                 }
             }
         }
+
+        psl->Release();
     }
-    psl->Release();
     return success;
 }
-#endif
+
+#endif // __WIN32__ && !__WXWINCE__
 
 
 // ----------------------------------------------------------------------------
@@ -1239,8 +1249,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) ||
@@ -1251,6 +1264,7 @@ wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
     //else: leave empty
 
     return sepVol;
+#endif
 }
 
 /* static */
@@ -1987,7 +2001,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 )
     {
@@ -2014,6 +2028,87 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess,
 
 #endif // wxUSE_DATETIME
 
+
+// ----------------------------------------------------------------------------
+// file size functions
+// ----------------------------------------------------------------------------
+
+/* static */
+wxULongLong wxFileName::GetSize(const wxString &filename)
+{
+    if (!wxFileExists(filename))
+        return wxInvalidSize;
+
+#if defined(__WXPALMOS__)
+    // TODO
+    return wxInvalidSize;
+#elif defined(__WIN32__)
+    wxFileHandle f(filename, wxFileHandle::Read);
+    if (!f.IsOk())
+        return wxInvalidSize;
+
+    DWORD lpFileSizeHigh;
+    DWORD ret = GetFileSize(f, &lpFileSizeHigh);
+    if (ret == INVALID_FILE_SIZE)
+        return wxInvalidSize;
+
+    // compose the low-order and high-order byte sizes
+    return wxULongLong(ret | (lpFileSizeHigh << sizeof(WORD)*2));
+
+#else           // ! __WIN32__
+
+    wxStructStat st;
+#ifndef wxNEED_WX_UNISTD_H
+    if (wxStat( filename.fn_str() , &st) != 0)
+#else
+    if (wxStat( filename, &st) != 0)
+#endif
+        return wxInvalidSize;
+    return wxULongLong(st.st_size);
+#endif
+}
+
+/* static */
+wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs,
+                                          const wxString &nullsize,
+                                          int precision)
+{
+    static const double KILOBYTESIZE = 1024.0;
+    static const double MEGABYTESIZE = 1024.0*KILOBYTESIZE;
+    static const double GIGABYTESIZE = 1024.0*MEGABYTESIZE;
+    static const double TERABYTESIZE = 1024.0*GIGABYTESIZE;
+
+    if (bs == 0 || bs == wxInvalidSize)
+        return nullsize;
+
+    double bytesize = bs.ToDouble();
+    if (bytesize < KILOBYTESIZE)
+        return wxString::Format(_("%s B"), bs.ToString().c_str());
+    if (bytesize < MEGABYTESIZE)
+        return wxString::Format(_("%.*f kB"), precision, bytesize/KILOBYTESIZE);
+    if (bytesize < GIGABYTESIZE)
+        return wxString::Format(_("%.*f MB"), precision, bytesize/MEGABYTESIZE);
+    if (bytesize < TERABYTESIZE)
+        return wxString::Format(_("%.*f GB"), precision, bytesize/GIGABYTESIZE);
+
+    return wxString::Format(_("%.*f TB"), precision, bytesize/TERABYTESIZE);
+}
+
+wxULongLong wxFileName::GetSize() const
+{
+    return GetSize(GetFullPath());
+}
+
+wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision) const
+{
+    return GetHumanReadableSize(GetSize(), failmsg, precision);
+}
+
+
+// ----------------------------------------------------------------------------
+// Mac-specific functions
+// ----------------------------------------------------------------------------
+
 #ifdef __WXMAC__
 
 const short kMacExtensionMaxLength = 16 ;
@@ -2043,7 +2138,6 @@ public :
   OSType m_creator ;
 }  ;
 
-#include "wx/dynarray.h"
 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ;
 
 bool gMacDefaultExtensionsInited = false ;