-
Notifications
You must be signed in to change notification settings - Fork 0
/
correctedstreams.pas
119 lines (95 loc) · 2.42 KB
/
correctedstreams.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
{*****************************************************************************
Author : Frédéric Libaud (http://www.libaudfrederic.fr
Version : 1.0
History:
2017 09 30 - 1.0 - First version
Implementation of corrected Position property
Implementation of EOF property and
GoToStart/GoToEnd method
*****************************************************************************}
unit correctedstreams;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
const
soStart = 0;
soEnd = -1;
type
{ TCorrectedFileStream }
{
This class is provided for Position property bug correction
and TFileStream extensions.
In LCL (Lazarus 1.2.6), property Position doesn't work correctly
and make bug like SIGSEV.
}
TCorrectedFileStream = class(TFileStream)
private
function GetPosition: integer;
procedure SetPosition(const aPosition: integer);
function GetEOF: boolean;
procedure SetEOF(const aEOF: boolean);
public
procedure GoToStart;
procedure GoToEnd;
function MoveTo(const aOffset: Int64; aOrigin: TSeekOrigin): Int64;
property Position: integer read GetPosition write SetPosition;
property EOF: boolean read GetEOF write SetEOF;
end;
{ TCorrectedFileStream }
implementation
{ TCorrectedFileStream }
function TCorrectedFileStream.GetPosition: integer;
begin
try
Result:= Seek(0, soCurrent);
except
Result:= Size;
end;
end;
procedure TCorrectedFileStream.SetPosition(const aPosition: integer);
begin
if aPosition < Size then
Seek(aPosition, soFromBeginning)
else
Seek(0, soFromEnd);
end;
procedure TCorrectedFileStream.GoToStart;
begin
if Size > 0 then
Seek(0, soFromBeginning);
end;
procedure TCorrectedFileStream.GoToEnd;
begin
if Size > 0 then
Seek(0, soFromEnd);
end;
function TCorrectedFileStream.GetEOF: boolean;
begin
if Size > 0 then
begin
if seek(0, soCurrent) >= Size then
Result:= true
else
Result:= false;
end
else
Result:= true;
end;
procedure TCorrectedFileStream.SetEOF(const aEOF: boolean);
begin
if aEOF then
GotoEnd;
end;
function TCorrectedFileStream.MoveTo(const aOffset: Int64; aOrigin: TSeekOrigin): Int64;
var
_Size: int64;
begin
_Size:= Size;
if (aOffset >= 0) and (aOffset <= _Size) then
Result:= Seek(aOffset, aOrigin)
else
Result:= Seek(0, soCurrent);
end;
{ TCorrectedFileStream }
end.