delphistream

How to cut out a part from a stream in Delphi7


I have an incoming soap message which in in the form of a TStream (Delphi7), server that sends this soap is in development mode and adds a html header to the message for debugging purposes. Now I need to cut out the html header part from it before I can pass it to soap converter. It starts from the beginning with 'pre' tag and ends with '/pre' tag. I'm thinking it should be fairly easy to do, but I haven't done it before in Delphi7, Can someone help me?


Solution

  • I think the following code would do what you want, assuming you only have one <pre> block in your document.

    function DepreStream(Stm : tStream):tStream;
    var
      sTemp : String;
      oStrStm : tStringStream;
      i : integer;
    begin
      oStrStm := tStringStream.create('');
      try
        Stm.Seek(0,soFromBeginning);
        oStrStm.copyfrom(Stm,Stm.Size);
        sTemp := oStrStm.DataString;
        if (Pos('<pre>',sTemp) > 0) and (Pos('</pre>',sTemp) > 0) then
          begin
            delete(sTemp,Pos('<pre>',sTemp),(Pos('</pre>',sTemp)-Pos('<pre>',sTemp))+6);
            oStrStm.free;
            oStrStm := tStringStream.Create(sTemp);
          end;
        Result := tMemoryStream.create;
        oStrStm.Seek(0,soFromBeginning);
        Result.CopyFrom(oStrStm,oStrStm.Size);
        Result.Seek(0,soFromBeginning);
      finally
        oStrStm.free;
      end;
    end;
    

    Another option I believe would be to use an xml transform to remove the unwanted tags, but I don't do much in the way of transforms so if anyone else wants that torch...

    EDIT: Corrected code so that it works. Teaches me for coding directly into SO rather than into the IDE first.