<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 21 March 2015 at 11:35, Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On <a href="tel:21.03.2015%2011" value="+12103201511" target="_blank">21.03.2015 11</a>:13, vfclists . wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<br>
<br>
On 20 March 2015 at 20:54, Sven Barth <<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a><br></span><span class="">
<mailto:<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@<u></u>googlemail.com</a>>> wrote:<br>
<br>
    On 20.03.2015 21:18, vfclists . wrote:<br>
<br>
<br>
<br>
        On 20 March 2015 at 19:34, Sven Barth<br>
        <<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a> <mailto:<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@<u></u>googlemail.com</a>><br></span>
        <mailto:<a href="mailto:pascaldragon@" target="_blank">pascaldragon@</a>__<a href="http://googlemail.com" target="_blank">googlem<u></u>ail.com</a><span class=""><br>
        <mailto:<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@<u></u>googlemail.com</a>>>> wrote:<br>
<br>
             Am 20.03.2015 19:19 schrieb "vfclists ."<br>
        <<a href="mailto:vfclists@gmail.com" target="_blank">vfclists@gmail.com</a> <mailto:<a href="mailto:vfclists@gmail.com" target="_blank">vfclists@gmail.com</a>><br></span>
             <mailto:<a href="mailto:vfclists@gmail.com" target="_blank">vfclists@gmail.com</a> <mailto:<a href="mailto:vfclists@gmail.com" target="_blank">vfclists@gmail.com</a>>>>:<div><div class="h5"><br>
<br>
<br>
snip<br>
<br>
        How do you ensure own implementation overrides the system's<br>
        implementation, does the compiler take care of that<br>
        automatically, or<br>
        will you have to name your function differently?<br>
<br>
<br>
    There is no need to ensure that. Here is an example:<br>
<br>
    === code begin ===<br>
<br>
    var<br>
       f, oldout: TextFile;<br>
    begin<br>
       Writeln('Hello Output as StdOut');<br>
<br>
       oldout := Output;<br>
<br>
       Assign(Output, 'test.txt');<br>
       Rewrite(Output);<br>
<br>
       Writeln('Hello Output as file');<br>
<br>
       Close(f);<br>
<br>
       Output := oldout;<br>
<br>
       Writeln('Hello Output as StdOut again');<br>
    end.<br>
<br>
    === code end ===<br>
<br>
    To see how such a TextFile is implemented you can take a look at<br>
    unit StreamIO which is part of FPC's units.<br>
<br>
    (Though I wonder why "Assign(f, 'test.txt'); Output := f;<br>
    Writeln('Hello Output as file');" does not work :/ )<br>
<br>
    Regards,<br>
    Sven<br>
<br></div></div>
    ______________________________<u></u>___________________<br>
    fpc-pascal maillist  - fpc-pascal@lists.freepascal.__<u></u>org<br>
    <mailto:<a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.<u></u>freepascal.org</a>><br>
    <a href="http://lists.freepascal.org/__cgi-bin/mailman/listinfo/fpc-__pascal" target="_blank">http://lists.freepascal.org/__<u></u>cgi-bin/mailman/listinfo/fpc-_<u></u>_pascal</a><span class=""><br>
    <<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/<u></u>cgi-bin/mailman/listinfo/fpc-<u></u>pascal</a>><br>
<br>
<br>
I need to get the output of a program which uses a lot of Write and<br>
Writeln commands into the GUI in realtime, by that I not having to<br>
output it to a text file and reading it afterwards, but by capturing the<br>
output of each Write command into a variable and displaying it in the<br>
GUI immediately.<br>
<br>
If each Write or Writeln could trigger an event, I could use the event<br>
to capture the output. My other option is to replace the calls to write<br>
with my own function, but Write has different number of call parameters<br>
and it may require as many variants of the function as are used in the<br>
program, assuming that the call syntax is regular, not something like<br>
this one - write(JSValToDouble(cx,pom^)):<u></u>1:scale).<br>
</span></blockquote>
<br>
The usage of a text file was merely an example. As I said there already is the possibility to use a TStream provided by FPC. But since I'm nice here you also have an example for a TMemo, I'm sure you can adjust that for your needs:<br>
<br>
=== code begin ===<br>
<br>
resourcestring<br>
  SErrNilMemo = 'Memo is nil';<br>
<br>
type<br>
  PMemo = ^TMemo;<br>
<br>
function GetMemo(var F: TTextRec): TMemo;<br>
begin<br>
  Result:=PMemo(@F.Userdata)^;<br>
end;<br>
<br>
function MemoWrite(var F: TTextRec): LongInt;<br>
var<br>
  s: String;<br>
begin<br>
  Result := 0;<br>
  with F do<br>
    if BufPos > 0 then<br>
      try<br>
        SetLength(s, BufPos);<br>
        Move(BufPtr^, s[1], BufPos);<br>
        GetMemo(F).SelText := s;<br>
        BufPos:=0;<br>
      except<br>
        Result:=101;<br>
      end;<br>
end;<br>
<br>
<br>
function MemoClose(var F: TTextRec): LongInt;<br>
begin<br>
  Result := 0;<br>
end;<br>
<br>
function MemoOpen(var F: TTextRec): LongInt;<br>
begin<br>
  Result := 0;<br>
  with F do begin<br>
    BufPos:=0;<br>
    Bufend:=0;<br>
    case Mode of<br>
      fmInput: begin<br>
        Result := 104;<br>
      end;<br>
      fmOutput, fmAppend: begin<br>
        InOutFunc := @MemoWrite;<br>
        FlushFunc := @MemoWrite;<br>
        if Mode = fmAppend then<br>
          Try<br>
            with GetMemo(F) do begin<br>
              SelStart := Length(Text);<br>
            end;<br>
          except<br>
            Result := 156;<br>
          end;<br>
        end else begin<br>
          GetMemo(F).Clear;<br>
        end;<br>
    end;<br>
    end;<br>
end;<br>
<br>
procedure AssignMemo(var F: Text; aMemo: TMemo);<br>
var<br>
  e: EInoutError;<br>
begin<br>
  if not Assigned(aMemo) then begin<br>
    E:=EInOutError.Create(<u></u>SErrNilMemo);<br>
    E.ErrorCode:=6;<br>
    Raise E;<br>
  end;<br>
  with TTextRec(F) do begin<br>
    OpenFunc := @MemoOpen;<br>
    CloseFunc := @MemoClose;<br>
    case DefaultTextLineBreakStyle Of<br>
      tlbsLF:<br>
        TextRec(f).LineEnd := #10;<br>
      tlbsCRLF:<br>
        TextRec(f).LineEnd := #13#10;<br>
      tlbsCR:<br>
        TextRec(f).LineEnd := #13;<br>
    end;<br>
    PMemo(@UserData)^ := aMemo;<br>
    Mode := fmClosed;<br>
    BufSize := SizeOf(Buffer);<br>
    BufPtr := @Buffer;<br>
    Name[0] := #0;<br>
  end;<br>
end;<br>
<br>
=== code end ===<br>
<br>
To use it you should use the following code (for example in FormCreate):<br>
<br>
=== code begin ===<br>
<br>
  fOldOutput := Output; // store the old Output somewhere<br>
  AssignMemo(Output, Memo1);<br>
  Rewrite(Output);<br>
<br>
=== code end ===<br>
<br>
Then in FormDestroy you cleanup:<br>
<br>
=== code begin ===<br>
<br>
  CloseFile(Output);<br>
  Output := fOldOutput; // restore old Output<br>
<br>
=== code end ===<br>
<br>
And to illustrate that it works I've used a TTimer and added the following to its OnTimer event:<br>
<br>
=== code begin ===<br>
<br>
  Writeln('Hello World ', fIndex);<br>
  Inc(fIndex); // fIndex is a LongInt field of the form<br>
<br>
=== code end ===<br>
<br>
And with that TMemo gets spammed with 'Hello World N' messages ;)<br>
<br>
You could of course also implement it in a way that you assign a event handler to a text file instead of a memo. As I said the Pascal I/O mechanism is very flexible here.<br>
<br>
And just in case: by using this mechanism you don't need to adjust any of the Write(Ln)s, the full spectrum of variants of how Write(Ln) can be called is supported.<div class="HOEnZb"><div class="h5"><br>
<br>
Regards,<br>
Sven<br>
______________________________<u></u>_________________<br>
fpc-pascal maillist  -  <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.<u></u>org</a><br>
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/<u></u>cgi-bin/mailman/listinfo/fpc-<u></u>pascal</a><br>
</div></div></blockquote></div><br><br clear="all"></div><div class="gmail_extra">Much obliged.<br><br></div><div class="gmail_extra">Is the Text object the Write is using global for the whole program or thread, ie all Writes will go to that Text object until it is reverted back to the default?<br><br></div><div class="gmail_extra">In case of wanting to use different Text objects is it possible to prefix the Write command with the Text e.g Write(TextObject, 'outputtext')? Do Write and WriteLn support something like that?<br></div><div class="gmail_extra"><br><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">-- <br><div class="gmail_signature">Frank Church<br><br>=======================<br><a href="http://devblog.brahmancreations.com">http://devblog.brahmancreations.com</a></div>
</div></div>