-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOctoid.SourceReader.pas
93 lines (82 loc) · 1.98 KB
/
Octoid.SourceReader.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
unit Octoid.SourceReader;
interface
uses
System.Classes;
type
TSourceReader = class(TObject)
private
FCurrentLine: Integer;
FCurrentText: string;
FFileName: string;
FReader: TStreamReader;
procedure CheckReader(const AFileName: string);
function ReadLine(const ALine: Integer; var AText: string): Boolean;
public
destructor Destroy; override;
function ReadSubstring(const AFileName: string; const ALine, AColStart, AColEnd: Integer): string;
end;
implementation
uses
System.SysUtils;
{ TSourceReader }
procedure TSourceReader.CheckReader(const AFileName: string);
begin
if not SameText(AFileName, FFileName) then
begin
FReader.Free;
FReader := TStreamReader.Create(AFileName, TEncoding.ANSI);
FReader.Rewind;
FFileName := AFileName;
FCurrentLine := 0;
end;
end;
destructor TSourceReader.Destroy;
begin
FReader.Free;
inherited;
end;
function TSourceReader.ReadLine(const ALine: Integer; var AText: string): Boolean;
var
I: Integer;
LText: string;
begin
if ALine > FCurrentLine then
begin
for I := 1 to (ALine - FCurrentLine) do
begin
if not FReader.EndOfStream then
begin
LText := FReader.ReadLine;
Inc(FCurrentLine);
end
else
Exit(False); // <======
end;
AText := LText;
FCurrentText := AText;
end
else if ALine < FCurrentLine then
begin
FReader.Rewind;
FCurrentLine := 0;
ReadLine(ALine, AText);
end
else
AText := FCurrentText;
Result := True;
end;
function TSourceReader.ReadSubstring(const AFileName: string; const ALine, AColStart, AColEnd: Integer): string;
var
LText: string;
begin
Result := '';
CheckReader(AFileName);
if ReadLine(ALine, LText) then
begin
if AColEnd > 0 then
Result := LText.Substring(AColStart - 1, AColEnd - AColStart + 1)
else
Result := LText.Substring(AColStart - 1);
end;
end;
end.