Debian Codegraph @

plugins/wcx/sevenzip/src/jcl/DCJclAlternative.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 SevenZip archiver plugin
5
6 Copyright (C) 2015-2024 Alexander Koblov ([email protected])
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 }
22
23 unit DCJclAlternative;
24
25 {$mode objfpc}{$H+}
26
27 interface
28
29 uses
30 Classes, SysUtils, fgl, Windows, DCClassesUtf8;
31
32 // JclBase.pas -----------------------------------------------------------------
33 type
34 EJclError = class(Exception);
35 TDynByteArray = array of Byte;
36 TDynCardinalArray = array of Cardinal;
37
38 type
39 JclBase = class
40 type
41 PPInt64 = ^PInt64;
42 end;
43
44 // JclStreams.pas --------------------------------------------------------------
45 type
46 TJclStream = TStream;
47 TJclOnVolume = function(Index: Integer): TStream of object;
48 TJclOnVolumeMaxSize = function(Index: Integer): Int64 of object;
49
50 type
51
52 { TJclDynamicSplitStream }
53
54 TJclDynamicSplitStream = class(TJclStream)
55 private
56 FVolume: TStream;
57 FOnVolume: TJclOnVolume;
58 FOnVolumeMaxSize: TJclOnVolumeMaxSize;
59 private
60 function LoadVolume: Boolean;
61 function GetVolume(Index: Integer): TStream;
62 function GetVolumeMaxSize(Index: Integer): Int64;
63 protected
64 function GetSize: Int64; override;
65 procedure SetSize(const NewSize: Int64); override;
66 public
67 constructor Create(ADummy: Boolean = False);
68
69 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
70 function Read(var Buffer; Count: LongInt): LongInt; override;
71 function Write(const Buffer; Count: LongInt): LongInt; override;
72
73 property OnVolume: TJclOnVolume read FOnVolume write FOnVolume;
74 property OnVolumeMaxSize: TJclOnVolumeMaxSize read FOnVolumeMaxSize write FOnVolumeMaxSize;
75 end;
76
77 function StreamCopy(Source, Target: TStream): Int64;
78
79 // JclDateTime.pas -------------------------------------------------------------
80 function LocalDateTimeToFileTime(DateTime: TDateTime): TFileTime;
81
82 // JclFileUtils.pas ------------------------------------------------------------
83 const
84 DirDelimiter = DirectorySeparator;
85 DirSeparator = PathSeparator;
86
87 type
88 TJclOnAddDirectory = procedure(const Directory: String) of object;
89 TJclOnAddFile = procedure(const Directory: String; const FileInfo: TSearchRec) of object;
90
91 function PathAddSeparator(const Path: String): String; inline;
92 function PathRemoveSeparator(const Path: String): String; inline;
93 function PathGetRelativePath(const Base, Path: String): String; inline;
94
95 function PathCanonicalize(const Path: WideString): WideString;
96 function IsFileNameMatch(const FileName, Mask: WideString): Boolean; inline;
97
98 procedure BuildFileList(const SourceFile: String; FileAttr: Integer; InnerList: TStrings; Dummy: Boolean);
99 procedure EnumFiles(const Path: String; OnAddFile: TJclOnAddFile; ExcludeAttributes: Integer);
100 procedure EnumDirectories(const Path: String; OnAddDirectory: TJclOnAddDirectory;
101 DummyBoolean: Boolean; const DummyString: String; DummyPointer: Pointer);
102
103 function FileDelete(const FileName: String): Boolean; inline;
104 function FindUnusedFileName(const FileName, FileExt: String): String;
105 function FileMove(const OldName, NewName: String; Replace: Boolean = False): Boolean;
106
107 // JclSysUtils.pas -------------------------------------------------------------
108 type
109 TModuleHandle = TLibHandle;
110
111 const
112 INVALID_MODULEHANDLE_VALUE = NilHandle;
113
114 type
115 JclSysUtils = class
116 class function LoadModule(var Module: TModuleHandle; FileName: String): Boolean;
117 class procedure UnloadModule(var Module: TModuleHandle);
118 end;
119
120 function GUIDEquals(const GUID1, GUID2: TGUID): Boolean; inline;
121 function GetModuleSymbol(Module: TModuleHandle; SymbolName: String): Pointer; inline;
122
123 // JclStrings.pas --------------------------------------------------------------
124 procedure StrTokenToStrings(const Token: String; Separator: AnsiChar; var Strings: TStrings);
125
126 // JclWideStrings.pas ----------------------------------------------------------
127 type
128 TFPWideStrObjMap = specialize TFPGMap<WideString, TObject>;
129
130 type
131
132 { TJclWideStringList }
133
134 TJclWideStringList = class(TFPWideStrObjMap)
135 private
136 FCaseSensitive: Boolean;
137 private
138 procedure SetCaseSensitive(AValue: Boolean);
139 function CompareWideStringProc(Key1, Key2: Pointer): Integer;
140 function CompareTextWideStringProc(Key1, Key2: Pointer): Integer;
141 protected
142 function Get(Index: Integer): WideString;
143 function GetObject(Index: Integer): TObject;
144 procedure Put(Index: Integer; const S: WideString);
145 procedure PutObject(Index: Integer; AObject: TObject);
146 public
147 constructor Create;
148 function AddObject(const S: WideString; AObject: TObject): Integer;
149
150 property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
151 property Objects[Index: Integer]: TObject read GetObject write PutObject;
152 property Strings[Index: Integer]: WideString read Get write Put; default;
153 end;
154
155 // Classes.pas -----------------------------------------------------------------
156 type
157 TFileStream = TFileStreamEx;
158
159 // SysUtils.pas ----------------------------------------------------------------
160 function FileExists(const FileName: String): Boolean; inline;
161
162 // Windows.pas -----------------------------------------------------------------
163 function CreateFile(lpFileName: LPCSTR; dwDesiredAccess: DWORD; dwShareMode: DWORD; lpSecurityAttributes: LPSECURITY_ATTRIBUTES;
164 dwCreationDisposition: DWORD; dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; inline;
165 function GetFileAttributesEx(lpFileName: LPCSTR; fInfoLevelId: TGET_FILEEX_INFO_LEVELS; lpFileInformation: Pointer): BOOL; inline;
166
167 implementation
168
169 uses
170 LazFileUtils, DCOSUtils, DCWindows;
171
172 function StreamCopy(Source, Target: TStream): Int64;
173 begin
174 Result:= Target.CopyFrom(Source, Source.Size);
175 end;
176
177 function LocalDateTimeToFileTime(DateTime: TDateTime): TFileTime;
178 begin
179 Int64(Result) := Round((Extended(DateTime) + 109205.0) * 864000000000.0);
180 Windows.LocalFileTimeToFileTime(@Result, @Result);
181 end;
182
183 function PathAddSeparator(const Path: String): String;
184 begin
185 Result:= IncludeTrailingPathDelimiter(Path);
186 end;
187
188 function PathRemoveSeparator(const Path: String): String;
189 begin
190 Result:= ExcludeTrailingPathDelimiter(Path);
191 end;
192
193 function PathGetRelativePath(const Base, Path: String): String;
194 begin
195 Result:= ExtractRelativePath(Base, Path);
196 end;
197
198 function PathMatchSpecW(pszFile, pszSpec: LPCWSTR): BOOL; stdcall; external 'shlwapi.dll';
199 function PathCanonicalizeW(lpszDst, lpszSrc: LPCWSTR): BOOL; stdcall; external 'shlwapi.dll';
200
201 function PathCanonicalize(const Path: WideString): WideString;
202 begin
203 SetLength(Result, MAX_PATH);
204 if PathCanonicalizeW(PWideChar(Result), PWideChar(Path)) then
205 Result:= PWideChar(Result)
206 else begin
207 Result:= EmptyWideStr;
208 end;
209 end;
210
211 function IsFileNameMatch(const FileName, Mask: WideString): Boolean;
212 begin
213 Result:= PathMatchSpecW(PWideChar(FileName), PWideChar(Mask));
214 end;
215
216 procedure BuildFileList(const SourceFile: String; FileAttr: Integer;
217 InnerList: TStrings; Dummy: Boolean);
218 begin
219 raise Exception.Create('Not implemented');
220 end;
221
222 procedure EnumFiles(const Path: String; OnAddFile: TJclOnAddFile; ExcludeAttributes: Integer);
223 begin
224 raise Exception.Create('Not implemented');
225 end;
226
227 procedure EnumDirectories(const Path: String; OnAddDirectory: TJclOnAddDirectory;
228 DummyBoolean: Boolean; const DummyString: String; DummyPointer: Pointer);
229 begin
230 raise Exception.Create('Not implemented');
231 end;
232
233 function FileDelete(const FileName: String): Boolean;
234 begin
235 Result:= mbDeleteFile(FileName);
236 end;
237
238 function FindUnusedFileName(const FileName, FileExt: String): String;
239 var
240 Counter: Int64 = 0;
241 begin
242 Result:= FileName + ExtensionSeparator + FileExt;
243 if FileExists(Result) then
244 repeat
245 Inc(Counter);
246 Result:= FileName + IntToStr(Counter) + ExtensionSeparator + FileExt;
247 until not FileExists(Result);
248 end;
249
250 function FileMove(const OldName, NewName: String; Replace: Boolean): Boolean;
251 const
252 dwFlags: array[Boolean] of DWORD = (0, MOVEFILE_REPLACE_EXISTING);
253 begin
254 Result:= MoveFileExW(PWideChar(UTF16LongName(OldName)), PWideChar(UTF16LongName(NewName)),
255 dwFlags[Replace] or MOVEFILE_COPY_ALLOWED);
256 end;
257
258 function GUIDEquals(const GUID1, GUID2: TGUID): Boolean;
259 begin
260 Result:= IsEqualGUID(GUID1, GUID2);
261 end;
262
263 class function JclSysUtils.LoadModule(var Module: TModuleHandle; FileName: String): Boolean;
264 begin
265 Module:= mbLoadLibrary(FileName);
266 Result:= Module <> INVALID_MODULEHANDLE_VALUE;
267 end;
268
269 function GetModuleSymbol(Module: TModuleHandle; SymbolName: String): Pointer;
270 begin
271 Result:= GetProcAddress(Module, PAnsiChar(SymbolName));
272 end;
273
274 class procedure JclSysUtils.UnloadModule(var Module: TModuleHandle);
275 begin
276 if Module <> INVALID_MODULEHANDLE_VALUE then
277 begin
278 FreeLibrary(Module);
279 Module:= INVALID_MODULEHANDLE_VALUE;
280 end;
281 end;
282
283 procedure StrTokenToStrings(const Token: String; Separator: AnsiChar; var Strings: TStrings);
284 var
285 Start: Integer = 1;
286 Len, Finish: Integer;
287 begin
288 Len:= Length(Token);
289 Strings.BeginUpdate;
290 try
291 Strings.Clear;
292 for Finish:= 1 to Len - 1 do
293 begin
294 if Token[Finish] = Separator then
295 begin
296 Strings.Add(Copy(Token, Start, Finish - Start));
297 Start:= Finish + 1;
298 end;
299 end;
300 if Start <= Len then
301 begin
302 Strings.Add(Copy(Token, Start, Len - Start + 1));
303 end;
304 finally
305 Strings.EndUpdate;
306 end;
307 end;
308
309 function FileExists(const FileName: String): Boolean;
310 begin
311 Result:= mbFileExists(FileName);
312 end;
313
314 function CreateFile(lpFileName: LPCSTR; dwDesiredAccess: DWORD; dwShareMode: DWORD; lpSecurityAttributes: LPSECURITY_ATTRIBUTES;
315 dwCreationDisposition: DWORD; dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE;
316 begin
317 Result:= CreateFileW(PWideChar(UTF16LongName(lpFileName)), dwDesiredAccess, dwShareMode,
318 lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
319 end;
320
321 function GetFileAttributesEx(lpFileName: LPCSTR; fInfoLevelId: TGET_FILEEX_INFO_LEVELS; lpFileInformation: Pointer): BOOL;
322 begin
323 Result:= GetFileAttributesExW(PWideChar(UTF16LongName(lpFileName)), fInfoLevelId, lpFileInformation);
324 end;
325
326 { TJclDynamicSplitStream }
327
328 function TJclDynamicSplitStream.LoadVolume: Boolean;
329 begin
330 Result:= Assigned(FVolume);
331 if not Result then
332 begin
333 FVolume:= GetVolume(0);
334 GetVolumeMaxSize(0);
335 Result := Assigned(FVolume);
336 if Result then FVolume.Seek(0, soBeginning);
337 end;
338 end;
339
340 function TJclDynamicSplitStream.GetVolume(Index: Integer): TStream;
341 begin
342 if Assigned(FOnVolume) then
343 Result:= FOnVolume(Index)
344 else begin
345 Result:= nil;
346 end;
347 end;
348
349 function TJclDynamicSplitStream.GetVolumeMaxSize(Index: Integer): Int64;
350 begin
351 if Assigned(FOnVolumeMaxSize) then
352 Result:= FOnVolumeMaxSize(Index)
353 else begin
354 Result:= 0;
355 end;
356 end;
357
358 function TJclDynamicSplitStream.GetSize: Int64;
359 begin
360 if not LoadVolume then
361 Result:= 0
362 else begin
363 Result:= FVolume.Size;
364 end;
365 end;
366
367 procedure TJclDynamicSplitStream.SetSize(const NewSize: Int64);
368 begin
369 if LoadVolume then FVolume.Size:= NewSize;
370 end;
371
372 constructor TJclDynamicSplitStream.Create(ADummy: Boolean);
373 begin
374 inherited Create;
375 end;
376
377 function TJclDynamicSplitStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
378 begin
379 if not LoadVolume then
380 Result:= 0
381 else begin
382 Result:= FVolume.Seek(Offset, Origin);
383 end;
384 end;
385
386 function TJclDynamicSplitStream.Read(var Buffer; Count: LongInt): LongInt;
387 begin
388 if not LoadVolume then
389 Result:= 0
390 else begin
391 Result:= FVolume.Read(Buffer, Count);
392 end;
393 end;
394
395 function TJclDynamicSplitStream.Write(const Buffer; Count: LongInt): LongInt;
396 begin
397 if not LoadVolume then
398 Result:= 0
399 else begin
400 Result:= FVolume.Write(Buffer, Count);
401 end;
402 end;
403
404 { TJclWideStringList }
405
406 procedure TJclWideStringList.SetCaseSensitive(AValue: Boolean);
407 begin
408 if FCaseSensitive <> AValue then
409 begin
410 FCaseSensitive:= AValue;
411 if FCaseSensitive then
412 OnKeyPtrCompare := @CompareWideStringProc
413 else begin
414 OnKeyPtrCompare := @CompareTextWideStringProc;
415 end;
416 if Sorted then Sort;
417 end;
418 end;
419
420 function TJclWideStringList.Get(Index: Integer): WideString;
421 begin
422 Result := Keys[Index];
423 end;
424
425 function TJclWideStringList.GetObject(Index: Integer): TObject;
426 begin
427 Result := Data[Index];
428 end;
429
430 procedure TJclWideStringList.Put(Index: Integer; const S: WideString);
431 begin
432 Keys[Index] := S;
433 end;
434
435 procedure TJclWideStringList.PutObject(Index: Integer; AObject: TObject);
436 begin
437 Data[Index] := AObject;
438 end;
439
440 function TJclWideStringList.CompareWideStringProc(Key1, Key2: Pointer): Integer;
441 begin
442 {$if FPC_FULLVERSION<30002}
443 Result:= WideStringManager.CompareWideStringProc(WideString(Key1^), WideString(Key2^));
444 {$else}
445 Result:= WideStringManager.CompareWideStringProc(WideString(Key1^), WideString(Key2^), []);
446 {$endif}
447 end;
448
449 function TJclWideStringList.CompareTextWideStringProc(Key1, Key2: Pointer): Integer;
450 begin
451 {$if FPC_FULLVERSION<30002}
452 Result:= WideStringManager.CompareTextWideStringProc(WideString(Key1^), WideString(Key2^));
453 {$else}
454 Result:= WideStringManager.CompareWideStringProc(WideString(Key1^), WideString(Key2^), [coIgnoreCase]);
455 {$endif}
456 end;
457
458 constructor TJclWideStringList.Create;
459 begin
460 inherited Create;
461 OnKeyPtrCompare := @CompareTextWideStringProc;
462 end;
463
464 function TJclWideStringList.AddObject(const S: WideString; AObject: TObject): Integer;
465 begin
466 Result:= inherited Add(S, AObject);
467 end;
468
469 end.
470