SaveTextToStreamW - string too long

General TRichView support forum. Please post your questions here
Post Reply
msimmons15
Posts: 29
Joined: Mon Oct 25, 2010 3:59 pm

SaveTextToStreamW - string too long

Post by msimmons15 »

I have a firebird blob field field (memo) that contains uncompressed rich text. I am extracting just the plain text using the function below. My basic problem is that the text being stored in the SaveTextToStreamW method is double what it should be.

Just to compare results, I also called SaveTextW. This writes the correct string to the file.

The function I am using is below. The RVE1 component is TRichViewEdit.

Am I doing something wrong here?

Thanks
Mike Simmons

Code: Select all

FUNCTION TFindGeneration.GetKPADesc(seq: INTEGER): STRING;
var
 S: string;
 TempStream: TRVMemoryStream;
BEGIN
   Result := '';
   s := '';
   RVE1.Clear;
   IF NOT IBQuery2.active THEN
      Exit;

   TempStream := TRVMemoryStream.Create;
   Try
   IF IBQuery2.Locate('id_sequence', VarArrayOf([seq]), []) THEN
      BEGIN
       IF not IBQuery2.FieldByName('AbbrevText').IsNULL
          THEN BEGIN
               IBQuery2AbbrevText.SaveToStream(TempStream);
               END
          ELSE BEGIN
               IBQuery2Text.SaveToStream(TempStream);
               END;
      TempStream.Position := 0;      // reset to the beginning of the stream
      RVE1.LoadFromStream( TempStream, rvynaAuto );
      RVE1.Format;
      RVE1.SaveTextW('c:\temp\texttest.txt', 0);
      TempStream.Clear;
      RVE1.SaveTextToStreamW('', TempStream, 0, false, True);
      TempStream.Position := 0;
      SetString(s,PChar(TempStream.memory),TempStream.Size);

      S := StringReplace(S, #13, '', [rfReplaceAll]);
      S := StringReplace(S, #10, '', [rfReplaceAll]);
      Result := S;
      END;
   Finally
    TempStream.Free;
   End;
END;
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

The wrong line is

Code: Select all

SetString(s,PChar(TempStream.memory),TempStream.Size);

TempStream.Size returns text size in bytes, while SetString needs length in characters. For Unicode text, this line must be

Code: Select all

SetString(s,PChar(TempStream.memory),TempStream.Size [color=red]div 2[/color]);
msimmons15
Posts: 29
Joined: Mon Oct 25, 2010 3:59 pm

Post by msimmons15 »

Thanks Sergey!
Post Reply