Recent

Author Topic: How to read TMemoryStream directly from Memo.Lines.Text?  (Read 1807 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
How to read TMemoryStream directly from Memo.Lines.Text?
« on: February 14, 2024, 10:17:54 am »
How do you read TMemoryStream directly from a Memo.Lines.Text, basically from a TStringList?

The example I could find and currently use entails saving the TStringList to a file, and loading the MemoryStream from the file.

Can't it be done directly?

Code: Pascal  [Select][+][-]
  1.   compStream := TMemoryStream.Create;
  2.   try
  3.     mmoTriedLFM.Lines.SaveToFile('atempFile');
  4.     compStream.LoadFromFile('atempFile');
  5.     compStream.Position := 0;
  6.     component := FActiveForm.FindComponent(GridSelectedComponentName);
  7.     try
  8.       ReadComponentFromTextStream(compStream, component, @OnFindClass, nil, nil);
  9.     except
  10.       on E : Exception do
  11.         ProcessLoadLFMException(E);
  12.     end;
  13.  
  14.   finally
  15.     compStream.Free;
  16.   end;
  17.  
Lazarus 3.0/FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #1 on: February 14, 2024, 10:22:15 am »
Yes
Code: Pascal  [Select][+][-]
  1. compStream := TMemoryStream.Create;
  2.   try
  3.     mmoTriedLFM.Lines.SaveToStream(compStream);
  4.     compStream.Position := 0;
  5.     component := FActiveForm.FindComponent(GridSelectedComponentName);
  6.     try
  7.       ReadComponentFromTextStream(compStream, component, @OnFindClass, nil, nil);
  8.     except
  9.       on E : Exception do
  10.         ProcessLoadLFMException(E);
  11.     end;
  12.  
  13.   finally
  14.     compStream.Free;
  15.   end;
Untested.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #2 on: February 14, 2024, 10:25:33 am »
Hi
edit: Thaddy was quicker...
Code: Pascal  [Select][+][-]
  1. compStream := TMemoryStream.Create;
  2. try
  3.   { TStrings can save directly to streams }
  4.   mmoTriedLFM.Lines.SaveToStream(compStream);
  5.   compStream.Position := 0;
  6.   component := FActiveForm.FindComponent(GridSelectedComponentName);
  7.     try
  8.       ReadComponentFromTextStream(compStream, component, @OnFindClass, nil, nil);
  9.     except
  10.       on E : Exception do
  11.         ProcessLoadLFMException(E);
  12.     end;
  13. finally
  14.   compStream.Free;
  15. end;
  16.  
I've used "SaveToStream" a lot.
I even think that TMemo.Lines.SaveToFile uses it internally.
Regards Benny
« Last Edit: February 14, 2024, 10:28:30 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #3 on: February 14, 2024, 10:26:38 am »
You duplicate me Benny....
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

cdbc

  • Hero Member
  • *****
  • Posts: 2683
    • http://www.cdbc.dk
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #4 on: February 14, 2024, 10:29:57 am »
Hi Thaddy, it's impossible to duplicate you, when you're writing  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #5 on: February 14, 2024, 12:53:37 pm »
i also use savetostream a lot.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

ASerge

  • Hero Member
  • *****
  • Posts: 2477
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #6 on: February 14, 2024, 09:06:33 pm »
Code: Pascal  [Select][+][-]
  1. SS := TStringStream.Create(mmoTriedLFM.Text);
  2. try
  3.   component := FActiveForm.FindComponent(GridSelectedComponentName);
  4.   try
  5.     ReadComponentFromTextStream(SS, component, @OnFindClass, nil, nil);
  6.   except
  7.     on E : Exception do
  8.       ProcessLoadLFMException(E);
  9.   end;
  10. finally
  11.   SS.Free;
  12. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #7 on: February 14, 2024, 09:09:06 pm »
Neat
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

jamie

  • Hero Member
  • *****
  • Posts: 7601
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #8 on: February 15, 2024, 01:01:06 am »
How do you read TMemoryStream directly from a Memo.Lines.Text, basically from a TStringList?

The example I could find and currently use entails saving the TStringList to a file, and loading the MemoryStream from the file.

Can't it be done directly?

Code: Pascal  [Select][+][-]
  1.   compStream := TMemoryStream.Create;
  2.   try
  3.     mmoTriedLFM.Lines.SaveToFile('atempFile');
  4.     compStream.LoadFromFile('atempFile');
  5.     compStream.Position := 0;
  6.     component := FActiveForm.FindComponent(GridSelectedComponentName);
  7.     try
  8.       ReadComponentFromTextStream(compStream, component, @OnFindClass, nil, nil);
  9.     except
  10.       on E : Exception do
  11.         ProcessLoadLFMException(E);
  12.     end;
  13.  
  14.   finally
  15.     compStream.Free;
  16.   end;
  17.  

Am I missing something, or did I miss the joke? %)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   TheMemoryStream:TmemoryStream;
  4. begin
  5.   TheMemoryStream := TMemoryStream.Create;
  6.   Memo1.Lines.SaveToStream(TheMemoryStream);
  7.   caption := TheMemoryStream.Size.Tostring;
  8.   TheMemorystream.Free;
  9. end;
  10.  

I must have missed the train.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to read TMemoryStream directly from Memo.Lines.Text?
« Reply #9 on: February 15, 2024, 07:02:06 am »
No, you missed the component handling. it is a memo with a lfm file that he wants to read the component(s) back from.
« Last Edit: February 15, 2024, 07:04:40 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018