INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problems with TMemoryStream.CopyFrom

Problems with TMemoryStream.CopyFrom

(OP)
Hi,
I have a small problem copying part of a MemoryStream (Stream) into another MemoryStream (FoundStr).
I have a long RTF document that I load into a memory stream and then search for different sections based of a bunch of changing requirements. I then want to load the new MemoryStream into the RichEdit Box (Description_RE).
I have the start position already found :- StartPos: Int64;
I have string from the end of the section I am interested in (s) - happens to be '====================================='
The new stream is "FoundStr"
The source stream "Stream" is currently a global veriable.

The line
FoundStr.CopyFrom(Stream, (EndPos - StartPos));
does not work.
I have the correct position as I have put in the line
Stream.Read(Buffer1[0],250);
in its place and the contents of Buffer1 is correct.

Any ideas what I am doing wrong?


procedure TForm1.GetDescription(StartPos: Int64; S: String);
var
Buffer1, Buffer2: array[0..254] of Char;
FoundStr : TMemoryStream;
EndPos : Int64;
begin
Description_RE.Clear;
FillChar(Buffer1, 255, #0);
FillChar(Buffer2, 255, #0);
StrPCopy(@Buffer2, S);
FoundStr := TMemoryStream.Create;
Stream.Position := StartPos;
Stream.Read(Buffer1[0],Length(S));
while (not CompareMem(@Buffer1,@Buffer2,Length(S))) do
begin
if Stream.Position = Stream.Size then exit;
Stream.Position := Stream.Position - (Length(S) - 1);
Stream.Read(Buffer1[0], Length(S));
end;
EndPos := Stream.Position - Length(S);
Stream.Position := StartPos;
FoundStr.CopyFrom(Stream, (EndPos - StartPos));
Description_RE.Lines.LoadFromStream(FoundStr);

FoundStr.Free;

end;

RE: Problems with TMemoryStream.CopyFrom

(OP)
Should have pointed out that there is no error.
Simply nothing shows up in the RichEdit box

RE: Problems with TMemoryStream.CopyFrom

(OP)
OK, Basic mistake. Forgot to set FoundStr.Position := 0; before loading it into the RE.
New Problem though:-
No formatting copies over.
There is text and tables in the selection area and all the formatting is lost.
Is there a way around this?

RE: Problems with TMemoryStream.CopyFrom

Why the memorystreams? Seems much more simple to work with TStringStream or ordinary strings.
As for the RTF, what is the value of the PlainText property of the TRichEdit?

/Daddy

-----------------------------------------------------
Helping people is my job...

RE: Problems with TMemoryStream.CopyFrom

(OP)
I would be happy to use TStringStream or ordinary strings if they retained the formatting.
PlainText property is False.
I am trying to do the same process now by loading the file into Temp_RE, Searching and selecting in there and copying the selected area to Description_RE.
Still Struggling with it at the moment. The search code I have found takes quite a while.
I am not sure if this is the best method.

procedure TForm1.GetDescription(S1, S2: String);
var
StartPos, Position1, Position2, Endpos: Integer;
begin
Description_RE.Clear;

StartPos := 0;
Endpos := Length(Temp_RE.Text);
Temp_RE.Lines.BeginUpdate;
while Temp_RE.FindText(S1, StartPos, Endpos, [stMatchCase])<>-1 do
begin
Endpos := Length(Temp_RE.Text) - StartPos;
Position1 := Temp_RE.FindText(S1, StartPos, Endpos, [stMatchCase]);
Inc(StartPos, Length(S1));
Temp_RE.SetFocus;
Temp_RE.SelStart := Position1;
Temp_RE.SelLength := Length(S1);
end;
Temp_RE.Lines.EndUpdate;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close