report_reader.pas
3.01 KB
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
{
Free-mtrix - Free cultural selection and social behavior experiments.
Copyright (C) 2016-2017 Carlos Rafael Fernandes Picanço, Universidade Federal do Pará.
The present file is distributed under the terms of the GNU General Public License (GPL v3.0).
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit report_reader;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TRowRange = record
Low,
High : integer;
end;
{ TReportReader }
TReportReader = class
private
FLastRowsX : integer;
FRows : TStringList;
FCols : TStringList;
FRowRange: TRowRange;
FUseRange: Boolean;
function GetColumnOf(AName: string): TStringList;
procedure RangeAsLastXRows;
public
VRow : string; //helper
constructor Create;
destructor Destroy; override;
function Dump : string;
procedure Append(ARow : string);
procedure Extend(ARowExtention : string);
procedure Clean;
procedure SetXLastRows(X:integer);
property Range : TRowRange read FRowRange;
property UseRange : Boolean read FUseRange write FUseRange;
property ColumnOf[AName:string]:TStringList read GetColumnOf;
end;
implementation
uses strutils;
{ TReportReader }
function TReportReader.GetColumnOf(AName: string): TStringList;
var
c,
i : integer;
Row : string;
begin
Result := TStringList.Create;
c := FCols.IndexOf(AName);
if c > -1 then
if FUseRange and (FRowRange.Low <= FRowRange.High) and (FRowRange.Low > 0) then
for i := FRowRange.Low to FRowRange.High do
Result.Append(ExtractDelimited(c+2, FRows[i],[#9,#10]))
else
for Row in FRows do
Result.Append(ExtractDelimited(c+2, Row,[#9,#10]));
end;
constructor TReportReader.Create;
begin
inherited Create;
FUseRange := False;
FRows := TStringList.Create;
FCols := TStringList.Create;
FCols.Delimiter := #9;
FCols.StrictDelimiter := True;
end;
destructor TReportReader.Destroy;
begin
FRows.Free;
FCols.Free;
inherited Destroy;
end;
function TReportReader.Dump: string;
begin
Result := FCols.Text+LineEnding+FRows.Text;
end;
procedure TReportReader.Append(ARow: string);
begin
if FCols.Count = 0 then
FCols.DelimitedText := ARow
else
begin
FRows.Append(ARow);
RangeAsLastXRows;
end;
end;
procedure TReportReader.Extend(ARowExtention: string);
begin
FRows[FRows.Count-1] := FRows[FRows.Count-1] + ARowExtention;
end;
procedure TReportReader.Clean;
begin
FCols.Clear;
FRows.Clear;
end;
procedure TReportReader.SetXLastRows(X: integer);
begin
FLastRowsX:=X;
RangeAsLastXRows;
end;
procedure TReportReader.RangeAsLastXRows;
begin
FRowRange.High := FRows.Count-1;
FRowRange.Low := FRows.Count-FLastRowsX;
{$IFDEF DEBUG}
if FRowRange.Low > FRowRange.High then
WriteLn('Warning: FRowRange.Low > FRowRange.High, range will not be used');
if FRowRange.Low < 0 then
WriteLn('Warning: FRowRange.Low < 0, range will not be used');
{$ENDIF}
end;
end.