unit Unit1;
interface
uses classes,SysUtils;
type
TDaten=class
constructor create (s:
string;i:integer);
private
mytext:
String;
myinteger:integer;
public
procedure readFromStream(aStream:tstream);
function writeToStream(aStream:tstream):integer;
end;
TDatenContainer=class
constructor create(fname:
string);
overload;
destructor destroy;
override;
private
filename:
string;
mylist:tlist;
public
procedure addItem(td:tdaten);
function getItemAt(i:integer):tdaten;
procedure readFromStream(aStream:Tstream);
procedure writeToStream(aStream:TStream);
end;
implementation
constructor TDaten.create(s:
string;i:integer);
begin
mytext:=s;
myinteger:=i;
end;
procedure tDaten.readFromStream(aStream:Tstream);
var len:integer;
begin
aStream.
read(myinteger,sizeof(myinteger));
aStream.
read(len,sizeof(len));
setLength(mytext,len);
aStream.
read(mytext[1],sizeof(mytext));
//...
end;
function TDaten.writeToStream(aStream:tstream):integer;
var len:integer;
begin
result:=aStream.position;
aStream.
write(myinteger,sizeof(myinteger));
len:=length(mytext);
aStream.
write(len,sizeof(len));
aStream.
write(mytext[1],sizeof(mytext));
//...
end;
constructor TDatenContainer.create(fname:
string);
begin
inherited create;
filename:=fname;
mylist:=tlist.create;
end;
procedure TDatenContainer.addItem(td:tdaten);
begin
mylist.add(td);
end;
function TDatenContainer.getItemAt(i:integer):tdaten;
begin
result:=nil;
if i<mylist.count
then result:=mylist.items[i];
end;
procedure TDatenContainer.readFromStream;
var mystream:tfileStream;
mydaten:tdaten;
i:integer;
begin
try
mystream:=tfilestream.create(filename,fmopenread);
mystream.
read(i,sizeof(i));
for i:=0
to mylist.count-1
do
begin
mydaten:=TDaten.create;
//erst Instanz erzeugen, sorry!
mydaten.readFromStream(mystream);
Mylist.add(mydaten);
end;
finally
mystream.free;
end;
end;
procedure TDatenContainer.writeToStream;
var mystream:tfileStream;
mydaten:tdaten;
i,len:integer;
begin
try
mystream:=tfilestream.create(filename,fmopenread);
len:=mylist.count;
mystream.
write(len,sizeof(len));
for i:=0
to mylist.count-1
do
begin
mydaten:=mylist.items[i];
mydaten.writeToStream(mystream);
end;
finally
mystream.free;
end;
end;
destructor TDatenContainer.destroy;
begin
mylist.free;
inherited;
end;
end.