Properly handle stdin and stdout

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

Properly handle stdin and stdout

Post by tj1010 »

http://forum.purebasic.com/english/view ... 13&t=21488

As described there. A LOT of software, most notably the latest build of FFMPEG doesn't write to stdout like PB team says is the right way.. So reading console output from such programs is literally impossible without detecting non-terminated output with a lot of low-level code..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Properly handle stdin and stdout

Post by netmaestro »

PB Doc for ReadProgramString wrote:This function also waits until a full line of output is available. If not line-based or raw output is to be read, ReadProgramData() may be used.
Freak also replied in that thread: "To prevent this, ReadProgramData() can be used." quoting directly from the doc.

Use ReadProgramData() as directed by the teammember and you won't have problems. Only use ReadProgramString() in cases where you know for sure the data is coming as null-terminated strings. If there is any doubt at all, use ReadProgramData().

Imho, ReadProgramString() should be considered for deprecation because it causes as many problems as it solves.
BERESHEIT
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

Re: Properly handle stdin and stdout

Post by tj1010 »

netmaestro wrote:
PB Doc for ReadProgramString wrote:This function also waits until a full line of output is available. If not line-based or raw output is to be read, ReadProgramData() may be used.
Freak also replied in that thread: "To prevent this, ReadProgramData() can be used." quoting directly from the doc.

Use ReadProgramData() as directed by the teammember and you won't have problems. Only use ReadProgramString() in cases where you know for sure the data is coming as null-terminated strings. If there is any doubt at all, use ReadProgramData().

Imho, ReadProgramString() should be considered for deprecation because it causes as many problems as it solves.

ReadProgramData with PeekS both ASCII and UTF-8 don't work with some programs like FFMPEG.


snippets from a project

Code: Select all

prog=RunProgram("ffmpeg\bin\ffmpeg.exe","-ss "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(7)))+" -t "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(8)))+" -i "+Chr(34)+file$+Chr(34)+" -s "+GetGadgetText(5)+"x"+GetGadgetText(6)+" "+Chr(34)+local$+".gif"+Chr(34),GetCurrentDirectory(),#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
While ProgramRunning(prog)
  If AvailableProgramOutput(prog)
    *fff=AllocateMemory(2000)
    ReadProgramData(prog,*fff,2000)
    aaa$=aaa$+PeekS(*fff,2000,#PB_Unicode)
    FreeMemory(*fff)
  EndIf
Wend
Debug aaa$

Code: Select all

prog=RunProgram("ffmpeg\bin\ffmpeg.exe","-ss "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(7)))+" -t "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(8)))+" -i "+Chr(34)+file$+Chr(34)+" -s "+GetGadgetText(5)+"x"+GetGadgetText(6)+" "+Chr(34)+local$+".gif"+Chr(34),GetCurrentDirectory(),#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
While ProgramRunning(prog)
  If AvailableProgramOutput(prog)
    *fff=AllocateMemory(2000)
    ReadProgramData(prog,*fff,2000)
    aaa$=aaa$+PeekS(*fff,2000,#PB_Ascii)
    FreeMemory(*fff)
  EndIf
Wend
Debug aaa$

Code: Select all

prog=RunProgram("ffmpeg\bin\ffmpeg.exe","-ss "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(7)))+" -t "+FormatDate("%hh:%ii:%ss",Val(GetGadgetText(8)))+" -i "+Chr(34)+file$+Chr(34)+" -s "+GetGadgetText(5)+"x"+GetGadgetText(6)+" "+Chr(34)+local$+".gif"+Chr(34),GetCurrentDirectory(),#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
While ProgramRunning(prog)
  If AvailableProgramOutput(prog)
    *fff=AllocateMemory(2000)
    ReadProgramData(prog,*fff,2000)
    aaa$=aaa$+PeekS(*fff,2000,#PB_UTF8)
    FreeMemory(*fff)
  EndIf
Wend
Debug aaa$
Post Reply