home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / zkuste / delphi / kompon / d5 / CAKDIR.ZIP / RsSupp.pas < prev    next >
Pascal/Delphi Source File  |  2001-05-23  |  11KB  |  357 lines

  1. unit RsSupp;
  2.  
  3. interface
  4.  
  5. uses Windows, Messages, SysUtils, FileCtrl, Classes,
  6.   Graphics, Controls, Forms, Dialogs, ErrorUnit, ArchiveHeadersUnit, ResourceCompUnit;
  7.  
  8. (*******************************************************************************
  9.   Column Data Extractor types
  10.  
  11.   Desc:
  12.   Each column in the FileList is represented by a class derived from TColDataExtr.
  13.   This makes each column capable of extracting its own data, info (header width,
  14.   header), and sort itself (ListSortCompare).
  15.  
  16.   Each child of TColDataExtr
  17.     - assigns its own header, width and ListSortCompare in the Create procedure.
  18.     - overrides the Extract procedure to return the data it exposes in a string
  19.  
  20.   To add a new column:
  21.   Derive a new column from TColDataExtr
  22.   Override Create:
  23.     - call the inherited create to give a header title and a width
  24.     - assign a pointer to the compare procedure if any
  25.   Override Extract to return a string for the data
  26. *******************************************************************************)
  27.  
  28. type
  29.   TColDataExtr = class
  30.   protected
  31.     fheader: string;
  32.     fwidth: integer;
  33.     FListSortCompare: TListSortCompare;
  34.   public
  35.     property header: string read fheader;
  36.     property Width: integer read fwidth;
  37.     property ListSortCompare: TListSortCompare read FListSortCompare;
  38.     constructor Create(aheader: string; awidth: integer);
  39.     function Extract(CFH: TCentralFileHeader): string; virtual; abstract;
  40.   end;
  41.  
  42.   TNameColDataExtr = class(TColDataExtr)
  43.   public
  44.     constructor Create;
  45.     function Extract(CFH: TCentralFileHeader): string; override;
  46.   end;
  47.  
  48.   TSizeColDataExtr = class(TColDataExtr)
  49.   public
  50.     constructor Create;
  51.     function Extract(CFH: TCentralFileHeader): string; override;
  52.   end;
  53.  
  54.   TPackedColDataExtr = class(TColDataExtr)
  55.   public
  56.     constructor Create;
  57.     function Extract(CFH: TCentralFileHeader): string; override;
  58.   end;
  59.  
  60.   TTimeColDataExtr = class(TColDataExtr)
  61.   public
  62.     constructor Create;
  63.     function Extract(CFH: TCentralFileHeader): string; override;
  64.   end;
  65.  
  66.   TRatioColDataExtr = class(TColDataExtr)
  67.   public
  68.     constructor Create;
  69.     function Extract(CFH: TCentralFileHeader): string; override;
  70.   end;
  71.  
  72.   TTypeNameColDataExtr = class(TColDataExtr)
  73.   public
  74.     constructor Create;
  75.     function Extract(CFH: TCentralFileHeader): string; override;
  76.   end;
  77.  
  78.   TNumBlocksColDataExtr = class(TColDataExtr)
  79.   public
  80.     constructor Create;
  81.     function Extract(CFH: TCentralFileHeader): string; override;
  82.   end;
  83.  
  84.   TDataOffsetColDataExtr = class(TColDataExtr)
  85.   public
  86.     constructor Create;
  87.     function Extract(CFH: TCentralFileHeader): string; override;
  88.   end;
  89.  
  90. implementation
  91.  
  92.  
  93. {-------------------------------------------------------------------------------
  94.   GetCompressionRatio
  95.   -------------------
  96.   Returns the compression ratio calculate from compressed and uncompresse
  97.   Notes: The compression ratio is a percentage describing the ratio the file
  98.   has shrunk by i.e. if the compression ratio is 30%, the file is 70% of its
  99.   original size.
  100.   This form of description is used Winzip, Arj and other major archivers.
  101. -------------------------------------------------------------------------------}
  102. function GetCompressionRatio(compressed, uncompressed: integer): integer;
  103. begin
  104.   if Uncompressed > 0 then
  105.     Result := 100 - (compressed * 100 div uncompressed)
  106.   else
  107.     Result := 0;
  108.   {try
  109.   except
  110.     on EDivByZero do
  111.       result := 0;
  112.   end;}
  113. end;
  114.  
  115. {-------------------------------------------------------------------------------
  116.   GetBitsPerByte
  117.   --------------
  118.   Gives an approximation of the bits per byte for a file.
  119.   The number of bits is rounded to next 8 bits because the exact value is
  120.   not known. It is calculated by multiplying compressed by 8.
  121. -------------------------------------------------------------------------------}
  122. function GetBitsPerByte(compressed, uncompressed: integer): extended;
  123. begin
  124.   try
  125.     if Uncompressed > 0 then
  126.       Result := compressed / uncompressed * 8
  127.     else
  128.       Result := 0;
  129.   except
  130.     on EInvalidOp do // Div by zero
  131.       Result := 0;
  132.   end;
  133. end;
  134.  
  135. function GetBitsPerByteStr(compressed, uncompressed: integer): string;
  136. var
  137.   bpb: extended;  // bits per byte
  138.   s:   string;  // result string
  139. begin
  140.   bpb := GetBitsPerByte(compressed, uncompressed);
  141.   Str(bpb: 5: 3, s);
  142.   Result := s;
  143. end;
  144.  
  145. (*******************************************************************************
  146.   Column Sort support
  147. *******************************************************************************)
  148.  
  149. {-------------------------------------------------------------------------------
  150.   InverseCompare
  151.   --------------
  152.   Compares Item1 and Item2, and returns the inverse of the result.
  153.   Uses the actual comparison function pointed to by InverseCompareActual to
  154.   do the actual comparison. Then internally reverses the result.
  155. -------------------------------------------------------------------------------}
  156. var
  157.   InverseCompareActual: TListSortCompare;
  158.  
  159. function InverseCompare(Item1, Item2: Pointer): integer;
  160. var
  161.   d: integer;
  162. begin
  163.   d := InverseCompareActual(Item1, Item2);
  164.   if (d > 0) then d := -1
  165.   else if (d < 0) then d := 1;
  166.   Result := d;
  167. end;
  168.  
  169. {-------------------------------------------------------------------------------
  170.   CompareInt
  171.   ----------
  172.   Compares two integers a and b.
  173.   Returns:
  174.     1  : a > b
  175.     -1 : a < b
  176.     0  : a = b
  177. -------------------------------------------------------------------------------}
  178. function CompareInt(a, b: integer): integer;
  179. begin
  180.   if a > b then
  181.     Result := 1
  182.   else if a < b then
  183.     Result := -1
  184.   else
  185.     Result := 0;
  186. end;
  187.  
  188. {-------------------------------------------------------------------------------
  189.   Various comparison functions
  190.  
  191.   Notes:
  192.   The variuos compare function compares a field in Item1 and Item2. Depending
  193.   on the data type of the fields, a different comparison method is used.
  194.   CompareStr: to compare strings
  195.   CompareInt: to compare integers
  196. -------------------------------------------------------------------------------}
  197. function NameCompare(Item1, Item2: Pointer): integer;
  198. begin
  199.   Result := CompareStr(TCentralFileHeader(Item1).filename,
  200.     TCentralFileHeader(Item2).filename);
  201. end;
  202.  
  203. function SizeCompare(Item1, Item2: Pointer): integer;
  204. begin
  205.   Result := CompareInt(TCentralFileHeader(Item1).uncompressed_size,
  206.     TCentralFileHeader(Item2).uncompressed_size);
  207. end;
  208.  
  209. function PackedCompare(Item1, Item2: Pointer): integer;
  210. begin
  211.   Result := CompareInt(TCentralFileHeader(Item1).compressed_size,
  212.     TCentralFileHeader(Item2).compressed_size);
  213. end;
  214.  
  215. function RatioCompare(Item1, Item2: Pointer): integer;
  216. var
  217.   r1, r2: integer;
  218. begin
  219.   r1     := GetCompressionRatio(TCentralFileHeader(Item1).compressed_size,
  220.     TCentralFileHeader(Item1).uncompressed_size);
  221.   r2     := GetCompressionRatio(TCentralFileHeader(Item2).compressed_size,
  222.     TCentralFileHeader(Item2).uncompressed_size);
  223.   Result := CompareInt(r1, r2);
  224. end;
  225.  
  226. function TimeCompare(Item1, Item2: Pointer): integer;
  227. begin
  228.   Result := CompareInt(TCentralFileHeader(Item1).Time, TCentralFileHeader(Item2).Time);
  229. end;
  230.  
  231. function TypeNameCompare(Item1, Item2: Pointer): integer;
  232. begin
  233.   Result := CompareStr(TCentralFileHeader(Item1).ShellTypeName,
  234.     TCentralFileHeader(Item2).ShellTypeName);
  235. end;
  236.  
  237. (*******************************************************************************
  238.   Column Data Extractor types
  239.  
  240.   Desc:
  241.   Each column in the FileList is represented by a class derived from TColDataExtr.
  242.   This makes each column capable of extracting its own data, info (header width,
  243.   header), and sort itself (ListSortCompare).
  244.  
  245.   Each child of TColDataExtr
  246.     - assigns its own header, width and ListSortCompare in the Create procedure.
  247.     - overrides the Extract procedure to return the data it exposes in a string
  248.  
  249.   To add a new column:
  250.   Derive a new column from TColDataExtr
  251.   Override Create:
  252.     - call the inherited create to give a header title and a width
  253.     - assign a pointer to the compare procedure if any
  254.   Override Extract to return a string for the data
  255. *******************************************************************************)
  256.  
  257. {-------------------------------------------------------------------------------
  258.   Column Data Extractors
  259. -------------------------------------------------------------------------------}
  260. constructor TColDataExtr.Create;
  261. begin
  262.   inherited Create;
  263.   fheader          := aheader;
  264.   fwidth           := awidth;
  265.   FListSortCompare := nil;
  266. end;
  267.  
  268. constructor TNameColDataExtr.Create;
  269. begin
  270.   inherited Create('Name', 140);
  271.   FListSortCompare := NameCompare;
  272. end;
  273.  
  274. function TNameColDataExtr.Extract(CFH: TCentralFileHeader): string;
  275. begin
  276.   Result := CFH.filename;
  277. end;
  278.  
  279. constructor TSizeColDataExtr.Create;
  280. begin
  281.   inherited Create('Size', 100);
  282.   FListSortCompare := SizeCompare;
  283. end;
  284.  
  285. function TSizeColDataExtr.Extract(CFH: TCentralFileHeader): string;
  286. begin
  287.   Result := IntToStr(CFH.uncompressed_size);
  288. end;
  289.  
  290. constructor TPackedColDataExtr.Create;
  291. begin
  292.   inherited Create('Packed', 100);
  293.   FListSortCompare := PackedCompare;
  294. end;
  295.  
  296. function TPackedColDataExtr.Extract(CFH: TCentralFileHeader): string;
  297. begin
  298.   Result := IntToStr(CFH.compressed_size);
  299. end;
  300.  
  301. constructor TTimeColDataExtr.Create;
  302. begin
  303.   inherited Create('Time', 120);
  304.   FListSortCompare := TimeCompare;
  305. end;
  306.  
  307. function TTimeColDataExtr.Extract(CFH: TCentralFileHeader): string;
  308. begin
  309.   Result := CFH.timeStr; // info cached
  310. end;
  311.  
  312. constructor TRatioColDataExtr.Create;
  313. begin
  314.   inherited Create('Ratio', 50);
  315.   FListSortCompare := RatioCompare;
  316. end;
  317.  
  318. constructor TTypeNameColDataExtr.Create;
  319. begin
  320.   inherited Create('Type', 130);
  321.   FListSortCompare := TypeNameCompare;
  322. end;
  323.  
  324. function TTypeNameColDataExtr.Extract(CFH: TCentralFileHeader): string;
  325. begin
  326.   Result := CFH.shelltypename;
  327. end;
  328.  
  329. function TRatioColDataExtr.Extract(CFH: TCentralFileHeader): string;
  330. begin
  331.   Result := IntToStr(GetCompressionRatio(CFH.compressed_size, CFH.uncompressed_size)) +
  332.     '%';
  333. end;
  334.  
  335. constructor TNumBlocksColDataExtr.Create;
  336. begin
  337.   inherited Create('Blocks', 50);
  338. end;
  339.  
  340. function TNumBlocksColDataExtr.Extract(CFH: TCentralFileHeader): string;
  341. begin
  342.   Result := IntToStr(CFH.num_blocks);
  343. end;
  344.  
  345. constructor TDataOffsetColDataExtr.Create;
  346. begin
  347.   inherited Create('Data', 50);
  348. end;
  349.  
  350. function TDataOffsetColDataExtr.Extract(CFH: TCentralFileHeader): string;
  351. begin
  352.   Result := IntToStr(CFH.data_offset);
  353. end;
  354.  
  355.  
  356.  
  357. end.