home *** CD-ROM | disk | FTP | other *** search
/ PC Open 19 / pcopen19.iso / Win31 / Calmira / SOURCE.ZIP / UTILS / DRIVES.PAS next >
Encoding:
Pascal/Delphi Source File  |  1998-02-20  |  6.0 KB  |  246 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {    Calmira System Library 2.1                           }
  4. {    by Li-Hsin Huang,                                    }
  5. {    released into the public domain January 1998         }
  6. {                                                         }
  7. {*********************************************************}
  8.  
  9. unit Drives;
  10.  
  11. { Disk drive detecting functions }
  12.  
  13. interface
  14.  
  15. uses FileCtrl; { defines TDriveType }
  16.  
  17. const
  18.   DriveDesc : array[TDriveType] of string[15] =
  19.     ('Unknown drive', 'No drive', 'Floppy drive', 'Hard drive', 'Network drive',
  20.      'CD-ROM drive', 'RAM disk');
  21.  
  22. type
  23.   TDriveRange = 'A'..'Z';
  24.   TDriveFlag = (dfValid, dfFloppy, dfFixed, dfNetwork, dfCDROM, dfRAM,
  25.                 dfRemoveable, dfWriteable);
  26.   TDriveFlags = set of TDriveFlag;
  27.  
  28. var
  29.   ValidDrives : set of TDriveRange;
  30.  
  31.  
  32. function DriveNumber(Drive : Char) : Integer;
  33. { Maps drive characters to Integers used by Delphi. A = 1, B = 2, ... }
  34.  
  35. function WinDriveNumber(Drive: Char): Integer;
  36. { Maps drive characters to Integers used by Windows. A = 0, B = 1, ... }
  37.  
  38. function FindDriveType(Drive: Char): TDriveType;
  39. { Detects the type of the specified drive }
  40.  
  41. function GuessDriveType(Drive : Char) : TDriveType;
  42. { Returns the type of the drive without needing disk access --
  43.   the information is obtained and stored during initialization }
  44.  
  45. function GetDriveFlags(drive: Char) : TDriveFlags;
  46. { Returns the flag set for the given drive }
  47.  
  48. function IsRAMDrive(DriveNum: Integer): Boolean;
  49. { Returns true if the drive is a RAM disk }
  50.  
  51. function IsCDROM(DriveNum: Integer): Boolean;
  52. { Returns true if the drive is a CD-ROM drive }
  53.  
  54. function GetNetworkVolume(Drive: Char): string;
  55. { Returns the network name of the drive if available, otherwise
  56.   just returns the volume label }
  57.  
  58. function GetVolumeID(Drive: Char): string;
  59. { Returns the volume label of the disk }
  60.  
  61. procedure DetectDrives;
  62. { Refreshes this unit's information about drives on the system.
  63.   Called automatically during initialization. }
  64.  
  65. function SystemDrivesChanged : Boolean;
  66.  
  67.  
  68. function IsDriveString(const s: string): Boolean;
  69.  
  70.  
  71. implementation
  72.  
  73. uses SysUtils, WinProcs, WinTypes;
  74.  
  75. var
  76.   DriveFlags  : array[TDriveRange] of TDriveFlags;
  77.   DriveTypes  : array[TDriveRange] of TDriveType;
  78.  
  79.  
  80. function DriveNumber(Drive : Char) : Integer;
  81. begin
  82.   Result := Ord(UpCase(Drive)) - Ord('A') + 1;
  83. end;
  84.  
  85.  
  86. function WinDriveNumber(Drive: Char): Integer;
  87. begin
  88.   Result := DriveNumber(Drive) - 1;
  89. end;
  90.  
  91.  
  92. function IsCDROM(DriveNum: Integer): Boolean; assembler;
  93. asm
  94.   MOV   AX, 1500h { look for MSCDEX }
  95.   XOR   BX, BX
  96.   INT   2Fh
  97.   OR    BX, BX
  98.   JZ    @Finish
  99.   MOV   AX, 150Bh { check for using CD driver }
  100.   MOV   CX, DriveNum
  101.   INT   2fh
  102.   OR    AX, AX
  103.   @Finish:
  104. end;
  105.  
  106.  
  107. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  108. var
  109.   Temp: Boolean;
  110. asm
  111.   MOV   Temp, False
  112.   PUSH  DS
  113.   MOV   BX, SS
  114.   MOV   DS, BX
  115.   SUB   SP, 0200h
  116.   MOV   BX, SP
  117.   MOV   AX, DriveNum
  118.   MOV   CX, 1
  119.   XOR   DX, DX
  120.   INT   25h  { read boot sector }
  121.   ADD   SP, 2
  122.   JC    @@1
  123.   MOV   BX, SP
  124.   CMP   BYTE PTR SS:[BX+15h], 0F8h  { reverify fixed disk }
  125.   JNE   @@1
  126.   CMP   BYTE PTR SS:[BX+10h], 1     { check for single FAT }
  127.   JNE   @@1
  128.   MOV   Temp, True
  129. @@1:
  130.   ADD   SP, 0200h
  131.   POP   DS
  132.   MOV   AL, Temp
  133. end;
  134.  
  135.  
  136. function FindDriveType(Drive: Char): TDriveType;
  137. var
  138.   n : Integer;
  139. begin
  140.   n := WinDriveNumber(Drive);
  141.  
  142.   case GetDriveType(n) of
  143.     0               : Result := dtNoDrive;
  144.     DRIVE_REMOVABLE : Result := dtFloppy;
  145.     DRIVE_FIXED     : if IsRAMDrive(n) then Result := dtRAM
  146.                       else Result := dtFixed;
  147.     DRIVE_REMOTE    : if IsCDROM(n) then Result := dtCDROM
  148.                       else Result := dtNetwork;
  149.   end;
  150. end;
  151.  
  152.  
  153.  
  154. procedure DetectDrives;
  155. var
  156.   d: Char;
  157. begin
  158.   ValidDrives := [];
  159.   for d := 'A' to 'Z' do begin
  160.     DriveTypes[d] := FindDriveType(d);
  161.     case DriveTypes[d] of
  162.       dtUnknown : DriveFlags[d] := [dfValid, dfFixed, dfWriteable];
  163.       dtNoDrive : DriveFlags[d] := [];
  164.       dtFloppy  : DriveFlags[d] := [dfValid, dfFloppy, dfRemoveable, dfWriteable];
  165.       dtFixed   : DriveFlags[d] := [dfValid, dfFixed, dfWriteable];
  166.       dtNetwork : DriveFlags[d] := [dfValid, dfNetwork, dfWriteable];
  167.       dtCDROM   : DriveFlags[d] := [dfValid, dfCDROM, dfRemoveable];
  168.       dtRAM     : DriveFlags[d] := [dfValid, dfRAM, dfWriteable];
  169.     end;
  170.     if dfValid in DriveFlags[d] then Include(ValidDrives, d);
  171.   end;
  172. end;
  173.  
  174. function SystemDrivesChanged : Boolean;
  175. var d: Char;
  176. begin
  177.   Result := False;
  178.   for d := 'A' to 'Z' do
  179.     if (DriveTypes[d] = dtNoDrive) xor (GetDriveType(WinDriveNumber(d)) = 0) then begin
  180.       Result := True;
  181.       Exit;
  182.     end;
  183. end;
  184.  
  185.  
  186. function GuessDriveType(Drive : Char) : TDriveType;
  187. begin
  188.   Drive := UpCase(Drive);
  189.   if Drive in ['A'..'Z'] then Result := DriveTypes[Drive]
  190.   else Result := dtNoDrive;
  191. end;
  192.  
  193.  
  194. function GetDriveFlags(Drive: Char) : TDriveFlags;
  195. begin
  196.   Drive := UpCase(Drive);
  197.   if Drive in ['A'..'Z'] then Result := DriveFlags[Drive]
  198.   else Result := [];
  199. end;
  200.  
  201. function Capitalize(const s: string): string;
  202. begin
  203.   Result := Lowercase(s);
  204.   Result[1] := Upcase(Result[1]);
  205. end;
  206.  
  207. function GetVolumeID(Drive: Char): string;
  208. var
  209.   SR: TSearchRec;
  210. begin
  211.   Result := '';
  212.   if FindFirst(Drive + ':\*.*', faVolumeID, SR) = 0 then
  213.     Result := Capitalize(SR.Name);
  214. end;
  215.  
  216.  
  217.  
  218. function GetNetworkVolume(Drive: Char): string;
  219. const
  220.   LocalName: array[0..2] of Char = 'C:'#0;
  221. var
  222.   BufSize: Word;
  223.   Temp: array[0..128] of Char;
  224. begin
  225.   LocalName[0] := Drive;
  226.   BufSize := 127;
  227.   if WNetGetConnection(LocalName, Temp, @BufSize) = WN_SUCCESS then
  228.     Result := Capitalize(StrPas(Temp))
  229.   else
  230.     Result := GetVolumeID(Drive);
  231. end;
  232.  
  233.  
  234. function IsDriveString(const s: string): Boolean;
  235. begin
  236.   Result := (Length(s) = 3) and (UpCase(s[1]) in ['A'..'Z'])
  237.             and (s[2] = ':') and (s[3] = '\');
  238. end;
  239.  
  240.  
  241.  
  242.  
  243. initialization
  244.   DetectDrives;
  245. end.
  246.