-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.pas
More file actions
255 lines (232 loc) · 6.66 KB
/
FileUtils.pas
File metadata and controls
255 lines (232 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{
Project: FindDupFar http://code.google.com/p/findupfar/
Author: Alexey Suhinin http://x-alexey.narod.ru
License: GPL v.2
}
unit FileUtils;
{$I PRJDefines.inc}
interface
uses
Classes,
FileSystemObjects,
// Windows,
SysUtils;
type
TFileGroupListFlags = set of (flShowUnGroupped);
TFileGroupList = class(TList)
private
FFlags: TFileGroupListFlags;
FUnGrpFileList: TFileSystemObjectList;
// FTotalFiles: Integer;
FGroupedFilesCount: Integer;
// FGroupsCount: Integer;
protected
property Count;
function GetItems(Index: Integer): TFileSystemObjectList;
// procedure SetItems(Index: Integer; Item: TFileSystemObjectList);
function GetGroupsCount: Integer;
function GetGroupedFilesCount: Integer;
public
{
property GroupsCount: Integer read FGroupsCount;
property GroupedFilesCount: Integer read FGroupedFilesCount;
}
property GroupsCount: Integer read GetGroupsCount;
property GroupedFilesCount: Integer read GetGroupedFilesCount;
property Items[Index: Integer]: TFileSystemObjectList read GetItems{ write SetItems}; default;
constructor Create;
destructor Destroy; override;
function Add(aFileObj: TFileSystemObject): Integer;
procedure Clear; override;
procedure DeleteFileByIndex(I, J: Integer);
procedure Delete(Index: Integer);
{$IFDEF VALIDATE}
procedure Validate;
{$ENDIF}
procedure Pack;
procedure ChangeMode;
{$IFDEF SORT_PANEL}
procedure Sort;
{$ENDIF}
end;
implementation
{ðåàëèçàöèÿ TFileGroupList}
function TFileGroupList.GetItems(Index: Integer): TFileSystemObjectList;
begin
if flShowUnGroupped in FFlags then
Result:=FUnGrpFileList
else
Result:=TFileSystemObjectList(inherited Items[Index]);
end;
{
procedure TFileGroupList.SetItems(Index: Integer; Item: TFileSystemObjectList);
begin
inherited Items[Index]:=Item;
end;
}
function TFileGroupList.GetGroupsCount: Integer;
begin
if flShowUnGroupped in FFlags then
Result:=1
else
Result:=Count;
end;
function TFileGroupList.GetGroupedFilesCount: Integer;
begin
if flShowUnGroupped in FFlags then
Result:=FUnGrpFileList.Count
else
Result:=FGroupedFilesCount;
end;
constructor TFileGroupList.Create;
begin
inherited Create;
FUnGrpFileList:=TFileSystemObjectList.Create;
FGroupedFilesCount:=0;
FFlags:=[];
end;
destructor TFileGroupList.Destroy;
begin
FreeAndNil(FUnGrpFileList);
inherited Destroy;
end;
function TFileGroupList.Add;
var
tmpFileGroup: TFileSystemObjectList;
I: Integer;
begin
{åñëè êîëè÷åñòâî ãðóïï îáúåêòîâ áîëüøå íóëÿ, òî ïûòàåìñÿ íàéòè ãðóïïó ñîäåðæàùóþ
äóáëèêàòû òåêóùåãî îáúåêòà.}
I:=0;
if Count>0 then
begin
while (I<Count) and
(not TFileSystemObjectList(Items[I]).IsDuplicate(aFileObj)) do Inc(I);
end;
{åñëè êîëè÷åñòâî ãðóïï îáúåêòîâ ðàâíî íóëþ èëè èíäåêñ íàéäåííîé ãðóïïû áîëüøå
îáùåãî êîëè÷åñòâà ãðóïï, òî òåêóùèé îáúåêò íå èìååò äóáëèêàòîâ ñðåäè ñïèñêà
äóáëèêàòîâ, èùåì åãî äóáëèêàòû â ñïèñêå îáúåêòîâ íå èìåþùèõ äóáëèêàòîâ}
if (Count=0) or (I=Count) then
begin
I:=0;
if FUnGrpFileList.Count>0 then
begin
while (I<FUnGrpFileList.Count) and
(FUnGrpFileList.Items[I].Compare(aFileObj, [cfHash])<>0) do Inc(I);
end;
if (FUnGrpFileList.Count=0) or (I=FUnGrpFileList.Count) then
{åñëè ÷èñëî îáúåêòîâ â ñïèñêå ðàâíî íóëþ èëè èíäåêñ íàéäåííîãî îáúåêòà áîëüøå
îáùåãî ÷èñëà îáúåêòîâ, òî òåêóùèé îáúåêò íå èìååò äóáëèêàòîâ. Äîáàâëÿåì åãî â
ñïèñîê íå äóáëèêàòîâ}
Result:=FUnGrpFileList.Add(aFileObj)
else
{äóáëèêàò òåêóùåãî îáúåêòà íàéäåí â ñïèñêå îáúåêòîâ íå èìåþùèõ äóáëèêàòîâ}
begin
{ñîçäàåì íîâóþ ãðóïïó â ñïèñêå äóáëèêàòîâ}
tmpFileGroup:=TFileSystemObjectList.Create;
inherited Add(pointer(tmpFileGroup));
{çàíîñèì â ñîçäàííóþ ãðóïïó íàéäåííûé è òåêóùèé îáúåêò}
tmpFileGroup.Add(FUnGrpFileList.Items[I]);
Result:=tmpFileGroup.Add(aFileObj);
{óäàëÿåì íàéäåííûé îáúåêò èç ñïèñêà íå äóáëèêàòîâ}
FUnGrpFileList.Delete(I);
Inc(FGroupedFilesCount, 2);
end;
end
else
begin
Result:=TFileSystemObjectList(Items[I]).Add(aFileObj);
Inc(FGroupedFilesCount);
end;
end;
procedure TFileGroupList.Clear;
var
i: Integer;
begin
if Assigned(FUnGrpFileList) then FUnGrpFileList.Clear;
for i:=0 to Count-1 do
TFileSystemObjectList(inherited Items[i]).Free;
// FTotalFiles:=0;
FGroupedFilesCount:=0;
// FGroupsCount:=0;
inherited Clear;
end;
procedure TFileGroupList.DeleteFileByIndex(I, J: Integer);
begin
with Items[I] do
begin
Delete(J);
if not (flShowUnGroupped in FFlags) then // !!!ïîõîæå ýòî ëèøíåå, èáî , åñëè ïîêàçûâàåì áåç ãðóïï, òî ñþäà ìû è íå ïîïàä¸ì.
Dec(FGroupedFilesCount);
end;
end;
procedure TFileGroupList.Delete;
begin
if not (flShowUnGroupped in FFlags) then
begin
Dec(FGroupedFilesCount, Items[Index].Count);
TFileSystemObjectList(Items[Index]).Free;
inherited Delete(Index);
end;
end;
{$IFDEF VALIDATE}
procedure TFileGroupList.Validate;
var
I, J: LongInt;
begin
for I:=0 to GroupsCount-1 do
if Items[I].Count>1 then
for J:=0 to Items[I].Count-1 do
Items[I].Items[J].IsValid;
Pack;
end;
{$ENDIF}
procedure TFileGroupList.Pack;
var
I, J, StepI: LongInt;
begin
I:=0;
while I<GroupsCount do
if (not (flShowUnGroupped in FFlags)) and
(
{$IFDEF AUTO_GROUP_REMOVE}
(Items[I].Count<2) or // ñêðûâàåì ãðóïïó, åñëè â íåé îñòàëîñü ìåíåå äâóõ ýëåìåíòîâ
{$ELSE}
(Items[I].Count<1) or // ñêðûâàåì ïóñòóþ ãðóïïó
{$ENDIF}
(gfDeleted in Items[I].Flags)) then
Delete(I)
else
begin
J:=0;
StepI:=1;
while J<Items[I].Count do
if flDeleted in Items[I].Items[J].Flags then
begin
StepI:=0;
DeleteFileByIndex(I, J);
end
else
Inc(J);
Inc(I, StepI);
end;
// Inherited Pack;
end;
procedure TFileGroupList.ChangeMode;
begin
if flShowUnGroupped in FFlags then
Exclude(FFlags, flShowUnGroupped)
else
Include(FFlags, flShowUnGroupped);
end;
{$IFDEF SORT_PANEL}
function Compare(Item1, Item2: Pointer): Integer;
begin
Result:=TFileSystemObject(TFileSystemObjectList(Item1).Items[0]).Compare(TFileSystemObject(TFileSystemObjectList(Item2).Items[0]), []);
end;
procedure TFileGroupList.Sort;
begin
inherited Sort(Compare);
end;
{$ENDIF}
end.