Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with TMemoryStream.CopyFrom

Status
Not open for further replies.

ColinTod

Programmer
Oct 16, 2006
19
NZ
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;
 
Should have pointed out that there is no error.
Simply nothing shows up in the RichEdit box
 
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?
 
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...
 
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;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top