{ -- Author: max hub (aka maximov alias maximus) - 2003 --}
unit dmService;
interface
uses classes, sysUtils, TypInfo;
function SaveCompoToFile(aCompo : TComponent; FileName:
string; asBinary: Boolean = false): boolean;
function LoadCompoFromFile(aCompo : TComponent; FileName:
string; asBinary: Boolean = false): boolean;
implementation
function SaveCompoToFile(aCompo : TComponent; FileName:
string; asBinary: Boolean = false): boolean;
var Stream1:TMemoryStream;
Stream2:TMemoryStream;
format : TStreamOriginalFormat;
begin
result := false;
format := sofText;
Stream1 := TMemoryStream.Create;
Stream2 := TMemoryStream.Create;
try
Stream1.WriteComponent(aCompo);
Stream1.Position := 0;
if not asBinary
then
begin
format := sofText;
ObjectBinaryToText(Stream1,Stream2,format);
end else Stream1.SaveToStream(Stream2);
Stream2.SaveToFile(FileName);
result := true;
finally
Stream1.Free;
Stream2.Free;
end;
end;
function LoadCompoFromFile(aCompo : TComponent; FileName:
string; asBinary: Boolean = false): boolean;
var FileStream:TFileStream;
MemoryStream:TMemoryStream;
format:TStreamOriginalFormat;
begin
result := false;
if not FileExists(fileName)
then exit;
MemoryStream := TMemoryStream.Create;
FileStream := TFileStream.Create(FileName,fmOpenRead);
try
if not asBinary
then
begin
format := sofBinary;
ObjectTextToBinary(FileStream,MemoryStream,format);
end else MemoryStream.LoadFromStream(FileStream);
MemoryStream.Position := 0;
MemoryStream.ReadComponent(aCompo);
result := true;
finally
FileStream.Free;
MemoryStream.Free;
end;
end;
end.