-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringrecorder.pas
127 lines (109 loc) · 2.59 KB
/
stringrecorder.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit stringrecorder;
interface
type
TStringRecorder = class abstract
public
procedure Add(const Value: UTF8String); virtual; abstract;
end;
TStringRecorderForStrings = class(TStringRecorder)
private
type
PEntry = ^TEntry;
TEntry = record
Value: AnsiString;
Next: PEntry;
end;
var
FFirst, FLast: PEntry;
FLength: Cardinal;
function GetString(): UTF8String;
public
destructor Destroy(); override;
procedure Add(const Value: UTF8String); override;
property Value: UTF8String read GetString;
property Length: Cardinal read FLength;
end;
TStringRecorderForFile = class abstract (TStringRecorder)
private
FFile: Text;
public
procedure Add(const Value: UTF8String); override;
end;
TStringRecorderForFileByFile = class(TStringRecorderForFile)
public
constructor Create(var AFile: Text);
end;
TStringRecorderForFileByName = class(TStringRecorderForFile)
public
constructor Create(const FileName: AnsiString);
destructor Destroy(); override;
end;
implementation
destructor TStringRecorderForStrings.Destroy();
var
Next: PEntry;
begin
while (Assigned(FFirst)) do
begin
Next := FFirst^.Next;
Dispose(FFirst);
FFirst := Next;
end;
inherited;
end;
procedure TStringRecorderForStrings.Add(const Value: UTF8String);
var
Addition: PEntry;
begin
New(Addition);
Addition^.Value := Value;
Addition^.Next := nil;
if (not Assigned(FFirst)) then
begin
FFirst := Addition;
end
else
begin
FLast^.Next := Addition;
end;
FLast := Addition;
Inc(FLength, system.Length(Value));
end;
function TStringRecorderForStrings.GetString(): UTF8String;
var
Current: PEntry;
Index: Cardinal;
begin
SetLength(Result, FLength);
Index := 1;
Current := FFirst;
while (Assigned(Current)) do
begin
Move(Current^.Value[1], Result[Index], system.Length(Current^.Value));
Inc(Index, system.Length(Current^.Value));
Current := Current^.Next;
end;
end;
procedure TStringRecorderForFile.Add(const Value: UTF8String);
begin
Write(FFile, Value);
end;
constructor TStringRecorderForFileByFile.Create(var AFile: Text);
begin
FFile := AFile;
inherited Create();
end;
constructor TStringRecorderForFileByName.Create(const FileName: AnsiString);
begin
Assign(FFile, FileName);
Rewrite(FFile);
inherited Create();
end;
destructor TStringRecorderForFileByName.Destroy();
begin
Close(FFile);
inherited;
end;
end.