-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentarrayunit.pas
120 lines (94 loc) · 2.5 KB
/
segmentarrayunit.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
unit SegmentArrayUnit;
//------------------------------------------------------------------------------
// Фасад для накопления данных сегментированных команд
//------------------------------------------------------------------------------
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
SegmentType=record
guid: string;
count: integer;
segments: TStringList;
end;
SegmentArrayType=array of SegmentType;
{ TSegmentArray }
TSegmentArray = class(TObject)
private
arr: SegmentArrayType;
function getSegmentInd(const guid: string): integer;
public
constructor Create;
destructor Destroy; override;
procedure addSegment(const guid, text: string; const count: integer);
function getGuidFullData(const guid: string; const del_after_get: boolean = true): string;
procedure delSegment(const ind: integer);
end;
var
segments_arr: TSegmentArray;
implementation
{ TSegmentArray }
function TSegmentArray.getSegmentInd(const guid: string): integer;
var i: integer;
begin
result := -1;
for i:=0 to Length(arr)-1 do
if AnsiCompareText(guid, arr[i].guid)=0 then
begin
result := i;
break;
end;
end;
constructor TSegmentArray.Create;
begin
inherited;
end;
destructor TSegmentArray.Destroy;
var i: integer;
begin
for i := 0 to Length(arr)-1 do
arr[i].segments.Free;
SetLength(arr, 0);
inherited Destroy;
end;
procedure TSegmentArray.addSegment(const guid, text: string; const count: integer);
var ind: integer;
begin
ind := getSegmentInd(guid);
if ind<0 then
begin
SetLength(arr, Length(arr)+1);
ind := Length(arr)-1;
arr[ind].segments := TStringList.create;
end;
arr[ind].segments.Add(text);
arr[ind].guid := guid;
arr[ind].count := count;
end;
function TSegmentArray.getGuidFullData(const guid: string;
const del_after_get: boolean): string;
var ind: integer;
begin
result := '';
ind := getSegmentInd(guid);
if ind >= 0 then
if arr[ind].count=arr[ind].segments.Count then
begin
result := StringReplace(arr[ind].segments.Text, #13#10, '', [rfReplaceAll]);
if del_after_get then
delSegment(ind);
end;
end;
procedure TSegmentArray.delSegment(const ind: integer);
var i: integer;
begin
if not ((ind >= 0) and (ind < Length(arr))) then
exit;
arr[ind].segments.Free;
if Length(arr) > 1 then
for i := ind to Length(arr)-1 do
arr[i] := arr[i+1];
SetLength(arr, Length(arr)-1);
end;
end.