-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDataFile.pas
690 lines (612 loc) · 19.2 KB
/
DataFile.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
{----------------------------------------------------------------}
{ }
{ Degisy TDataFile 1.22 }
{ Writen by Alexander Momot }
{ http://www.degisy.com }
{ E-mail: [email protected] }
{ (c)2001-2004 Degisy Software }
{ }
{----------------------------------------------------------------}
unit DataFile;
interface
uses
Windows,
SysUtils,
Classes,
Graphics;
const
MAX_NAMELEN = 36;
MAX_PATHLEN = 240;
SECTION_TEST = '$test';
type
DF_IDENTNAME = array[0..MAX_NAMELEN - 1]of Char;
pDataHeader = ^IDataHeader;
IDataHeader = packed record
Id : Integer;
Section : DF_IDENTNAME;
Ident : DF_IDENTNAME;
Size : Integer;
end;
{ TDataFile }
TDataFile = class(TObject)
private
FFile: TFileStream;
FFileName: string;
FCodeKey: string;
function GetSectionCount: Integer;
function FindIdent(Section, Ident: string; pHdr: pDataHeader): boolean;
public
constructor Create(const FileName: string);
destructor Destroy; override;
//----------------------------------------------------
procedure GetSectionNames(List: TStrings);
procedure GetValueNames(Section: string; List: TStrings);
//----------------------------------------------------
function SectionExists(Section: string): Boolean;
function ValueExists(Section, Ident: string): Boolean;
//----------------------------------------------------
function ReadData(Section, Ident: string; pBuf: Pointer): Integer;
function ReadStream(Section, Ident: string; Stream: TStream): Integer;
function ReadString(Section, Ident, Default: string): string;
function ReadInteger(Section, Ident: string; Default: Integer): Integer;
function ReadDouble(Section, Ident: string; Default: Double): Double;
function ReadExtended(Section, Ident: string; Default: Extended): Extended;
function ReadDateTime(Section, Ident: string; Default: TDateTime): TDateTime;
function ReadBoolean(Section, Ident: string; Default: Boolean): Boolean;
procedure ReadStrings(Section, Ident: string; List: TStrings);
procedure ReadFont(Section, Ident: string; Font: TFont);
//----------------------------------------------------
function WriteData(Section, Ident: string; pBuf: Pointer; Count: Integer): Integer;
function WriteStream(Section, Ident: string; Stream: TStream): Integer;
procedure WriteString(Section, Ident, Value: string);
procedure WriteInteger(Section, Ident: string; Value: Integer);
procedure WriteDouble(Section, Ident: string; Value: Double);
procedure WriteExtended(Section, Ident: string; Value: Extended);
procedure WriteDateTime(Section, Ident: string; Value: TDateTime);
procedure WriteBoolean(Section, Ident: string; Value: Boolean);
procedure WriteStrings(Section, Ident: string; List: TStrings);
procedure WriteFont(Section, Ident: string; Font: TFont);
//----------------------------------------------------
procedure Delete(Section, Ident: string);
procedure DeleteSection(Section: string);
//----------------------------------------------------
procedure EncryptBuf(pBuf: Pointer; BufLen: Integer); dynamic;
procedure DecryptBuf(pBuf: Pointer; BufLen: Integer); dynamic;
//----------------------------------------------------
property CodeKey: string read FCodeKey write FCodeKey;
property FileName: string read FFileName;
property SectionCount: Integer read GetSectionCount;
end;
implementation
const
DF_HEADER_IDENT = $112;
type
TDfFont = class(TFont);
pSaveFont = ^ISaveFont;
ISaveFont = packed record
CharSet : TFontCharSet;
Color : TColor;
Pitch : TFontPitch;
Size : Word;
Style : TFontStyles;
end;
{ TDataFile }
constructor TDataFile.Create(const FileName: string);
var
OpenMode: integer;
begin
FFileName := FileName;
if FileExists(FFileName)then
OpenMode := fmOpenReadWrite or fmShareDenyNone
else
OpenMode := fmCreate or fmShareDenyNone;
FFile := TFileStream.Create(FileName, OpenMode);
FCodeKey := 'hDmpSwrdGZxqlHdgfcIRuHsDHs5Tu';
end;
//------------------------------------------------------------------------------
destructor TDataFile.Destroy;
begin
if Assigned( FFile )then FFile.Free;
end;
//------------------------------------------------------------------------------
function TDataFile.FindIdent(Section, Ident: string; pHdr: pDataHeader): boolean;
var
Sect : string;
Iden : string;
Count : integer;
IsError : boolean;
begin
IsError := False;
Result := False;
FFile.Seek(0, soFromBeginning);
repeat
Count := FFile.Read(pHdr^, SizeOf(IDataHeader));
if( Count <> SizeOf(IDataHeader))then Break;
DecryptBuf(pHdr, SizeOf(IDataHeader));
if( pHdr^.ID <> DF_HEADER_IDENT )then
begin
IsError := True;
Break;
end;
Sect := pHdr^.Section;
Iden := pHdr^.Ident;
Result := ( ANSICompareText(Sect, Section) = 0 )and
(( ANSICompareText(Iden, Ident) = 0 )or
( Ident = SECTION_TEST ));
if( Result )then Break;
FFile.Seek(pHdr^.Size, soFromCurrent);
until( False );
if( IsError )then raise EInvalidOperation.Create('Îäèí èç êîìïîíåíòîâ ïîâðåæäåí! Ïåðåóñòàíîâèòå ïðèëîæåíèå.');
end;
//------------------------------------------------------------------------------
function TDataFile.GetSectionCount: Integer;
var
Hdr : IDataHeader;
Count : integer;
IsError: boolean;
begin
IsError := False;
Result := 0;
FFile.Seek(0, soFromBeginning);
repeat
Count := FFile.Read(Hdr, SizeOf(IDataHeader));
if( Count <> SizeOf(IDataHeader))then Break;
DecryptBuf(@Hdr, SizeOf(IDataHeader));
if( Hdr.ID <> DF_HEADER_IDENT )then
begin
IsError := True;
Break;
end else inc(Result);
FFile.Seek(Hdr.Size, soFromCurrent);
until( False );
if( IsError )then raise EInvalidOperation.Create('Îäèí èç êîìïîíåíòîâ ïîâðåæäåí! Ïåðåóñòàíîâèòå ïðèëîæåíèå.');
end;
//------------------------------------------------------------------------------
procedure TDataFile.GetSectionNames(List: TStrings);
var
Hdr : IDataHeader;
Count : integer;
IsError: boolean;
begin
IsError := False;
List.Clear;
FFile.Seek(0, soFromBeginning);
repeat
Count := FFile.Read(Hdr, SizeOf(IDataHeader));
if( Count <> SizeOf(IDataHeader))then Break;
DecryptBuf(@Hdr, SizeOf(IDataHeader));
if( Hdr.ID <> DF_HEADER_IDENT )then
begin
IsError := True;
Break;
end else
if( List.IndexOf(Hdr.Section) = -1 )then
List.Add(Hdr.Section);
FFile.Seek(Hdr.Size, soFromCurrent);
until( False );
if( IsError )then raise EInvalidOperation.Create('Îäèí èç êîìïîíåíòîâ ïîâðåæäåí! Ïåðåóñòàíîâèòå ïðèëîæåíèå.');
end;
//------------------------------------------------------------------------------
procedure TDataFile.GetValueNames(Section: string; List: TStrings);
var
Hdr : IDataHeader;
Count : integer;
IsError: boolean;
begin
IsError := False;
List.Clear;
FFile.Seek(0, soFromBeginning);
repeat
Count := FFile.Read(Hdr, SizeOf(IDataHeader));
if( Count <> SizeOf(IDataHeader))then Break;
DecryptBuf(@Hdr, SizeOf(IDataHeader));
if( Hdr.ID <> DF_HEADER_IDENT )then
begin
IsError := True;
Break;
end else
if ANSICompareText(Section, Hdr.Section) = 0 then
List.Add(Hdr.Ident);
FFile.Seek(Hdr.Size, soFromCurrent);
until( False );
if( IsError )then raise EInvalidOperation.Create('Îäèí èç êîìïîíåíòîâ ïîâðåæäåí! Ïåðåóñòàíîâèòå ïðèëîæåíèå.');
end;
{------------------------------------------------------------------------------}
{ find }
{------------------------------------------------------------------------------}
function TDataFile.SectionExists(Section: string): Boolean;
var
Hdr: IDataHeader;
begin
Result := FindIdent(Section, SECTION_TEST, @Hdr);
end;
//------------------------------------------------------------------------------
function TDataFile.ValueExists(Section, Ident: string): Boolean;
var
Hdr: IDataHeader;
begin
Result := FindIdent(Section, Ident, @Hdr);
end;
{------------------------------------------------------------------------------}
{ read }
{------------------------------------------------------------------------------}
function TDataFile.ReadData(Section, Ident: string; pBuf: Pointer): Integer;
var
Found : boolean;
Hdr : IDataHeader;
begin
Found := FindIdent(Section, Ident, @Hdr);
if( Found )then
begin
Result := FFile.Read(pBuf^, Hdr.Size);
DecryptBuf(pBuf, Hdr.Size);
end else
Result := -1;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadStream(Section, Ident: string; Stream: TStream): Integer;
var
Hdr : IDataHeader;
pBuf : Pointer;
begin
if( FindIdent(Section, Ident, @Hdr) )then
begin
Result := Hdr.Size;
GetMem(pBuf, Result);
try
FFile.Read(pBuf^, Result);
DecryptBuf(pBuf, Result);
Stream.Size := 0;
Stream.Write(pBuf^, Result);
Stream.Seek(0, soFromBeginning);
finally
FreeMem(pBuf, Result);
end;
end else
Result := -1;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadString(Section, Ident, Default: string): string;
var
Buf : TMemoryStream;
pBuf : PChar;
Count : Integer;
begin
Buf := TMemoryStream.Create;
try
Count := ReadStream(Section, Ident, Buf);
if( Count > -1 )then
begin
pBuf := StrAlloc(Count);
try
Buf.Seek(0, soFromBeginning);
Buf.Read(pBuf^, Count);
Result := StrPas(pBuf);
finally
StrDispose(pBuf);
end;
end else
Result := Default;
finally
Buf.Free;
end;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadInteger(Section, Ident: string; Default: Integer): Integer;
var
Buf : array[0..1023]of Char;
Count : Integer;
begin
Count := ReadData(Section, Ident, @Buf);
if( Count >= SizeOf(Integer) )then
Move(Buf, Result, SizeOf(Integer))
else
Result := Default;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadDouble(Section, Ident: string; Default: Double): Double;
var
Buf : array[0..1023]of Char;
Count : Integer;
begin
Count := ReadData(Section, Ident, @Buf);
if( Count >= SizeOf(Double) )then
Move(Buf, Result, SizeOf(Double))
else
Result := Default;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadExtended(Section, Ident: string; Default: Extended): Extended;
var
Buf : array[0..1023]of Char;
Count : Integer;
begin
Count := ReadData(Section, Ident, @Buf);
if( Count >= SizeOf(Extended) )then
Move(Buf, Result, SizeOf(Extended))
else
Result := Default;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadDateTime(Section, Ident: string; Default: TDateTime): TDateTime;
var
Buf : array[0..1023]of Char;
Count : Integer;
begin
Count := ReadData(Section, Ident, @Buf);
if( Count >= SizeOf(TDateTime) )then
Move(Buf, Result, SizeOf(TDateTime))
else
Result := Default;
end;
//------------------------------------------------------------------------------
function TDataFile.ReadBoolean(Section, Ident: string; Default: Boolean): Boolean;
var
Buf : array[0..1023]of Char;
Count : Integer;
begin
Count := ReadData(Section, Ident, @Buf);
if( Count >= SizeOf(Boolean) )then
Move(Buf, Result, SizeOf(Boolean))
else
Result := Default;
end;
//------------------------------------------------------------------------------
procedure TDataFile.ReadStrings(Section, Ident: string; List: TStrings);
var
Buf : TMemoryStream;
Count : Integer;
begin
List.Clear;
Buf := TMemoryStream.Create;
try
Count := ReadStream(Section, Ident, Buf);
if( Count > -1 )then
List.LoadFromStream( Buf );
finally
Buf.Free;
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.ReadFont(Section, Ident: string; Font: TFont);
var
Buf : TMemoryStream;
pPos : PChar;
pBuf : Pointer;
Count : Integer;
FontChange: TNotifyEvent;
begin
Buf := TMemoryStream.Create;
try
Count := ReadStream(Section, Ident, Buf);
if( Count > SizeOf(ISaveFont))then
begin
GetMem(pBuf, Count);
FontChange := Font.OnChange;
try
Buf.Seek(0, soFromBeginning);
Buf.Read(pBuf^, Count);
Font.OnChange := nil;
Font.Charset := pSaveFont(pBuf)^.CharSet;
Font.Color := pSaveFont(pBuf)^.Color;
Font.Pitch := pSaveFont(pBuf)^.Pitch;
Font.Size := pSaveFont(pBuf)^.Size;
Font.Style := pSaveFont(pBuf)^.Style;
pPos := pBuf;
inc(pPos, SizeOf(ISaveFont));
Font.Name := StrPas(pPos);
finally
Font.OnChange := FontChange;
TDfFont(Font).Changed;
FreeMem(pBuf, Count);
end;
end;
finally
Buf.Free;
end;
end;
{------------------------------------------------------------------------------}
{ write }
{------------------------------------------------------------------------------}
function TDataFile.WriteData(Section, Ident: string; pBuf: Pointer; Count: Integer): Integer;
var
Hdr : IDataHeader;
P : Pointer;
begin
Delete(Section, Ident);
FFile.Seek(0, soFromEnd);
{ feel header }
Hdr.Id := DF_HEADER_IDENT;
StrPCopy(Hdr.Section, Section);
StrPCopy(Hdr.Ident, Ident);
Hdr.Size := Count;
{ xor }
EncryptBuf(@Hdr, SizeOf(IDataHeader));
{ write header }
Result := FFile.Write(Hdr, SizeOf(IDataHeader));
if( Result > -1 )then
begin
GetMem(P, Count);
try
Move(pBuf^, P^, Count);
{ xor data }
EncryptBuf(P, Count);
{ write data }
Result := FFile.Write(P^, Count);
finally
FreeMem(P, Count);
end;
end;
end;
//------------------------------------------------------------------------------
function TDataFile.WriteStream(Section, Ident: string; Stream: TStream): Integer;
var
pBuf : Pointer;
begin
{ init buffer }
GetMem(pBuf, Stream.Size);
try
Stream.Seek(0, soFromBeginning);
Stream.Read(pBuf^, Stream.Size);
{ write data }
Result := WriteData(Section, Ident, pBuf, Stream.Size);
finally
FreeMem(pBuf, Stream.Size);
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteString(Section, Ident, Value: string);
var
pBuf : pChar;
begin
pBuf := StrNew(PChar(Value));
try
WriteData(Section, Ident, pBuf, StrLen(pBuf) + 1);
finally
StrDispose(pBuf);
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteInteger(Section, Ident: string; Value: Integer);
begin
WriteData(Section, Ident, @Value, SizeOf(Integer));
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteDouble(Section, Ident: string; Value: Double);
begin
WriteData(Section, Ident, @Value, SizeOf(Double));
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteExtended(Section, Ident: string; Value: Extended);
begin
WriteData(Section, Ident, @Value, SizeOf(Extended));
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteDateTime(Section, Ident: string; Value: TDateTime);
begin
WriteData(Section, Ident, @Value, SizeOf(TDateTime));
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteBoolean(Section, Ident: string; Value: Boolean);
begin
WriteData(Section, Ident, @Value, SizeOf(Boolean));
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteStrings(Section, Ident: string; List: TStrings);
var
Buf : TMemoryStream;
begin
Buf := TMemoryStream.Create;
try
List.SaveToStream( Buf );
WriteStream(Section, Ident, Buf);
finally
Buf.Free;
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.WriteFont(Section, Ident: string; Font: TFont);
var
pBuf: Pointer;
pPos: PChar;
Len : Integer;
begin
Len := SizeOf(ISaveFont) + Length(Font.Name) + 1;
GetMem(pBuf, Len);
try
pSaveFont(pBuf)^.CharSet := Font.Charset;
pSaveFont(pBuf)^.Color := Font.Color;
pSaveFont(pBuf)^.Pitch := Font.Pitch;
pSaveFont(pBuf)^.Size := Font.Size;
pSaveFont(pBuf)^.Style := Font.Style;
pPos := pBuf;
inc(pPos, SizeOf(ISaveFont));
StrPCopy(pPos, Font.Name);
WriteData(Section, Ident, pBuf, Len);
finally
FreeMem(pBuf, Len);
end;
end;
{------------------------------------------------------------------------------}
{ delete }
{------------------------------------------------------------------------------}
procedure TDataFile.Delete(Section, Ident: string);
var
BufPos : Integer;
HdrPos : Integer;
EndPos : Integer;
FileSize : Integer;
Count : Integer;
Hdr : IDataHeader;
pBuf : Pointer;
begin
if( FindIdent(Section, Ident, @Hdr) )then
begin
FileSize := FFile.Size;
BufPos := FFile.Position;
HdrPos := BufPos - SizeOf(IDataHeader);
{ seek to end buffer }
EndPos := FFile.Seek(Hdr.Size, soFromCurrent);
Count := FileSize - EndPos;
GetMem(pBuf, Count);
try
FFile.Read(pBuf^, Count);
FFile.Seek(HdrPos, soFromBeginning);
FFile.Write(pBuf^, Count);
FFile.Size := FileSize - ( Hdr.Size + SizeOf(IDataHeader) );
finally
FreeMem(pBuf, Count);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.DeleteSection(Section: string);
var
BufPos : Integer;
HdrPos : Integer;
EndPos : Integer;
Size : Integer;
Count : Integer;
Hdr : IDataHeader;
pBuf : Pointer;
begin
while FindIdent(Section, SECTION_TEST, @Hdr)do
begin
Size := FFile.Size;
BufPos := FFile.Position;
HdrPos := BufPos - SizeOf(IDataHeader);
{ Seek to end buffer }
EndPos := FFile.Seek(Hdr.Size, soFromCurrent);
Count := Size - EndPos;
GetMem(pBuf, Count);
try
FFile.Read(pBuf^, Count);
FFile.Seek(HdrPos, soFromBeginning);
FFile.Write(pBuf^, Count);
FFile.Size := Size - ( Hdr.Size + SizeOf(IDataHeader) );
finally
FreeMem(pBuf, Count);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.DecryptBuf(pBuf: Pointer; BufLen: Integer);
var
I: Integer;
P: pByte;
begin
P := pBuf;
if( FCodeKey <> '' )then
for I := 0 to BufLen - 1 do
begin
P^ := Byte(FCodeKey[1 + ((I - 1) mod Length(FCodeKey))]) xor P^;
inc(P);
end;
end;
//------------------------------------------------------------------------------
procedure TDataFile.EncryptBuf(pBuf: Pointer; BufLen: Integer);
begin
DecryptBuf(pBuf, BufLen);
end;
//------------------------------------------------------------------------------
end.