root/trunk/filesystem/getlogicaldrives.d

Revision 19, 1.9 kB (checked in by BCS, 2 years ago)

added some filesystem code by Regan Heath

Line 
1 /// Author: Regan Heath
2
3 import std.stdio, std.string;
4
5 alias std.string.toStringz CSTR;
6
7 import std.c.windows.windows;
8
9 extern(Windows)
10 {
11     DWORD GetLogicalDrives();
12     UINT GetDriveTypeA(LPCSTR lpRootPathName);
13     UINT GetDriveTypeW(LPCWSTR lpRootPathName);
14     const UINT DRIVE_UNKNOWN     = 0; //The drive type cannot be determined.
15     const UINT DRIVE_NO_ROOT_DIR = 1; //The root path is invalid; for example, there is no volume is mounted at the path.
16     const UINT DRIVE_REMOVABLE   = 2; //The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
17     const UINT DRIVE_FIXED       = 3; //The drive has fixed media; for example, a hard drive or flash drive.
18     const UINT DRIVE_REMOTE      = 4; //The drive is a remote (network) drive.
19     const UINT DRIVE_CDROM       = 5; //The drive is a CD-ROM drive.
20     const UINT DRIVE_RAMDISK     = 6; //The drive is a RAM disk.
21 }
22
23 char[][] LocalDrives()
24 {
25     DWORD mask = GetLogicalDrives();
26     char[] drive = new char[3];
27     char[][] list; 
28    
29     drive[1..3] = ":\\";   
30     for(int i = 0; i < 26; i++)
31     {
32         if (mask & 0x1<<i)
33         {
34             drive[0] = 'A'+i;
35             list ~= drive.dup;
36         }       
37     }
38    
39     return list;
40 }
41
42 template SortDrive(UINT TYPE) { char[][] SortDrive() {
43     char[][] list;
44    
45     foreach(drive; LocalDrives())
46     {
47         switch(GetDriveTypeA(CSTR(drive)))
48         {
49             case TYPE:
50                 list ~= drive;
51                 break;
52             default:
53                 break;
54         }
55     }
56    
57     return list;
58 }}
59
60 alias SortDrive!(DRIVE_FIXED)       FixedDrives;
61 alias SortDrive!(DRIVE_REMOVABLE)   RemovableDrives;
62 alias SortDrive!(DRIVE_REMOTE)      RemoteDrives;
63 alias SortDrive!(DRIVE_CDROM)       CdDrives;
64 alias SortDrive!(DRIVE_RAMDISK)     RamDisks;
65
66 unittest
67 {
68     writef("Fixed    : ",FixedDrives(),"\n");
69     writef("Removable: ",RemovableDrives(),"\n");
70     writef("Remote   : ",RemoteDrives(),"\n");
71     writef("Cd       : ",CdDrives(),"\n");
72     writef("Ramdisk  : ",RamDisks(),"\n");
73 }
Note: See TracBrowser for help on using the browser.