{-----------------------------------------------------------------------------
 Unit Name: uCPUMem
 Author:    8jie
 Purpose:   硬盘信息相关公共函数，
            将常用的函数精简出来，以供开发者更简单的开发脚本
 History:
   2009-05-08: + Create this file
-----------------------------------------------------------------------------
           Copyright(c) 8-jie 2009
-----------------------------------------------------------------------------}

//打开指定盘符
procedure OpenDrive(aDriveName:string);
begin
  ShellExecute(0,'open',aDriveName+':\','','',1);
end;

//将文件大小转换成合适的单位，此处的参数以M为单位，因为GetDriveInfo取回的磁盘信息是以M为单位的，而不是字节，注意！！！
function GetMSize(value:Integer):string;
var
  i:integer;
  f:double;
  s:string;
begin
  result:='';
  //计算剩余容量
  i:=0;
  f:=value;
  while f>1024 do
  begin
    inc(i);
    f:=f/1024;
  end;
  case i of
    0: s:='MB';
    1: s:='GB';
    2: s:='TB';
  else
    s:='MB';
  end;
  //下面这一行会导致程序异常，不知道为什么:(
  //result:=format('%.1f %s',[f,s]);
  i:=round(f*10);
  if i mod 10=0 then
    result:=format('%d %s',[i div 10,s])
  else
    result:=format('%.1f %s',[i/10,s]);
end;

{function GetDrives:string;
var
  lst:TStrings;
  i:integer;
begin
  result:='';
  lst:=TStringList.create;
  try
    lst.text:=System.Shell.GetDriveList;
    if lst.count>0 then
      for i:=0 to lst.count-1 do
        if trim(lst.strings[i])<>'' then
          result:=result+lst.strings[i];
  finally
    lst.free;
  end;
end;}

//获取有效的磁盘列表，未插入光盘的光驱不算有效的磁盘
function GetValidDrives:string;
var
  isReady:boolean;
  iTotal,iFree, iPer,iType:integer;
  sVolume:string;
  sDriveFormat:string;

  lst:TStrings;
  i:integer;
begin
  result:='';
  lst:=TStringList.create;
  try
    lst.text:=System.Shell.GetDriveList;
    if lst.count>0 then
      for i:=0 to lst.count-1 do
      if (trim(lst.strings[i])<>'') then
      begin              
        System.Shell.GetDriveInfo(lst.strings[i],isReady,sVolume,iFree,iTotal,iPer,iType,sDriveFormat);
        if isReady then
          result:=result+lst.strings[i];
      end;
  finally
    lst.free;
  end;
end;

//磁盘类型转字符
function DiskTypeToStr(aType:integer):string;
begin
  //类型 0:无法识别 1:Floppy 2:Removable 3:Fixed 4:Remote (network) 5:CD-ROM 6:RAM
  case aType of
    1: result:='软驱';
    2: result:='移动磁盘';
    3: result:='本地磁盘'; //即硬盘
    4: result:='远程磁盘';
    5: result:='光盘驱动器';
    6: result:='RAM';
  else
    result:='未知磁盘';
  end;
end;