]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxGetDiskSpace for Win/Unix
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 25 Aug 2001 16:54:14 +0000 (16:54 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 25 Aug 2001 16:54:14 +0000 (16:54 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11476 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/function.tex
include/wx/utils.h
samples/console/console.cpp
src/msw/utils.cpp
src/unix/utilsunx.cpp

index 48d3c7cc7ca897321b111bf9d407e62a1864ca0c..f7dc556f01560efb4fd90745eb80b094683c85ed 100644 (file)
@@ -7,7 +7,8 @@ wxWindows 2 Change Log
 wxBase:
 
 - wxRegEx class added
-- more fixes to wxMBConv classes. Conversion to and from wchar_t now works with 
+- wxGetDiskSpace() function added (Jonothan Farr, Markus Fieber)
+- more fixes to wxMBConv classes. Conversion to and from wchar_t now works with
   glibc 2.2 as well as with glibc 2.1. Unix version now checks for iconv()'s
   capabilities at runtime instead of in the configure script.
 
index aab0d2bcb6121dccde6c6ec05a0ec15026482a26..f940c470ec987b93532f628beaef54eddf376490 100644 (file)
@@ -177,6 +177,27 @@ Returns the next file that matches the path passed to \helpref{wxFindFirstFile}{
 
 See \helpref{wxFindFirstFile}{wxfindfirstfile} for an example.
 
+\membersection{::wxGetDiskSpace}\label{wxgetdiskspace}
+
+\func{bool}{wxGetDiskSpace}{\param{const wxString\& }{path}, \path{wxLongLong }{*total = NULL}, \path{wxLongLong }{*free = NULL}}
+
+This function returns the total number of bytes and number of free bytes on
+the disk containing the directory {\it path} (it should exist). Both 
+{\it total} and {\it free} parameters may be {\tt NULL} if the corresponding
+information is not needed.
+
+\wxheading{Returns}
+
+{\tt TRUE} on success, {\tt FALSE} if an error occured (for example, the
+directory doesn't exist).
+
+\wxheading{Portability}
+
+This function is implemented for Win16 (only for drives less than 2Gb), Win32,
+Mac OS and generic Unix provided the system has {\tt statfs()} function.
+
+This function first appeared in wxWindows 2.3.2.
+
 \membersection{::wxGetOSDirectory}\label{wxgetosdirectory}
 
 \func{wxString}{wxGetOSDirectory}{\void}
index 601f3cedbb358629b4652e6e45687c29def1e6dd..02358570769f076e30cf8a81f67c4d8a177eaab5 100644 (file)
 #include "wx/list.h"
 #include "wx/filefn.h"
 
+// need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
+// wxLongLong
+#include "wx/longlong.h"
+
 #ifdef __X__
     #include <dirent.h>
     #include <unistd.h>
@@ -264,11 +268,16 @@ WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
 #endif
 
 #ifdef __WXMAC__
-WXDLLEXPORT wxString wxMacFindFolder(short                                     vRefNum,
-                                                                OSType                                 folderType,
-                                                                Boolean                                createFolder);
+WXDLLEXPORT wxString wxMacFindFolder(short vRefNum,
+                                     OSType folderType,
+                                     Boolean createFolder);
 #endif
 
+// get number of total/free bytes on the disk where path belongs
+WXDLLEXPORT bool wxGetDiskSpace(const wxString& path,
+                                wxLongLong *pTotal = NULL,
+                                wxLongLong *pFree = NULL);
+
 #if wxUSE_GUI // GUI only things from now on
 
 // ----------------------------------------------------------------------------
index 8a75d830ab5330afdbf7d18de176e18a925a3dea..f7f43fc2ea04270a6b01d316b47268d251ca4399 100644 (file)
 //#define TEST_FILENAME
 //#define TEST_FTP
 //#define TEST_HASH
-//#define TEST_INFO_FUNCTIONS
+#define TEST_INFO_FUNCTIONS
 //#define TEST_LIST
 //#define TEST_LOCALE
 //#define TEST_LOG
-#define TEST_LONGLONG
+//#define TEST_LONGLONG
 //#define TEST_MIME
 //#define TEST_PATHLIST
 //#define TEST_REGCONF
@@ -1366,7 +1366,7 @@ static void TestMimeAssociate()
 
 static void TestDiskInfo()
 {
-    puts("*** Testing wxGetDiskSpace() ***\n");
+    puts("*** Testing wxGetDiskSpace() ***");
 
     for ( ;; )
     {
@@ -1385,9 +1385,9 @@ static void TestDiskInfo()
         }
         else
         {
-            wxPrintf(_T("%s total bytes, %s free bytes on '%s'.\n"),
-                    total.ToString().c_str(),
-                    free.ToString().c_str(),
+            wxPrintf(_T("%sKb total, %sKb free on '%s'.\n"),
+                    (total / 1024).ToString().c_str(),
+                    (free / 1024).ToString().c_str(),
                     pathname);
         }
     }
index f9db6a472b51f1a2914aa9e8784bf4208478bc68..686ff7d697f34c4a5a858bc51accbe79c09fa1f5 100644 (file)
@@ -449,6 +449,97 @@ bool wxDirExists(const wxString& dir)
 #endif // Win32/16
 }
 
+bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
+{
+    if ( path.empty() )
+        return FALSE;
+
+#ifdef __WIN32__
+    // GetDiskFreeSpaceEx() is not available under original Win95, check for
+    // it
+    typedef BOOL (*GetDiskFreeSpaceEx_t)(LPCTSTR,
+                                         PULARGE_INTEGER,
+                                         PULARGE_INTEGER,
+                                         PULARGE_INTEGER);
+
+    GetDiskFreeSpaceEx_t
+        pGetDiskFreeSpaceEx = ::GetProcAddress
+                              (
+                                ::GetModuleHandle(_T("kernel32.dll")),
+#if wxUSE_UNICODE
+                                "GetDiskFreeSpaceExW"
+#else
+                                "GetDiskFreeSpaceExA"
+#endif
+                              );
+
+    if ( pGetDiskFreeSpaceEx )
+    {
+        ULARGE_INTEGER bytesFree, bytesTotal;
+
+        // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
+        if ( !pGetDiskFreeSpaceEx(path,
+                                  &bytesFree,
+                                  &bytesTotal,
+                                  NULL) )
+        {
+            wxLogLastError(_T("GetDiskFreeSpaceEx"));
+
+            return FALSE;
+        }
+
+        if ( pTotal )
+        {
+            *pTotal = wxLongLong(bytesTotal.HighPart, bytesTotal.LowPart);
+        }
+
+        if ( pFree )
+        {
+            *pFree = wxLongLong(bytesFree.HighPart, bytesFree.LowPart);
+        }
+    }
+    else
+#endif // Win32
+    {
+        DWORD lSectorsPerCluster,
+              lBytesPerSector,
+              lNumberOfFreeClusters,
+              lTotalNumberOfClusters;
+
+        // FIXME: this is wrong, we should extract the root drive from path
+        //        instead, but this is the job for wxFileName...
+        if ( !::GetDiskFreeSpace(path,
+                                 &lSectorsPerCluster,
+                                 &lBytesPerSector,
+                                 &lNumberOfFreeClusters,
+                                 &lTotalNumberOfClusters) )
+        {
+            wxLogLastError(_T("GetDiskFreeSpace"));
+
+            return FALSE;
+        }
+
+        // there's a problem with drives larger than 2GB on non Win32!!
+        // let's calculate it, nevertheless....
+        wxLongLong lBytesPerCluster = lSectorsPerCluster;
+        lBytesPerCluster *= lBytesPerSector;
+
+        if ( pTotal )
+        {
+            *pTotal = lBytesPerCluster;
+            *pTotal *= lTotalNumberOfClusters;
+        }
+
+        if ( pFree )
+        {
+            *pFree = lBytesPerCluster;
+            *pFree *= lNumberOfFreeClusters;
+        }
+    }
+
+    return TRUE;
+}
+
 // ----------------------------------------------------------------------------
 // env vars
 // ----------------------------------------------------------------------------
index ad8a078fabd6c6d96e2bf1a4e514748e168e4634..3bd19ffbf6a38d18bd3c3ed5c44abebd7794f852 100644 (file)
 
 #include "wx/stream.h"
 
+#ifdef HAVE_STATFS
+    #include <sys/vfs.h>
+#endif // HAVE_STATFS
+
 #if wxUSE_GUI
     #include "wx/unix/execute.h"
 #endif
@@ -909,6 +913,34 @@ long wxGetFreeMemory()
     return -1;
 }
 
+bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
+{
+#ifdef HAVE_STATFS
+
+    struct statfs fs;
+    if ( statfs(path, &fs) != 0 )
+    {
+        wxLogSysError("Failed to get file system statistics");
+
+        return FALSE;
+    }
+
+    if ( pTotal )
+    {
+        *pTotal = wxLongLong(fs.f_blocks) * fs.f_bsize;
+    }
+
+    if ( pFree )
+    {
+        *pFree = wxLongLong(fs.f_bavail) * fs.f_bsize;
+    }
+
+    return TRUE;
+#endif // HAVE_STATFS
+
+    return FALSE;
+}
+
 // ----------------------------------------------------------------------------
 // env vars
 // ----------------------------------------------------------------------------