forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbgramultifiletype.pas
488 lines (421 loc) · 14.4 KB
/
bgramultifiletype.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program 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. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAMultiFileType;
{$i bgrabitmap.inc}{$H+}
{$IFDEF FPC}
{$MODESWITCH ADVANCEDRECORDS}
{$ENDIF}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} {$IFDEF OBJ}fgl{$ELSE}generics.collections{$ENDIF};
type
{ TEntryFilename }
TEntryFilename = record
private
FExtension: utf8string;
FName: utf8string;
function GetFilename: utf8string;
function GetIsEmpty: boolean;
procedure SetExtension(AValue: utf8string);
procedure SetFilename(AValue: utf8string);
procedure SetName(AValue: utf8string);
public
class operator {$IFDEF OBJ} = {$ELSE} Equal {$ENDIF}(const AValue1,AValue2: TEntryFilename): boolean;
property Filename: utf8string read GetFilename write SetFilename;
property Name: utf8string read FName write SetName;
property Extension: utf8string read FExtension write SetExtension;
property IsEmpty: boolean read GetIsEmpty;
end;
function EntryFilename(AName,AExtension: string): TEntryFilename; overload;
function EntryFilename(AFilename: string): TEntryFilename; overload;
type
TMultiFileContainer = class;
{ TMultiFileEntry }
TMultiFileEntry = class
protected
FContainer: TMultiFileContainer;
function GetName: utf8string; virtual; abstract;
procedure SetName(AValue: utf8string); virtual; abstract;
function GetFileSize: BGRAInt64; virtual;
function GetExtension: utf8string; virtual;
public
constructor Create(AContainer: TMultiFileContainer);
function CopyTo({%H-}ADestination: TStream): BGRAInt64; virtual;
property Name: utf8string read GetName write SetName;
property Extension: utf8string read GetExtension;
property FileSize: BGRAInt64 read GetFileSize;
property Container: TMultiFileContainer read FContainer;
end;
TMultiFileEntryList = {$IFDEF OBJ}specialize TFPGList{$ELSE}TList{$ENDIF}<TMultiFileEntry>;
{ TMultiFileContainer }
TMultiFileContainer = class
private
FEntries: TMultiFileEntryList;
protected
procedure Init; virtual;
function AddEntry(AEntry: TMultiFileEntry; AIndex: integer = -1): integer;
function GetCount: integer;
function GetEntry(AIndex: integer): TMultiFileEntry;
function CreateEntry(AName: utf8string; AExtension: utf8string; AContent: TStream): TMultiFileEntry; virtual; abstract;
function GetRawString(AIndex: integer): RawByteString;
function GetRawStringByFilename(AFilename: string): RawByteString;
procedure SetRawString(AIndex: integer; AValue: RawByteString);
procedure SetRawStringByFilename(AFilename: string; AValue: RawByteString);
public
constructor Create; overload;
constructor Create(AFilename: utf8string); overload;
constructor Create(AStream: TStream); overload;
constructor Create(AStream: TStream; AStartPos: BGRAInt64); overload;
function Add(AName: utf8string; AExtension: utf8string; AContent: TStream; AOverwrite: boolean = false; AOwnStream: boolean = true): integer; overload;
function Add(AName: utf8string; AExtension: utf8string; AContent: RawByteString; AOverwrite: boolean = false): integer; overload;
function Add(AFilename: TEntryFilename; AContent: TStream; AOverwrite: boolean = false; AOwnStream: boolean = true): integer; overload;
function Add(AFilename: TEntryFilename; AContent: RawByteString; AOverwrite: boolean = false): integer; overload;
procedure Clear; virtual;
destructor Destroy; override;
procedure LoadFromFile(AFilename: utf8string);
procedure LoadFromStream(AStream: TStream); virtual; abstract;
procedure SaveToFile(AFilename: utf8string);
procedure SaveToStream(ADestination: TStream); virtual; abstract;
procedure Remove(AEntry: TMultiFileEntry); virtual;
procedure Delete(AIndex: integer); overload; virtual;
function Delete(AName: utf8string; AExtension: utf8string; ACaseSensitive: boolean = True): boolean; overload;
function Delete(AFilename: TEntryFilename; ACaseSensitive: boolean = True): boolean; overload;
function IndexOf(AEntry: TMultiFileEntry): integer; overload;
function IndexOf(AName: utf8string; AExtenstion: utf8string; ACaseSensitive: boolean = True): integer; overload; virtual;
function IndexOf(AFilename: TEntryFilename; ACaseSensitive: boolean = True): integer; overload;
property Count: integer read GetCount;
property Entry[AIndex: integer]: TMultiFileEntry read GetEntry;
property RawString[AIndex: integer]: RawByteString read GetRawString write SetRawString;
property RawStringByFilename[AFilename: string]: RawByteString read GetRawStringByFilename write SetRawStringByFilename;
end;
implementation
uses BGRAUTF8, strutils;
{ TEntryFilename }
function TEntryFilename.GetFilename: utf8string;
begin
if Extension = '' then
result := Name
else
result := Name+'.'+Extension;
end;
function TEntryFilename.GetIsEmpty: boolean;
begin
result := (FName='') and (FExtension = '');
end;
procedure TEntryFilename.SetExtension(AValue: utf8string);
var
i: Integer;
begin
if FExtension=AValue then Exit;
for i := 1 to length(AValue) do
if AValue[i] in ['.','/'] then
raise Exception.Create('Invalid extension');
FExtension:=AValue;
end;
procedure TEntryFilename.SetFilename(AValue: utf8string);
var
idxDot: SizeInt;
begin //#
idxDot := {$IFDEF FPC}RPos{$ELSE}Pos{$ENDIF}('.',AValue);
if idxDot = 0 then
begin
Name := AValue;
Extension := '';
end
else
begin
Name := copy(AValue,1,idxDot-1);
Extension := copy(AValue,idxDot+1,length(AValue)-idxDot);
end;
end;
procedure TEntryFilename.SetName(AValue: utf8string);
var
i: Integer;
begin
if FName=AValue then Exit;
for i := 1 to length(AValue) do
if AValue[i] = '/' then
raise Exception.Create('Invalid name');
FName:=AValue;
end;
function EntryFilename(AName, AExtension: string): TEntryFilename;
begin
result.Name := AName;
result.Extension:= AExtension;
end;
function EntryFilename(AFilename: string): TEntryFilename;
begin
result.Filename:= AFilename;
end;
class operator TEntryFilename.{$IFDEF OBJ} = {$ELSE} Equal {$ENDIF}(const AValue1, AValue2: TEntryFilename): boolean;
begin
result := (AValue1.Name = AValue2.Name) and (AValue1.Extension = AValue2.Extension);
end;
{ TMultiFileEntry }
function TMultiFileEntry.GetFileSize: BGRAInt64;
begin
result := 0;
end;
function TMultiFileEntry.GetExtension: utf8string;
begin
result := '';
end;
constructor TMultiFileEntry.Create(AContainer: TMultiFileContainer);
begin
FContainer := AContainer;
end;
function TMultiFileEntry.CopyTo(ADestination: TStream): BGRAInt64;
begin
result := 0;
end;
{ TMultiFileContainer }
function TMultiFileContainer.GetCount: integer;
begin
if Assigned(FEntries) then
result := FEntries.Count
else
result := 0;
end;
function TMultiFileContainer.GetEntry(AIndex: integer): TMultiFileEntry;
begin
result := FEntries[AIndex];
end;
function TMultiFileContainer.GetRawString(AIndex: integer): RawByteString;
var s: TStringStream;
begin
s := TStringStream.Create('');
try
Entry[AIndex].CopyTo(s);
result := s.DataString;
finally
s.Free;
end;
end;
function TMultiFileContainer.GetRawStringByFilename(AFilename: string
): RawByteString;
var
idx: Integer;
begin
idx := IndexOf(EntryFilename(AFilename));
if idx = -1 then
result := ''
else
result := GetRawString(idx);
end;
procedure TMultiFileContainer.SetRawString(AIndex: integer;
AValue: RawByteString);
begin
with Entry[AIndex] do
Add(Name, Extension, AValue, true);
end;
procedure TMultiFileContainer.SetRawStringByFilename(AFilename: string;
AValue: RawByteString);
var
f: TEntryFilename;
begin
f := EntryFilename(AFilename);
Add(f.Name,f.Extension,AValue,true);
end;
procedure TMultiFileContainer.Init;
begin
FEntries := TMultiFileEntryList.Create;
end;
function TMultiFileContainer.AddEntry(AEntry: TMultiFileEntry; AIndex: integer): integer;
begin
if not Assigned(FEntries) then
raise exception.Create('Entry list not created');
if (AIndex >= 0) and (AIndex < FEntries.Count) then
begin
FEntries.Insert(AIndex, AEntry);
result := AIndex;
end
else
result := FEntries.Add(AEntry);
end;
constructor TMultiFileContainer.Create;
begin
Init;
end;
constructor TMultiFileContainer.Create(AFilename: utf8string);
begin
Init;
LoadFromFile(AFilename);
end;
constructor TMultiFileContainer.Create(AStream: TStream);
begin
Init;
LoadFromStream(AStream);
end;
constructor TMultiFileContainer.Create(AStream: TStream; AStartPos: BGRAInt64);
begin
Init;
AStream.Position := AStartPos;
LoadFromStream(AStream);
end;
function TMultiFileContainer.Add(AName: utf8string; AExtension: utf8string;
AContent: TStream; AOverwrite: boolean; AOwnStream: boolean): integer;
var
index: Integer;
newEntry: TMultiFileEntry;
contentCopy: TMemoryStream;
begin
index := IndexOf(AName,AExtension);
if index <> -1 then
begin
if AOverwrite then
Delete(index)
else
raise Exception.Create('Duplicate entry');
end;
if not AOwnStream then
begin
AContent.Position:= 0;
contentCopy := TMemoryStream.Create;
contentCopy.CopyFrom(AContent, AContent.Size);
newEntry := CreateEntry(AName, AExtension, contentCopy);
end else
newEntry := CreateEntry(AName, AExtension, AContent);
if Assigned(newEntry) then
result := AddEntry(newEntry, index)
else
raise exception.Create('Unable to create entry');
end;
function TMultiFileContainer.Add(AName: utf8string; AExtension: utf8string;
AContent: RawByteString; AOverwrite: boolean): integer;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
stream.Write(AContent[1],length(AContent));
result := Add(AName,AExtension,stream,AOverwrite);
end;
function TMultiFileContainer.Add(AFilename: TEntryFilename; AContent: TStream;
AOverwrite: boolean; AOwnStream: boolean): integer;
begin
result := Add(AFilename.Name,AFilename.Extension, AContent, AOverwrite, AOwnStream);
end;
function TMultiFileContainer.Add(AFilename: TEntryFilename;
AContent: RawByteString; AOverwrite: boolean): integer;
begin
result := Add(AFilename.Name,AFilename.Extension, AContent, AOverwrite);
end;
destructor TMultiFileContainer.Destroy;
begin
Clear;
FreeAndNil(FEntries);
inherited Destroy;
end;
procedure TMultiFileContainer.LoadFromFile(AFilename: utf8string);
var stream: TFileStream;
begin
stream := TFileStream.Create(Utf8ToAnsi(AFilename), fmOpenRead);
LoadFromStream(stream);
stream.Free;
end;
procedure TMultiFileContainer.SaveToFile(AFilename: utf8string);
var stream: TFileStream;
begin
stream := TFileStream.Create(Utf8ToAnsi(AFilename), fmCreate);
SaveToStream(stream);
stream.Free;
end;
procedure TMultiFileContainer.Remove(AEntry: TMultiFileEntry);
var
index: Integer;
begin
index := IndexOf(AEntry);
if index = -1 then
raise exception.Create('Entry not found');
Delete(index);
end;
procedure TMultiFileContainer.Delete(AIndex: integer);
begin
if (AIndex >= 0) and (AIndex < Count) then
begin
Entry[AIndex].Free;
FEntries.Delete(AIndex);
end else
raise ERangeError.Create('Index out of bounds');
end;
function TMultiFileContainer.Delete(AName: utf8string; AExtension: utf8string;
ACaseSensitive: boolean): boolean;
var
index: Integer;
begin
index := IndexOf(AName, AExtension, ACaseSensitive);
if index = -1 then
result := false
else
begin
Delete(index);
result := true;
end;
end;
function TMultiFileContainer.Delete(AFilename: TEntryFilename;
ACaseSensitive: boolean): boolean;
begin
result := Delete(AFilename.Name,AFilename.Extension,ACaseSensitive);
end;
function TMultiFileContainer.IndexOf(AEntry: TMultiFileEntry): integer;
begin
result := FEntries.IndexOf(AEntry);
end;
function TMultiFileContainer.IndexOf(AName: utf8string; AExtenstion: utf8string; ACaseSensitive: boolean): integer;
var
i: Integer;
begin
if ACaseSensitive then
begin
for i := 0 to Count-1 do
if (Entry[i].Name = AName) and (UTF8CompareText(Entry[i].Extension,AExtenstion) = 0) then
begin
result := i;
exit;
end;
end else
for i := 0 to Count-1 do
if (UTF8CompareText(Entry[i].Name,AName) = 0) and (UTF8CompareText(Entry[i].Extension,AExtenstion) = 0) then
begin
result := i;
exit;
end;
result := -1;
end;
function TMultiFileContainer.IndexOf(AFilename: TEntryFilename;
ACaseSensitive: boolean): integer;
begin
result := IndexOf(AFilename.Name,AFilename.Extension,ACaseSensitive);
end;
procedure TMultiFileContainer.Clear;
var
i: Integer;
begin
for i := 0 to FEntries.Count-1 do
FEntries.Items[i].Free;
FEntries.Clear;
end;
end.