-
Notifications
You must be signed in to change notification settings - Fork 13
/
SCFileList.pas
419 lines (339 loc) · 11.5 KB
/
SCFileList.pas
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{
This file is part of SuperCopier.
SuperCopier is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SuperCopier is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
unit SCFileList;
{$MODE Delphi}
interface
uses SCCommon,SCDirList,Classes,SCObjectThreadList, FileUtil, Windows;
const
FILELIST_DATA_VERSION=001;
type
TFileList=class;
TFileItem=class
private
Owner:TFileList;
public
BaseListId:Integer;
CopyTryCount:Integer;
SrcName,
DestName:WideString;
SrcSize:Int64;
Directory:TDirItem;
procedure SaveToStream(TheStream:TStream);
procedure LoadFromStream(TheStream:TStream;Version:integer;BaseDirListIndex:Integer);
function SrcFullName:WideString;
function DestFullName:WideString;
function DestSize:Int64;
function SrcAge:Integer;
function DestAge:Integer;
function SrcExists:Boolean;
function DestExists:Boolean;
function DestIsSameFile:Boolean;
function SrcDelete:Boolean;
function DestDelete:Boolean;
function DestCopyAttributes:boolean;
function DestCopySecurity:boolean;
function DestClearAttributes:boolean;
end;
TFileList = class (TObjectThreadList)
private
FDirList:TDirList;
FSortMode:TFileListSortMode;
FSortReverse:Boolean;
protected
function Get(Index: Integer): TFileItem;
procedure Put(Index: Integer; Item: TFileItem);
public
TotalCount:cardinal;
TotalSize,Size:Int64;
constructor Create(PDirList:TDirList);
destructor Destroy; override;
procedure SaveToStream(TheStream:TStream);
procedure LoadFromStream(TheStream:TStream);
function Add(Item: TFileItem): Integer;
procedure Delete(Index: Integer;UpdateTotalCount:Boolean=False);
procedure Sort;
property Items[Index: Integer]: TFileItem read Get write Put; default;
property SortMode:TFileListSortMode read FSortMode write FSortMode;
property SortReverse:Boolean read FSortReverse write FSortReverse;
end;
implementation
uses SCWin32,SysUtils,LCLIntf, LCLType, LMessages, Contnrs, Math;
function FLSortCompare(Item1,Item2:Pointer):Integer;forward;
//******************************************************************************
//******************************************************************************
//******************************************************************************
// TFileItem: fichier а copier
//******************************************************************************
//******************************************************************************
//******************************************************************************
procedure TFileItem.SaveToStream(TheStream:TStream);
var Len,Idx:Integer;
begin
// version 1
// On ne peut pas sauvegarder un pointeur, on sauvegarde l'index a la place
// L'index commence de la fin de la liste pour pouvoir retrouver le DirItem
// au chargement alors que la DirList est dйjа chargйe
Idx:=Owner.FDirList.Count-1-Owner.FDirList.IndexOf(Directory);
TheStream.Write(Idx,SizeOf(Integer));
Len:=Length(SrcName);
TheStream.Write(Len,SizeOf(Integer));
TheStream.Write(SrcName[1],Len*SizeOf(WideChar));
Len:=Length(DestName);
TheStream.Write(Len,SizeOf(Integer));
TheStream.Write(DestName[1],Len*SizeOf(WideChar));
TheStream.Write(SrcSize,SizeOf(Int64));
end;
procedure TFileItem.LoadFromStream(TheStream:TStream;Version:integer;BaseDirListIndex:Integer);
var Len,Idx:Integer;
begin
if Version>=001 then
begin
TheStream.Read(Idx,SizeOf(Integer));
// on rйcupиre le DirItem en retranchant а l'index du dernier item de DirList
// l'index sauvegardй
Directory:=Owner.FDirList[BaseDirListIndex-Idx];
TheStream.Read(Len,SizeOf(Integer));
SetLength(SrcName,Len);
TheStream.Read(SrcName[1],Len*SizeOf(WideChar));
TheStream.Read(Len,SizeOf(Integer));
SetLength(DestName,Len);
TheStream.Read(DestName[1],Len*SizeOf(WideChar));
TheStream.Read(SrcSize,SizeOf(Int64));
end;
end;
function TFileItem.SrcFullName:WideString;
begin
Result:=Concat(Directory.SrcPath,SrcName);
end;
function TFileItem.DestFullName:WideString;
begin
Result:=Concat(Directory.DestPath,DestName);
end;
function TFileItem.DestSize:Int64;
begin
Result:=SCCommon.GetFileSizeByName(DestFullName);
end;
function TFileItem.SrcAge:Integer;
begin
Result:=FileAge(SrcFullName);
end;
function TFileItem.DestAge:Integer;
begin
Result:=FileAge(DestFullName);
end;
function TFileItem.DestExists:Boolean;
begin
Result:=FileAge(DestFullName)<>-1;
end;
function TFileItem.SrcExists: Boolean;
begin
Result:=FileAge(SrcFullName)<>-1;
end;
function TFileItem.DestIsSameFile:Boolean;
begin
// Deux fichiers sont considйrйs identiques lorsque ils ont la mкme taille et la mкme date de derniиre modif
Result:=(SrcSize=DestSize) and (SrcAge=DestAge);
end;
function TFileItem.SrcDelete:Boolean;
var ErrCode:Integer;
//HACK: la gestion interne de l'unicode de delphi pourrit le code d'erreur win32
// lors du retour d'une fonction, ceci permets de le conserver
procedure SrcDelete_;
begin
Result:=SCWin32.DeleteFile(PWideChar(SrcFullName)); { *Converted from DeleteFile* }
ErrCode:=GetLastOSError;
end;
begin
SrcDelete_;
Windows.SetLastError(ErrCode);
end;
function TFileItem.DestDelete:Boolean;
var ErrCode:Integer;
//HACK: la gestion interne de l'unicode de delphi pourrit le code d'erreur win32
// lors du retour d'une fonction, ceci permets de le conserver
procedure DestDelete_;
begin
Result:=SCWin32.DeleteFile(PWideChar(DestFullName)); { *Converted from DeleteFile* }
ErrCode:=GetLastError;
end;
begin
DestDelete_;
SetLastError(ErrCode);
end;
function TFileItem.DestCopyAttributes:Boolean;
var Attr:Cardinal;
ErrCode:Integer;
//HACK: la gestion interne de l'unicode de delphi pourrit le code d'erreur win32
// lors du retour d'une fonction, ceci permets de le conserver
procedure DestCopyAttributes_;
begin
Attr:=SCWin32.GetFileAttributes(PWideChar(SrcFullName));
Result:=Attr<>$ffffffff;
if Result then Result:=SCWin32.SetFileAttributes(PWideChar(DestFullName),Attr);
ErrCode:=GetLastError;
end;
begin
DestCopyAttributes_;
SetLastError(ErrCode);
end;
function TFileItem.DestCopySecurity:boolean;
var ErrCode:Integer;
//HACK: la gestion interne de l'unicode de delphi pourrit le code d'erreur win32
// lors du retour d'une fonction, ceci permets de le conserver
procedure DestCopySecurity_;
begin
Result:=CopySecurity(SrcFullName,DestFullName);
ErrCode:=GetLastError;
end;
begin
DestCopySecurity_;
SetLastError(ErrCode);
end;
function TFileItem.DestClearAttributes:boolean;
var ErrCode:Integer;
//HACK: la gestion interne de l'unicode de delphi pourrit le code d'erreur win32
// lors du retour d'une fonction, ceci permets de le conserver
procedure DestClearAttributes_;
begin
Result:=SCWin32.SetFileAttributes(PWideChar(DestFullName),FILE_ATTRIBUTE_NORMAL);
ErrCode:=GetLastError;
end;
begin
DestClearAttributes_;
SetLastError(ErrCode);
end;
//******************************************************************************
//******************************************************************************
//******************************************************************************
// TFileList: liste de fichiers а copier
//******************************************************************************
//******************************************************************************
//******************************************************************************
procedure TFileList.SaveToStream(TheStream:TStream);
var i,Num,Version:integer;
begin
Version:=FILELIST_DATA_VERSION;
TheStream.Write(Version,SizeOf(Integer));
Num:=Count;
TheStream.Write(Num,SizeOf(Integer));
for i:=0 to Num-1 do
begin
Items[i].SaveToStream(TheStream);
end;
end;
procedure TFileList.LoadFromStream(TheStream:TStream);
var i,Num,Version:integer;
FileItem:TFileItem;
BaseDirListIndex:Integer;
begin
Version:=000;
Num:=0;
TheStream.Read(Version,SizeOf(Integer));
if Version>FILELIST_DATA_VERSION then raise Exception.Create('FileItems: data file is for a newer SuperCopier version');
TheStream.Read(num,SizeOf(Integer));
BaseDirListIndex:=FDirList.Count-1;
for i:=0 to Num-1 do
begin
FileItem:=TFileItem.Create;
FileItem.Owner:=Self;
FileItem.LoadFromStream(TheStream,Version,BaseDirListIndex);
Add(FileItem);
end;
end;
function TFileList.Add(Item: TFileItem): Integer;
begin
// maj des compteurs
Inc(Size,Item.SrcSize);
Inc(TotalSize,Item.SrcSize);
Inc(TotalCount,1);
Item.Owner:=Self;
Result:=inherited Add(Item);
end;
procedure TFileList.Delete(Index: Integer;UpdateTotalCount:Boolean=False);
begin
// maj des compteurs
if Items[Index]<>nil then
begin
Dec(Size,Items[Index].SrcSize);
if UpdateTotalCount then
begin
Dec(TotalSize,Items[Index].SrcSize);
Dec(TotalCount,1);
end;
end;
inherited Delete(Index);
end;
function TFileList.Get(Index: Integer): TFileItem;
begin
Result:=TFileItem(inherited Get(Index));
end;
procedure TFileList.Put(Index: Integer; Item: TFileItem);
begin
inherited Put(Index,Item);
end;
constructor TFileList.Create(PDirList:TDirList);
begin
inherited Create;
// init des variables
TotalCount:=0;
TotalSize:=0;
Size:=0;
FDirList:=PDirList;
FSortMode:=fsmNone;
FSortReverse:=False;
end;
destructor TFileList.Destroy;
begin
inherited Destroy;
end;
procedure TFileList.Sort;
begin
if Count>1 then inherited Sort(FLSortCompare);
end;
//******************************************************************************
// FLSortCompare: fonction de tri pour la filelist
//******************************************************************************
function FLSortCompare(Item1,Item2:Pointer):Integer;
var FileItem1,FileItem2:TFileItem;
begin
FileItem1:=TFileItem(Item1);
FileItem2:=TFileItem(Item2);
// le premier element de la liste dois rester le premier element de la liste
// ca il est en train d'кtre copiй
if FileItem1=FileItem1.Owner.First then
begin
Result:=-1;
Exit;
end;
if FileItem2=FileItem2.Owner.First then
begin
Result:=1;
Exit;
end;
Result:=0;
case FileItem1.Owner.SortMode of
fsmBySrcName:
Result:=WideCompareText(FileItem1.SrcName,FileItem2.SrcName);
fsmBySrcFullName:
Result:=WideCompareText(FileItem1.SrcFullName,FileItem2.SrcFullName);
fsmByDestName:
Result:=WideCompareText(FileItem1.DestName,FileItem2.DestName);
fsmByDestFullName:
Result:=WideCompareText(FileItem1.DestFullName,FileItem2.DestFullName);
fsmBySrcSize:
Result:=CompareValue(FileItem1.SrcSize,FileItem2.SrcSize);
fsmBySrcExt:
Result:=WideCompareText(ExtractFileExt(FileItem1.SrcName),ExtractFileExt(FileItem2.SrcName));
end;
if FileItem1.Owner.SortReverse then Result:=-Result;
end;
end.