LoadTreeFromStream- eternal loop- TeeTreeFindGlobalComponent

TeeTree VCL for Borland Delphi and C++ Builder.
Post Reply
Keith
Newbie
Newbie
Posts: 17
Joined: Tue Mar 09, 2004 5:00 am
Location: South Africa
Contact:

LoadTreeFromStream- eternal loop- TeeTreeFindGlobalComponent

Post by Keith » Fri Feb 23, 2007 11:49 am

Support,

I see this has come up before, but I did not see a solution.

I am using SaveTreeToStream & LoadTreeFromStream to copy the contents of one TTree to another, but I am getting an eternal loop in the 'TeeTreeFindGlobalComponent' procedure when calling LoadTreeFromStream.

Here is the sample code:

Code: Select all

procedure TForm1.cmdStreamClick(Sender: TObject);
var
  msTemp: TStream;

begin
  Form2.Show;

  Form1.TreeSource.NoOwnerShapes := False;
  Form2.TreeDest.NoOwnerShapes := False;

  msTemp := TMemoryStream.Create;
  try
    SaveTreeToStream(TCustomTree(Form1.TreeSource), msTemp);
    msTemp.Position := 0;
    LoadTreeFromStream(TCustomTree(Form2.TreeDest), msTemp);
  finally
    FreeAndNil(msTemp);
  end;
end;

Form1 contains the source TTree and Form2 contains the destination TTree.


Kind regards,
Keith Blows

Keith
Newbie
Newbie
Posts: 17
Joined: Tue Mar 09, 2004 5:00 am
Location: South Africa
Contact:

Post by Keith » Fri Feb 23, 2007 12:50 pm

Found the problem- seems to be bug in that the component's name is saved with the stream, which causes a problem when trying to load it. This text must be removed from the stream. Here is a simple routine to handle it for you:

Code: Select all

procedure FixedSaveTreeToStream(ATeeTree: TCustomTree; AStream: TStream);
var
  lTemp: TMemoryStream;
  lChar: Byte;
  lComponentNameLen: Integer;

begin
  lComponentNameLen := Length(ATeeTree.Name);
  lTemp := TMemoryStream.Create;
  try
    SaveTreeToStream(ATeeTree, lTemp); // This saves the component's name in the stream, which causes problems when loading...

    // We need to delete the name from the resultant stream- it is located at position 11
    lTemp.Position := 0;
    AStream.CopyFrom(lTemp, 10);
    lChar := 0;  AStream.Write(lChar, 1);
    lTemp.Position := 11 + lComponentNameLen;
    AStream.CopyFrom(lTemp, lTemp.Size - lComponentNameLen - 11);
  finally
    FreeAndNil(lTemp);
  end;
end;


procedure TForm1.cmdStreamClick(Sender: TObject);
var
  lTemp: TMemoryStream;

begin
  Form2.Show;
  lTemp := TMemoryStream.Create;
  try
    FixedSaveTreeToStream(TreeSource, lTemp);
    lTemp.Position := 0;
    LoadTreeFromStream(TCustomTree(Form2.TreeDest), lTemp);
  finally
  end;
end;

Post Reply