-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPLOBJ.PAS
772 lines (691 loc) · 17.9 KB
/
APLOBJ.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
{$I COMPILER.INC}
unit AplObj;
interface
uses
AplConst,
AplStr,
Strings;
type
PObject = ^TObject;
PException = ^TException;
PIdentifiable = ^TIdentifiable;
PStatusObject = ^TStatusObject;
PStringBuilder = ^TStringBuilder;
TComparer = function(AItem1, AItem2: pointer): integer;
TPredicate = function(AItem: pointer; var AValue): boolean;
TCustomErrorMessageProc = function(AErrorCode: word; var AFound: boolean): string;
TObjectStatus = integer;
TExceptionCode = word;
TExceptionSeverity =
(
esNone,
esHint,
esWarning,
esError,
esFatal
);
TObject = object
constructor Create;
function IsPersistent: boolean; virtual;
procedure Init; virtual;
procedure Assign(var ASource: TObject); virtual;
procedure AssignTo(var ADest: TObject); virtual;
procedure Abstract; virtual;
destructor Free; virtual;
end;
TIdentifiable = object(TObject)
private
public
Id: PChar;
constructor CreateId(AId: string);
procedure Init; virtual;
procedure Assign(var ASource: TObject); virtual;
procedure SetId(const AId: string);
function GetId: string;
destructor Free; virtual;
end;
TException = object(TObject)
private
public
Code: TExceptionCode;
Severity: TExceptionSeverity;
InnerException: PException;
constructor Create(ACode: TExceptionCode);
procedure Init; virtual;
procedure Clear;
procedure Assign(var ASource: TObject); virtual;
function Message: string;
destructor Free; virtual;
end;
TStatusObject = object(TIdentifiable)
private
public
Exception: PException;
Status: TObjectStatus;
procedure Init; virtual;
procedure Raise(ACode: TExceptionCode);
procedure RaiseException(AException: PException);
procedure RaiseNullParameter(AParam: string);
procedure RaiseInvalidOperation(AMessage: string);
procedure RaiseWarning(ACode: TExceptionCode);
procedure RaiseHint(ACode: TExceptionCode);
procedure RaiseSeverity(ACode: TExceptionCode; ASeverity: TExceptionSeverity);
procedure ReRaise(AObject: PStatusObject);
procedure ClearException;
procedure Assign(var ASource: TObject); virtual;
function HasException: boolean;
function NoException: boolean;
function GetBaseException: PException;
function CheckReRaise(AObject: PStatusObject): boolean;
function NilPtr(APtr: pointer; ACode: TExceptionCode): boolean;
function NilPtrSeverity(APtr: pointer; ACode: TExceptionCode;
ASeverity: TExceptionSeverity): boolean;
destructor Free; virtual;
end;
TStringBuilder = object(TStatusObject)
private
FBuffer: PChar;
FLength: word;
FCapacity: word;
procedure SetCapacity(ACapacity: word);
function Grow(AMin: word): word;
public
constructor CreateCapacity(ACapacity: word);
constructor CreateString(const AString: string);
destructor Free; virtual;
procedure Init; virtual;
function Append(AString: PChar): PStringBuilder;
function AppendString(const AString: string): PStringBuilder;
function AppendBuilder(const AStringBuilder: TStringBuilder): PStringBuilder;
function AppendChar(AChar: char): PStringBuilder;
function AppendLine(AString: PChar): PStringBuilder;
function AppendLineString(const AString: string): PStringBuilder;
function Replace(AOldValue, ANewValue: PChar;
ACompareOptions: TStringCompareOptions): PStringBuilder;
function ReplaceString(const AOldValue, ANewValue: string;
ACompareOptions: TStringCompareOptions): PStringBuilder;
function Insert(AIndex: word; AString: PChar): PStringBuilder;
function InsertString(AIndex: word; const AString: string): PStringBuilder;
function InsertChar(AIndex: word; AChar: char): PStringBuilder;
function Clear: PStringBuilder;
function Find(AString: PChar; AStart: word;
ACompareOptions: TStringCompareOptions; var APos: word): PChar;
function FindString(const AString: string; AStart: word;
ACompareOptions: TStringCompareOptions; var APos: word): PChar;
function ToPChar: PChar;
function ToString: string;
function AsPChar: PChar;
function Length: word;
function Capacity: word;
end;
{ Represents a variable of 0 size that can be used as a dummy value for var
parameters that require a variable where none is needed. }
TNothing = object
end;
procedure FreeAndNil(var AObject); far;
var
OutOfMemoryException: PException;
Nothing: TNothing;
implementation
uses
AplTypes;
procedure FreeAndNil(var AObject);
var
obj: PObject;
ptr: PPointer;
begin
obj := PObject(AObject);
if Assigned(obj) then
Dispose(obj, Free);
ptr := @AObject;
ptr^ := nil;
end;
constructor TObject.Create;
begin
Init;
end;
procedure TObject.Abstract;
begin
RunError(211);
end;
procedure TObject.Init;
begin
end;
procedure TObject.Assign(var ASource: TObject);
begin
end;
procedure TObject.AssignTo(var ADest: TObject);
begin
ADest.Assign(Self);
end;
function TObject.IsPersistent: boolean;
begin
IsPersistent := false;
end;
destructor TObject.Free;
begin
end;
constructor TIdentifiable.CreateId(AId: string);
begin
inherited Create;
Id := TString.New(AId);
end;
procedure TIdentifiable.Init;
begin
inherited Init;
Id := nil;
end;
procedure TIdentifiable.Assign(var ASource: TObject);
var
source: PIdentifiable;
begin
inherited Assign(ASource);
source := PIdentifiable(@ASource);
SetId(source^.GetId);
end;
function TIdentifiable.GetId: string;
begin
if not Assigned(Id) then
GetId := ''
else
GetId := TString.GetString(Id);
end;
procedure TIdentifiable.SetId(const AId: string);
begin
TString.AssignString(Id, AId);
end;
destructor TIdentifiable.Free;
begin
TString.Free(Id);
inherited Free;
end;
constructor TException.Create(ACode: TExceptionCode);
begin
inherited Create;
Code := ACode;
end;
procedure TException.Init;
begin
inherited Init;
Code := 0;
Severity := esError;
InnerException := nil;
end;
procedure TException.Clear;
begin
FreeAndNil(InnerException);
Code := ecNone;
Severity := esError;
end;
destructor TException.Free;
begin
Clear;
inherited Free;
end;
procedure TException.Assign(var ASource: TObject);
var
source: PException;
begin
inherited Assign(ASource);
source := PException(@ASource);
Clear;
if Assigned(source^.InnerException) then begin
InnerException := New(PException, Create(ecNone));
InnerException^.Assign(source^.InnerException^);
end;
Code := source^.Code;
Severity := source^.Severity;
end;
function TException.Message: string;
begin
Message := ErrorMessage(Code);
end;
function TStatusObject.GetBaseException: PException;
var
result: PException;
begin
GetBaseException := Exception;
if not Assigned(Exception) then
exit;
result := Exception;
while Assigned(Exception^.InnerException) do
result := result^.InnerException;
GetBaseException := result;
end;
function TStatusObject.HasException: boolean;
begin
HasException := Assigned(Exception);
end;
function TStatusObject.NoException: boolean;
begin
NoException := not Assigned(Exception);
end;
procedure TStatusObject.Raise(ACode: TExceptionCode);
var
newException: PException;
prev: PException;
begin
newException := New(PException, Create(ACode));
if not Assigned(newException) then
newException := OutOfMemoryException;
if not Assigned(Exception) then begin
Exception := newException;
exit;
end;
prev := Exception;
Exception := newException;
Exception^.InnerException := prev;
Status := sException;
end;
procedure TStatusObject.RaiseException(AException: PException);
begin
if not Assigned(AException) then
exit;
RaiseSeverity(AException^.Code, AException^.Severity);
end;
procedure TStatusObject.RaiseSeverity(ACode: TExceptionCode; ASeverity: TExceptionSeverity);
begin
Raise(ACode);
Exception^.Severity := ASeverity;
end;
function TStatusObject.CheckReRaise(AObject: PStatusObject): boolean;
var
result: boolean;
begin
CheckReRaise := false;
if not Assigned(AObject) then
exit;
result := AObject^.HasException;
if result then
ReRaise(AObject);
CheckReRaise := result;
end;
function TStatusObject.NilPtr(APtr: pointer; ACode: TExceptionCode): boolean;
var
result: boolean;
begin
result := not Assigned(APtr);
if result then
Raise(ACode);
NilPtr := result;
end;
function TStatusObject.NilPtrSeverity(APtr: pointer; ACode: TExceptionCode;
ASeverity: TExceptionSeverity): boolean;
var
result: boolean;
begin
result := not Assigned(APtr);
if result then
RaiseSeverity(ACode, ASeverity);
NilPtrSeverity := result;
end;
procedure TStatusObject.ReRaise(AObject: PStatusObject);
begin
if not (Assigned(AObject) and AObject^.HasException) then
exit;
RaiseException(AObject^.Exception);
AObject^.ClearException;
end;
procedure TStatusObject.RaiseWarning(ACode: TExceptionCode);
begin
RaiseSeverity(ACode, esWarning);
end;
procedure TStatusObject.RaiseHint(ACode: TExceptionCode);
begin
RaiseSeverity(ACode, esHint);
end;
procedure TStatusObject.ClearException;
begin
FreeAndNil(Exception);
Status := sOk;
end;
destructor TStatusObject.Free;
begin
ClearException;
inherited Free;
end;
procedure TStatusObject.Assign(var ASource: TObject);
var
source: PStatusObject;
begin
inherited Assign(ASource);
source := PStatusObject(@ASource);
ClearException;
Exception := New(PException, Create(ecNone));
Exception^.Assign(source^.Exception^);
Status := source^.Status;
end;
procedure TStatusObject.RaiseNullParameter(AParam: string);
begin
Raise(ecNullParameter);
end;
procedure TStatusObject.RaiseInvalidOperation(AMessage: string);
begin
Raise(ecInvalidOperation);
end;
procedure TStatusObject.Init;
begin
inherited Init;
Exception := nil;
Status := sOk;
end;
constructor TStringBuilder.CreateCapacity(ACapacity: word);
begin
inherited Create;
Grow(ACapacity);
end;
constructor TStringBuilder.CreateString(const AString: string);
begin
inherited Create;
Grow(System.Length(AString) + 1);
AppendString(AString);
end;
procedure TStringBuilder.Init;
begin
inherited Init;
FBuffer := nil;
FLength := 0;
FCapacity := 0;
end;
destructor TStringBuilder.Free;
begin
if Assigned(FBuffer) then
FreeMem(FBuffer, FCapacity);
FBuffer := nil;
inherited Free;
end;
procedure TStringBuilder.SetCapacity(ACapacity: word);
var
newBuffer: PChar;
begin
if ACapacity = FCapacity then
exit;
if ACapacity = 0 then begin
if Assigned(FBuffer) then
FreeMem(FBuffer, FCapacity);
FBuffer := nil;
FCapacity := 0;
exit;
end;
if not Assigned(FBuffer) then begin
GetMem(FBuffer, ACapacity);
if NilPtr(FBuffer, ecNotEnoughMemory) then
exit;
FillChar(FBuffer^, ACapacity, 0);
FCapacity := ACapacity;
exit;
end;
GetMem(newBuffer, ACapacity);
FillChar(newBuffer^, ACapacity, 0);
if NilPtr(newBuffer, ecNotEnoughMemory) then
exit;
if ACapacity < FCapacity then
Move(FBuffer^, newBuffer^, ACapacity)
else
Move(FBuffer^, newBuffer^, FCapacity);
FreeMem(FBuffer, FCapacity);
FBuffer := newBuffer;
FCapacity := ACapacity;
end;
function TStringBuilder.Grow(AMin: word): word;
var
newCapacity: integer;
begin
newCapacity := 0;
while newCapacity < AMin do begin
if newCapacity > 64 then
newCapacity := (newCapacity * 3) div 2
else if newCapacity > 8 then
Inc(newCapacity, 16)
else
Inc(newCapacity, 4);
end;
SetCapacity(newCapacity);
Grow := newCapacity;
end;
function TStringBuilder.Append(AString: PChar): PStringBuilder;
var
len: word;
cap: word;
begin
if not Assigned(AString) then
exit;
len := StrLen(AString);
if len = 0 then
exit;
cap := FLength + len;
if cap > FCapacity then
Grow(cap);
Move(AString^, FBuffer[FLength], len);
Inc(FLength, len);
Append := @Self;
end;
function TStringBuilder.AppendString(const AString: string): PStringBuilder;
var
len: word;
cap: word;
str: PChar;
begin
len := System.Length(AString);
if System.Length(AString) = 0 then
exit;
cap := FLength + len;
if cap > FCapacity then
Grow(cap);
str := @AString[1];
Move(str^, FBuffer[FLength], len);
Inc(FLength, len);
AppendString := @Self;
end;
function TStringBuilder.AppendBuilder(const AStringBuilder: TStringBuilder): PStringBuilder;
begin
AppendBuilder := Append(AStringBuilder.FBuffer);
end;
function TStringBuilder.AppendChar(AChar: char): PStringBuilder;
begin
AppendChar := Append(@AChar);
end;
function TStringBuilder.AppendLine(AString: PChar): PStringBuilder;
var
str: array[0..1] of char;
begin
str := #13#10;
AppendLine := Append(AString);
Append(@str);
end;
function TStringBuilder.AppendLineString(const AString: string): PStringBuilder;
begin
AppendLineString := AppendString(AString);
AppendChar(#13);
AppendChar(#10);
end;
function TStringBuilder.Replace(AOldValue, ANewValue: PChar;
ACompareOptions: TStringCompareOptions): PStringBuilder;
var
str, ptr: PChar;
pos, len, oldLen, newLen, diff: word;
begin
Replace := @Self;
if not Assigned(AOldValue) then
exit;
pos := 0;
str := Find(AOldValue, pos, ACompareOptions, pos);
if not Assigned(str) then
exit;
newLen := StrLen(ANewValue);
oldLen := StrLen(AOldValue);
len := FLength;
while Assigned(str) do begin
if newLen > oldLen then begin
diff := newLen - oldLen;
Grow(FLength + diff + 1);
str := FBuffer;
Inc(str, pos);
Inc(FLength, diff);
len := FLength - pos;
ptr := str;
Inc(ptr, diff);
Move(str^, ptr^, len - diff);
end
else if newLen < oldLen then begin
diff := oldLen - newLen;
Grow(FLength - diff + 1);
str := FBuffer;
Inc(str, pos);
len := FLength - pos;
ptr := str;
Inc(ptr, newLen);
Move(ptr^, str^, len - newLen);
FillChar(ptr^, diff, 0);
Dec(FLength, diff);
end;
Move(ANewValue^, str^, newLen);
Inc(str, newLen);
str := Find(AOldValue, pos, ACompareOptions, pos);
end;
end;
function TStringBuilder.ReplaceString(const AOldValue, ANewValue: string;
ACompareOptions: TStringCompareOptions): PStringBuilder;
var
old, new: PChar;
oldValue, newValue: string;
begin
oldValue := AOldValue + #0;
newValue := ANewValue + #0;
old := @oldValue[1];
new := @newValue[1];
ReplaceString := Replace(old, new, ACompareOptions);
end;
function TStringBuilder.Insert(AIndex: word; AString: PChar): PStringBuilder;
var
len, cap: word;
ptr, dest: PChar;
begin
Insert := @Self;
if not Assigned(AString) then
exit;
len := StrLen(AString);
if len = 0 then
exit;
if AIndex > FLength then
AIndex := FLength;
cap := FLength + len;
if cap > FCapacity then
Grow(cap);
ptr := FBuffer;
Inc(ptr, AIndex);
dest := ptr;
Inc(dest, len);
Move(ptr^, dest^, FLength - AIndex);
Move(AString^, ptr^, len);
Inc(FLength, len);
end;
function TStringBuilder.InsertString(AIndex: word; const AString: string): PStringBuilder;
var
ptr: PChar;
str: string;
begin
str := AString + #0;
ptr := @str[1];
InsertString := Insert(AIndex, ptr);
end;
function TStringBuilder.InsertChar(AIndex: word; AChar: char): PStringBuilder;
var
str: array[0..1] of char;
begin
str[0] := AChar;
str[1] := #0;
InsertChar := Insert(AIndex, @str);
end;
function TStringBuilder.Clear: PStringBuilder;
begin
SetCapacity(0);
FLength := 0;
Clear := @Self;
end;
function TStringBuilder.ToPChar: PChar;
var
str: PChar;
begin
GetMem(str, FLength + 1);
FillChar(str^, FLength + 1, 0);
if NilPtr(str, ecNotEnoughMemory) then
exit;
StrLCopy(str, FBuffer, FLength);
ToPChar := FBuffer;
end;
function TStringBuilder.ToString: string;
begin
ToString := TString.GetString(FBuffer);
end;
function TStringBuilder.AsPChar: PChar;
begin
AsPChar := FBuffer;
end;
function TStringBuilder.Length: word;
begin
Length := FLength;
end;
function TStringBuilder.Capacity: word;
begin
Capacity := FCapacity;
end;
function TStringBuilder.Find(AString: PChar; AStart: word;
ACompareOptions: TStringCompareOptions; var APos: word): PChar;
var
str, bufferPtr, stringPtr: PChar;
index: word;
begin
Find := nil;
APos := 0;
if not Assigned(AString) then
exit;
if AStart >= FLength then
exit;
str := nil;
index := AStart;
stringPtr := AString;
for index := AStart to FLength - 1 do begin
bufferPtr := FBuffer;
Inc(bufferPtr, index);
if scIgnoreCase in ACompareOptions then begin
if StrLIComp(bufferPtr, stringPtr, StrLen(AString)) = 0 then begin
str := bufferPtr;
APos := index;
break;
end;
end
else begin
if StrLComp(bufferPtr, stringPtr, StrLen(AString)) = 0 then begin
str := bufferPtr;
APos := index;
break;
end;
end;
end;
if not Assigned(str) then
exit;
Find := str;
end;
function TStringBuilder.FindString(const AString: string; AStart: word;
ACompareOptions: TStringCompareOptions; var APos: word): PChar;
var
ptr: PChar;
str: string;
begin
str := AString + #0;
ptr := @str[1];
FindString := Find(ptr, AStart, ACompareOptions, APos);
end;
var
PreviousExitProc: PProc;
procedure Finalize; far;
begin
ExitProc := PreviousExitProc;
FreeAndNil(OutOfMemoryException);
end;
begin
PreviousExitProc := ExitProc;
ExitProc := @Finalize;
OutOfMemoryException := New(PException, Create(ecNotEnoughMemory));
end.