Page 1 of 1

SaveTextToStreamW - string too long

Posted: Fri May 02, 2014 7:03 pm
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;

Posted: Thu May 08, 2014 11:09 am
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]);

Posted: Fri May 09, 2014 2:53 pm
by msimmons15
Thanks Sergey!