1 | #include <windows.h>
|
---|
2 |
|
---|
3 | #include "DiskSpace.h"
|
---|
4 |
|
---|
5 | typedef BOOL (* diskEx) (LPCTSTR root, PULARGE_INTEGER a, PULARGE_INTEGER B, PULARGE_INTEGER C);
|
---|
6 |
|
---|
7 | DiskSpace::DiskSpace(const char *rootName)
|
---|
8 | {
|
---|
9 | char rootPath[128];
|
---|
10 | DWORD totalClusters;
|
---|
11 | OSVERSIONINFO osVerInfo;
|
---|
12 |
|
---|
13 | // copy the rootname to our own path
|
---|
14 | strcpy(rootPath, rootName);
|
---|
15 |
|
---|
16 | // get some guide to the os version
|
---|
17 | osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
18 | GetVersionEx(&osVerInfo);
|
---|
19 |
|
---|
20 | // if get disk free space fails then note fatal initialisation error
|
---|
21 | if (!GetDiskFreeSpace(rootName, §orsPerCluster, &bytesPerSector,
|
---|
22 | &freeClusters, &totalClusters))
|
---|
23 | { this->initialised = FALSE;
|
---|
24 | return;
|
---|
25 | }
|
---|
26 |
|
---|
27 | // call the advanced form of getDiskSpace if we have access to it; this
|
---|
28 | // will cope with 2Gb+ partitions correctly
|
---|
29 | if (osVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
|
---|
30 | LOWORD(osVerInfo.dwBuildNumber) > 1000 ||
|
---|
31 | TRUE)
|
---|
32 | { HMODULE module;
|
---|
33 | FARPROC proc;
|
---|
34 | ULARGE_INTEGER freeBytes, freeToCaller, totalBytes;
|
---|
35 |
|
---|
36 | module = LoadLibrary("KERNEL32.DLL");
|
---|
37 | proc = GetProcAddress(module, "GetDiskFreeSpaceEx");
|
---|
38 | if (proc == NULL)
|
---|
39 | { this->initialised = TRUE;
|
---|
40 | return;
|
---|
41 | }
|
---|
42 | (* ((diskEx) proc))(rootPath, &freeBytes, &freeToCaller, &totalBytes);
|
---|
43 | // Get free clusters - use the ULARGE_INTEGER variables to get the
|
---|
44 | // result and avoid overflow problems
|
---|
45 | //freeBytes = freeBytes / sectorsPerCluster; //(sectorsPerCluster * bytesPerSector);
|
---|
46 | //freeClusters = totalBytes;
|
---|
47 | }
|
---|
48 | this->initialised = TRUE;
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | BOOL DiskSpace::initialisedOk()
|
---|
53 | { return this->initialised;
|
---|
54 | }
|
---|
55 |
|
---|
56 | unsigned long DiskSpace::totalFreeSpace()
|
---|
57 | { return this->freeClusters * this->sectorsPerCluster * this->bytesPerSector;
|
---|
58 | }
|
---|
59 |
|
---|
60 | int DiskSpace::fileClusters(int fileSize)
|
---|
61 | { int sectors, clusters;
|
---|
62 |
|
---|
63 | sectors = (fileSize + this->bytesPerSector - 1) / this->bytesPerSector;
|
---|
64 | clusters = (sectors + this->sectorsPerCluster - 1) / this->sectorsPerCluster;
|
---|
65 | return clusters;
|
---|
66 | }
|
---|
67 |
|
---|
68 | int DiskSpace::fileSpace(int fileSize)
|
---|
69 | { return this->fileClusters(fileSize) * this->bytesPerSector * this->sectorsPerCluster;
|
---|
70 | }
|
---|
71 |
|
---|