Page 1 of 1
Properly handle stdin and stdout
Posted: Sun Jan 25, 2015 12:45 am
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..
Re: Properly handle stdin and stdout
Posted: Tue Jan 27, 2015 9:09 am
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.
Re: Properly handle stdin and stdout
Posted: Thu Feb 05, 2015 5:09 am
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$