home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / nastroje / d5 / MFTP.ZIP / src / FTPData.pas < prev    next >
Pascal/Delphi Source File  |  2001-03-05  |  10KB  |  433 lines

  1. unit FtpData;
  2.  
  3. {Infomation management implementation of Monster FTP}
  4.  
  5. interface
  6.  
  7. uses Classes, SysUtils, Windows, ShellApi, FtpMisc;
  8.  
  9. {$I mftp.inc}
  10.  
  11. const
  12.    MaxItem           =  7;
  13.    ItemAttrib        =  0;
  14.    ItemDateTime      =  1;
  15.    ItemSize          =  2;
  16.    ItemSymbolLink    =  3;
  17.    ItemDescription   =  4;
  18.    ItemFilename      =  5;
  19.    ItemOwner         =  6;
  20.    ItemGroup         =  7;
  21.    
  22.    { fill in dynamically}
  23.    ItemFileType      =  8;
  24.    ItemDateTimeValue = -1;
  25.  
  26. type
  27.    MFtpFileInfo = record
  28.       Attrib:              String;
  29.       DateTime:            String;
  30.       DateTimeValue:       String;
  31.       Description:         String;
  32.       Filename:            String;
  33.       Size:                String;
  34.       SymbolLink:          String;
  35.       Owner:               String;
  36.       Group:               String;
  37.    end;
  38.  
  39.    TGetString = function (I: Integer): String of Object;
  40.  
  41.    TMFtpFileInfoList = class(TObject)
  42.    private
  43.       FData: Array[-1..MaxItem] of TStringList;
  44.  
  45.       FCount:               Integer;
  46.  
  47.       function Get(I: Integer): MFtpFileInfo;
  48.       procedure Put(I: Integer; const P: MFtpFileInfo);
  49.  
  50.       function gsFilename(I: Integer): String;
  51.       function gsAttrib(I: Integer): String;
  52.       function gsDateTimeValue(I: Integer): String;
  53.       function gsDescription(I: Integer): String;
  54.       function gsFileType(I: Integer): String;
  55.       function gsSize(I: Integer): String;
  56.       function gsOwner(I: Integer): String;
  57.       function gsGroup(I: Integer): String;
  58.    public
  59.       constructor Create;
  60.       destructor Destroy; override;
  61.       procedure Assign(Source: TMFtpFileInfoList);
  62.  
  63.       procedure Add(S1, S2, S3, S4, S5, S6, S7, S8: String); overload;
  64.       procedure Add(const P: MFtpFileInfo); overload;
  65.       procedure Clear;
  66.       procedure ClearPart(ItemType: Integer);
  67.       procedure Delete(I: Integer);
  68.       procedure MyFree;
  69.       procedure Sort(ItemType: Integer; Ascending: Boolean);
  70.       procedure Swap(I1, I2: Integer);
  71.  
  72.       function IndexOf(S: String): Integer;
  73.  
  74. //    function GetData(I, ItemType: Integer): String;
  75.       procedure PutData(I, ItemType: Integer; Value: String);
  76.  
  77.       property Count: Integer read FCount;
  78.       property Items[Index: Integer]: MFtpFileInfo read Get write Put; default;
  79.    end;
  80.  
  81. implementation
  82.  
  83. constructor TMFtpFileInfoList.Create;
  84. var i: Integer;
  85. begin
  86.    inherited Create;
  87.  
  88.    for i := -1 to MaxItem do FData[i] := TStringList.Create;
  89. end;
  90.  
  91. destructor TMFtpFileInfoList.Destroy;
  92. begin
  93.    MyFree;
  94.  
  95.    inherited Destroy;
  96. end;
  97.  
  98. procedure TMFtpFileInfoList.Assign;
  99. var i: Integer;
  100. begin
  101.    Clear;
  102.  
  103.    for i := 0 to Source.Count - 1 do
  104.       Add(Source[i].Filename,
  105.           Source[i].Attrib,
  106.           Source[i].DateTime,
  107.           Source[i].Size,
  108.           Source[i].SymbolLink,
  109.           Source[i].Owner,
  110.           Source[i].Group,
  111.           Source[i].Description);
  112. end;
  113.  
  114. procedure TMFtpFileInfoList.Add(S1, S2, S3, S4, S5, S6, S7, S8: String);
  115. begin
  116.    FData[ItemAttrib].Add(S2);
  117.    FData[ItemDateTime].Add(S3);
  118.    FData[ItemSize].Add(S4);
  119.    FData[ItemSymbolLink].Add(S5);
  120.    FData[ItemDescription].Add(S8);
  121.    FData[ItemFilename].Add(S1);
  122.    FData[ItemOwner].Add(S6);
  123.    FData[ItemGroup].Add(S7);
  124.    FData[ItemDateTimeValue].Add('');
  125.  
  126.    Inc(FCount);
  127. end;
  128.  
  129. procedure TMFtpFileInfoList.Add(const P: MFtpFileInfo);
  130. begin
  131.    FData[ItemAttrib].Add(P.Attrib);
  132.    FData[ItemDateTime].Add(P.DateTime);
  133.    FData[ItemSize].Add(P.Size);
  134.    FData[ItemSymbolLink].Add(P.SymbolLink);
  135.    FData[ItemDescription].Add(P.Description);
  136.    FData[ItemFilename].Add(P.Filename);
  137.    FData[ItemOwner].Add(P.Owner);
  138.    FData[ItemGroup].Add(P.Group);
  139.    FData[ItemDateTimeValue].Add('');
  140.  
  141.    Inc(FCount);
  142. end;
  143.  
  144. procedure TMFtpFileInfoList.Clear;
  145. var i: Integer;
  146. begin
  147.    FCount := 0;
  148.  
  149.    for i := -1 to MaxItem do FData[i].Clear;
  150. end;
  151.  
  152. procedure TMFtpFileInfoList.ClearPart;
  153. var i: Integer;
  154. begin
  155.    for i := 0 to FCount - 1 do
  156.       FData[ItemType].Strings[i] := '';
  157. end;
  158.  
  159. procedure TMFtpFileInfoList.Delete;
  160. var j: Integer;
  161. begin
  162.    for j := -1 to MaxItem do FData[j].Delete(i);
  163. end;
  164.  
  165. procedure TMFtpFileInfoList.MyFree;
  166. var i: Integer;
  167. begin
  168.    for i := -1 to MaxItem do
  169.       FreeAndNil(FData[i]);
  170. end;
  171.  
  172. procedure TMFtpFileInfoList.Swap;
  173. var i: Integer;
  174. begin
  175.    for i := -1 to MaxItem do FData[i].Exchange(I1, I2);
  176. end;
  177.  
  178. function TMFtpFileInfoList.IndexOf;
  179. begin
  180.    Result := FData[ItemFilename].IndexOf(S);
  181. end;
  182.  
  183. {
  184.    function TMFtpFileInfoList.GetData;
  185.    begin
  186.    end;
  187. }
  188.  
  189. procedure TMFtpFileInfoList.PutData;
  190. begin
  191.    FData[ItemType].Strings[i] := Value;
  192. end;
  193.  
  194. function TMFtpFileInfoList.Get;
  195. begin
  196.    Result.Attrib := FData[ItemAttrib].Strings[I];
  197.    Result.DateTime := FData[ItemDateTime].Strings[I];
  198.    Result.Size := FData[ItemSize].Strings[I];
  199.    Result.SymbolLink := FData[ItemSymbolLink].Strings[I];
  200.    Result.Description := FData[ItemDescription].Strings[I];
  201.    Result.Filename := FData[ItemFilename].Strings[I];
  202.    Result.Owner := FData[ItemOwner].Strings[I];
  203.    Result.Group := FData[ItemGroup].Strings[I];
  204. end;
  205.  
  206. procedure TMFtpFileInfoList.Put;
  207. begin
  208.    FData[ItemAttrib].Strings[I] := P.Attrib;
  209.    FData[ItemDateTime].Strings[I] := P.DateTime;
  210.    FData[ItemSize].Strings[I] := P.Size;
  211.    FData[ItemSymbolLink].Strings[I] := P.SymbolLink;
  212.    FData[ItemDescription].Strings[I] := P.Description;
  213.    FData[ItemFilename].Strings[I] := P.Filename;
  214.    FData[ItemOwner].Strings[I] := P.Owner;
  215.    FData[ItemGroup].Strings[I] := P.Group;
  216. end;
  217.  
  218. function TMFtpFileInfoList.gsFilename;
  219. begin
  220.    Result := FData[ItemFilename].Strings[I];
  221. end;
  222.  
  223. function TMFtpFileInfoList.gsAttrib;
  224. begin
  225.    Result := FData[ItemAttrib].Strings[I];
  226. end;
  227.  
  228. function TMFtpFileInfoList.gsDateTimeValue;
  229. begin
  230.    Result := FData[ItemDateTimeValue].Strings[I];
  231. end;
  232.  
  233. function TMFtpFileInfoList.gsDescription;
  234. begin
  235.    Result := FData[ItemDescription].Strings[I];
  236. end;
  237.  
  238. function TMFtpFileInfoList.gsFileType;
  239. var
  240.    ShInfo: TSHFileInfo;
  241. begin
  242.    SHGetFileInfo(PChar(FData[ItemFilename].Strings[I]), 0, ShInfo, SizeOf(TSHFileInfo),
  243.                  SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES);
  244.    Result := ShInfo.szTypeName;
  245. end;
  246.  
  247. function TMFtpFileInfoList.gsSize;
  248. begin
  249.    Result := FData[ItemSize].Strings[I];
  250. end;
  251.  
  252. function TMFtpFileInfoList.gsOwner;
  253. begin
  254.    Result := FData[ItemOwner].Strings[I];
  255. end;
  256.  
  257. function TMFtpFileInfoList.gsGroup;
  258. begin
  259.    Result := FData[ItemGroup].Strings[I];
  260. end;
  261.  
  262. procedure TMFtpFileInfoList.Sort;
  263. var Temp: Word;
  264.     GetString: TGetString;
  265.    function BuildDateValue(S: String): String;
  266.    var Year, Month, Day: String;
  267.        CP: Word;
  268.    begin
  269.       Month := Copy(S, 1, 2);
  270.  
  271.       if Month[2] = '/' then
  272.       begin
  273.          Month[2] := Month[1];
  274.          Month[1] := '0';
  275.          CP := 3;
  276.       end
  277.       else
  278.       begin
  279.          CP := 4;
  280.       end;
  281.  
  282.       Day := Copy(S, CP, 2);
  283.       if Day[2] = '/' then
  284.       begin
  285.          Day[2] := Day[1];
  286.          Day[1] := '0';
  287.          Inc(CP, 2);
  288.       end
  289.       else
  290.       begin
  291.          Inc(CP, 3);
  292.       end;
  293.  
  294.       {$ifndef Y2K_DATE}
  295.       Year := Copy(S, CP, 2);
  296.       Inc(CP, 3);
  297.       if Year >= '70' then
  298.          Result := '19' + Year + Month + Day
  299.       else
  300.          Result := '20' + Year + Month + Day;
  301.       {$else}
  302.       Year := Copy(S, CP, 4);
  303.       Inc(CP, 5);
  304.       Result := Year + Month + Day;
  305.       {$endif}
  306.  
  307.       {$ifndef Y2K_DATE}
  308.       Result := Result + Copy(S, CP, 2);
  309.       if Result[10] = ':' then
  310.       begin
  311.          Result[10] := Result[9];
  312.          Result[9] := '0';
  313.       end;
  314.       {$else}
  315.       Result := Result + Copy(S, CP, 2);
  316.       if Result[12] = ':' then
  317.       begin
  318.          Result[12] := Result[11];
  319.          Result[11] := '0';
  320.       end;
  321.       {$endif}
  322.  
  323.       Result := Result + Copy(S, Length(S) - 4, 5);
  324.    end;
  325.    function Compare(S, S1: String): Integer;
  326.    var I1, I2: Integer;
  327.    begin
  328.       if S = S1 then
  329.       begin
  330.          Result := 0;
  331.          Exit;
  332.       end;
  333.       case ItemType of
  334.          ItemSize:
  335.          begin
  336.             I1 := StrToInt(S);
  337.             I2 := StrToInt(S1);
  338.             if I1 < I2 then
  339.                Result := -1
  340.             else
  341.                Result := 1;
  342.          end;
  343.          else
  344.          begin
  345.             {$ifdef OPTIMIZATION}
  346.             Result := optimizedAnsiCompareText(S, S1);
  347.             {$else}
  348.             Result := AnsiCompareText(S, S1);
  349.             {$endif}
  350.          end;
  351.       end;
  352.       if not Ascending then Result := -Result;
  353.    end;
  354.    procedure QuickSort(L, R: Integer);
  355.    var I, J: Integer;
  356.        P: string;
  357.    begin
  358.       repeat
  359.          I := L;
  360.          J := R;
  361.          P := GetString((L + R) shr 1);
  362.          repeat
  363.             while Compare(GetString(I), P) < 0 do Inc(I);
  364.             while Compare(GetString(J), P) > 0 do Dec(J);
  365.             if I <= J then
  366.             begin
  367.                Swap(I, J);
  368.                Inc(I);
  369.                Dec(J);
  370.             end;
  371.          until I > J;
  372.          if L < J then QuickSort(L, J);
  373.          L := I;
  374.       until I >= R;
  375.    end;
  376. begin
  377.    if FCount > 1 then
  378.    begin
  379.       case ItemType of
  380.          ItemFilename:
  381.          begin
  382.             GetString := gsFilename;
  383.          end;
  384.  
  385.          ItemAttrib:
  386.          begin
  387.             GetString := gsAttrib;
  388.          end;
  389.  
  390.          ItemDateTime:
  391.          begin
  392.             for Temp := 0 to FCount - 1 do
  393.                FData[ItemDateTimeValue].Strings[Temp] := BuildDateValue(FData[ItemDateTime].Strings[Temp]);
  394.             GetString := gsDateTimeValue;
  395.          end;
  396.  
  397.          ItemDescription:
  398.          begin
  399.             GetString := gsDescription;
  400.          end;
  401.  
  402.          ItemFileType:
  403.          begin
  404.             GetString := gsFileType;
  405.          end;
  406.  
  407.          ItemSize:
  408.          begin
  409.             GetString := gsSize;
  410.          end;
  411.  
  412.          ItemOwner:
  413.          begin
  414.             GetString := gsOwner;
  415.          end;
  416.  
  417.          ItemGroup:
  418.          begin
  419.             GetString := gsGroup;
  420.          end;
  421.  
  422.          else
  423.          begin
  424.             Exit;
  425.          end;
  426.       end;
  427.       QuickSort(0, FCount - 1);
  428.    end;
  429. end;
  430.  
  431. end.
  432.  
  433.