UPD: Some code optimizations

ADD: Functions description for uMyWindows unit
This commit is contained in:
Alexander Koblov 2008-10-18 11:21:33 +00:00
commit aeb076b02f
3 changed files with 53 additions and 28 deletions

View file

@ -548,21 +548,21 @@ end;
function IsAvailable(Path: String): Boolean;
{$IFDEF MSWINDOWS}
var
Drv: Char;
Drv: String;
DriveLabel: string;
begin
Drv := ExtractFileDrive(Path)[1];
Drv:= ExtractFileDrive(Path) + PathDelim;
{ Close CD/DVD }
if (GetDriveType(PChar(Drv + drive_root)) = DRIVE_CDROM) and
(not DriveReady(Drv)) then
if (GetDriveType(PChar(Drv)) = DRIVE_CDROM) and
(not mbDriveReady(Drv)) then
begin
DriveLabel:= mbGetVolumeLabel(Drv, False);
mbCloseCD(Drv);
if DriveReady(Drv) then
if mbDriveReady(Drv) then
mbWaitLabelChange(Drv, DriveLabel);
end;
Result:= DriveReady(Drv);
Result:= mbDriveReady(Drv);
end;
{$ELSE}
var

View file

@ -29,14 +29,37 @@ interface
uses
Classes, SysUtils, Windows;
const
drive_root: AnsiString = ':\';
function DriveReady(const Drv: Char): Boolean;
function mbGetVolumeLabel(const Drv: UTF8String; const VolReal: Boolean): UTF8String;
{en
Checks readiness of a drive
@param(sDrv String specifying the root directory of a file system volume)
@returns(The function returns @true if drive is ready, @false otherwise)
}
function mbDriveReady(const sDrv: UTF8String): Boolean;
{en
Get the label of a file system volume
@param(sDrv String specifying the root directory of a file system volume)
@param(bVolReal @true if it a real file system volume)
@returns(The function returns volume label)
}
function mbGetVolumeLabel(const sDrv: UTF8String; const bVolReal: Boolean): UTF8String;
{en
Set the label of a file system volume
@param(sRootPathName String specifying the root directory of a file system volume)
@param(sVolumeName String specifying a new name for the volume)
@returns(The function returns @true if successful, @false otherwise)
}
function mbSetVolumeLabel(sRootPathName, sVolumeName: UTF8String): Boolean;
procedure mbWaitLabelChange(const Drv: Char; const Str: UTF8String);
procedure mbCloseCD(const Drive: UTF8String);
{en
Wait for change disk label
@param(sDrv String specifying the root directory of a file system volume)
@param(sCurLabel Current volume label)
}
procedure mbWaitLabelChange(const sDrv: UTF8String; const sCurLabel: UTF8String);
{en
Close CD/DVD drive
@param(sDrv String specifying the root directory of a drive)
}
procedure mbCloseCD(const sDrv: UTF8String);
implementation
@ -57,17 +80,18 @@ end;
(* Drive ready *)
function DriveReady(const Drv: Char): Boolean;
function mbDriveReady(const sDrv: UTF8String): Boolean;
var
NotUsed: DWORD;
wsDrv: WideString;
begin
Result:= GetVolumeInformation(PChar(Drv + drive_root), nil, 0, nil,
NotUsed, NotUsed, nil, 0);
wsDrv:= UTF8Decode(sDrv);
Result:= GetVolumeInformationW(PWChar(wsDrv), nil, 0, nil, NotUsed, NotUsed, nil, 0);
end;
(* Disk label *)
function mbGetVolumeLabel(const Drv: UTF8String; const VolReal: Boolean): UTF8String;
function mbGetVolumeLabel(const sDrv: UTF8String; const bVolReal: Boolean): UTF8String;
var
WinVer: Byte;
DriveType, NotUsed: DWORD;
@ -76,21 +100,21 @@ var
wsResult: WideString;
begin
Result:= '';
wsDrv:= UTF8Decode(Drv);
wsDrv:= UTF8Decode(sDrv);
WinVer:= LOBYTE(LOWORD(GetVersion));
DriveType:= GetDriveTypeW(PWChar(wsDrv));
if (WinVer <= 4) and (DriveType <> DRIVE_REMOVABLE) or VolReal then
if (WinVer <= 4) and (DriveType <> DRIVE_REMOVABLE) or bVolReal then
begin // Win9x, Me, NT <= 4.0
Buf[0]:= #0;
GetVolumeInformationW(PWChar(wsDrv), Buf, DWORD(SizeOf(Buf)), nil,
NotUsed, NotUsed, nil, 0);
wsResult:= Buf;
if VolReal and (WinVer >= 5) and (Result <> '') and
if bVolReal and (WinVer >= 5) and (Result <> '') and
(DriveType <> DRIVE_REMOVABLE) then // Win2k, XP and higher
wsResult:= DisplayName(wsDrv)
else if (Result = '') and (not VolReal) then
else if (Result = '') and (not bVolReal) then
wsResult:= '<none>';
end
else
@ -110,27 +134,27 @@ begin
Result:= SetVolumeLabelW(PWChar(wsRootPathName), PWChar(wsVolumeName));
end;
procedure mbWaitLabelChange(const Drv: Char; const Str: UTF8String);
procedure mbWaitLabelChange(const sDrv: UTF8String; const sCurLabel: UTF8String);
var
st1, st2: string;
st1, st2: UTF8String;
begin
if mbGetVolumeLabel(Drv, True) = '' then
if mbGetVolumeLabel(sDrv, True) = '' then
Exit;
st1:= TrimLeft(Str);
st1:= TrimLeft(sCurLabel);
st2:= st1;
while st1 = st2 do
st2:= mbGetVolumeLabel(Drv, FALSE);
st2:= mbGetVolumeLabel(sDrv, FALSE);
end;
(* Close CD/DVD *)
procedure mbCloseCD(const Drive: UTF8String);
procedure mbCloseCD(const sDrv: UTF8String);
var
OpenParms: MCI_OPEN_PARMS;
begin
FillChar(OpenParms, SizeOf(OpenParms), 0);
OpenParms.lpstrDeviceType:= 'CDAudio';
OpenParms.lpstrElementName:= PChar(Drive + ':');
OpenParms.lpstrElementName:= PChar(ExtractFileDrive(sDrv));
mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE or MCI_OPEN_ELEMENT, Longint(@OpenParms));
mciSendCommand(OpenParms.wDeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, 0);
mciSendCommand(OpenParms.wDeviceID, MCI_CLOSE, MCI_OPEN_TYPE or MCI_OPEN_ELEMENT, Longint(@OpenParms));

View file

@ -6,3 +6,4 @@ uvfs.pas
uexts.pas
ufileprocs.pas
udescr.pas
umywindows.pas