-
Notifications
You must be signed in to change notification settings - Fork 0
/
cS_memstream.pas
92 lines (76 loc) · 2.03 KB
/
cS_memstream.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
unit cS_memstream;
interface
uses
Classes;
type
TAbstractFixedMemoryStream = class
private
fp: pointer;
fpos: Longint;
fsize: Longint;
public
property p: pointer read fp;
property size: Longint read fsize;
procedure WriteBuffer(const Buffer; Count: Longint);
procedure ReadBuffer(var Buffer; Count: Longint);
end;
TFixedMemoryStream_out = class(TAbstractFixedMemoryStream)
public
constructor Create(const size: cardinal);
destructor Destroy; override;
end;
TFixedMemoryStream_in = class(TAbstractFixedMemoryStream)
public
constructor Create(const aP: pointer; const size: cardinal);
end;
implementation
uses SysUtils;
constructor TFixedMemoryStream_out.Create(const size: cardinal);
begin
inherited Create;
GetMem(fp,size);
fsize := size;
end;
destructor TFixedMemoryStream_out.Destroy;
begin
FreeMem(fp);
inherited;
end;
procedure TAbstractFixedMemoryStream.WriteBuffer(const Buffer; Count: Longint);
var epos: Longint;
begin
if (fpos >= 0) and (Count >= 0) then
begin
epos := fpos + Count;
if epos > 0 then
begin
if epos > fsize then
raise Exception.Create('TFixedMemoryStream.Write(): Write over end!');
System.Move(Buffer, Pointer(Longint(fp) + fpos)^, Count);
fpos := ePos;
Exit;
end;
end;
raise Exception.Create('TFixedMemoryStream.Write(): ERROR!');
end;
procedure TAbstractFixedMemoryStream.ReadBuffer(var Buffer; Count: Longint);
begin
if (fpos >= 0) and (Count >= 0) then
begin
if (fsize - fpos) < Count then
raise Exception.Create('TFixedMemoryStream.Read(): to long!');
Move(Pointer(Longint(fp) + fpos)^, Buffer, Count);
Inc(fpos, Count);
Exit;
end;
raise Exception.Create('TFixedMemoryStream.Write(): ERROR!');
end;
{ TFixedMemoryStream_in }
constructor TFixedMemoryStream_in.Create(const aP: pointer;
const size: cardinal);
begin
inherited Create;
fp := aP;
fsize := size;
end;
end.