From 4d0a7466320e54f45f413efef09ef8e6ad21bb3e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Nov 2007 16:02:04 +0000 Subject: Added some helpers to fstring, and fixed a bug in Bu::Process, it wasn't closing the pipes properly, resulting in the child process going defunct and not dying, it also wasn't buffering properly, it now collects as much data as it can before returning from a read operation. --- src/fstring.h | 33 +++++++++++++++++++++++++++++++ src/process.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/process.h | 1 + src/tests/procs.cpp | 6 +++++- 4 files changed, 93 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/fstring.h b/src/fstring.h index 666480c..82e8b34 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -731,6 +731,39 @@ namespace Bu } return -1; } + + /** + * Find the index of the first occurrance of cChar + *@param sText (const chr *) The string to search for. + *@returns (long) The index of the first occurrance. -1 for not found. + */ + long find( long iStart, const chr cChar ) const + { + flatten(); + for( long j = iStart; j < pFirst->nLength; j++ ) + { + if( pFirst->pData[j] == cChar ) + return j; + } + return -1; + } + + /** + * Find the index of the first occurrance of sText + *@param cChar (const chr) The character to search for. + *@returns (long) The index of the first occurrance. -1 for not found. + */ + long find( long iStart, const chr *sText ) const + { + long nTLen = strlen( sText ); + flatten(); + for( long j = iStart; j < pFirst->nLength-nTLen; j++ ) + { + if( !strncmp( sText, pFirst->pData+j, nTLen ) ) + return j; + } + return -1; + } /** * Do a reverse search for (sText) diff --git a/src/process.cpp b/src/process.cpp index 8fe98f3..c554e1a 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -7,8 +7,11 @@ #include "process.h" #include +#include #include #include +#include +#include Bu::Process::Process( const char *sName, char *const argv[] ) { @@ -22,12 +25,17 @@ Bu::Process::Process( const char *sName, const char *argv, ...) va_start( ap, argv ); for(; va_arg( ap, const char *); iCnt++ ); va_end( ap ); + printf("Params: %d\n", iCnt ); char const **list = new char const *[iCnt+2]; va_start( ap, argv ); list[0] = argv; - for( int j = 1; j < iCnt; j++ ) + printf("list[0] = \"%s\"\n", argv ); + for( int j = 1; j <= iCnt; j++ ) + { list[j] = va_arg( ap, const char *); + printf("list[%d] = \"%s\"\n", j, list[j] ); + } list[iCnt+1] = NULL; va_end( ap ); @@ -52,6 +60,8 @@ void Bu::Process::gexec( const char *sName, char *const argv[] ) iStdOut = iaStdOut[0]; iStdErr = iaStdErr[0]; +// fcntl( iStdOut, F_SETFL, fcntl( iStdOut, F_GETFL, 0 )|O_NONBLOCK ); + iPid = fork(); if( iPid == 0 ) { @@ -63,6 +73,9 @@ void Bu::Process::gexec( const char *sName, char *const argv[] ) dup2( iaStdErr[1], 2 ); execvp( sName, argv ); } + ::close( iaStdIn[0] ); + ::close( iaStdOut[1] ); + ::close( iaStdErr[1] ); } void Bu::Process::close() @@ -71,7 +84,46 @@ void Bu::Process::close() size_t Bu::Process::read( void *pBuf, size_t nBytes ) { - return ::read( iStdOut, pBuf, nBytes ); + size_t iTotal = 0; + for(;;) + { + size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal, nBytes-iTotal ); + if( iRet == 0 ) + return iTotal; + printf("--read=%d / %d--\n", iRet, iTotal+iRet ); + iTotal += iRet; + if( iTotal == nBytes ) + return iTotal; + } + /* + size_t iTotal = 0; + fd_set rfs; + FD_ZERO( &rfs ); + for(;;) + { + if( waitpid( iPid, NULL, WNOHANG ) ) + { + printf("!!!wait failed!\n"); + size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal, + nBytes-iTotal ); + iTotal += iRet; + return iTotal; + } + + FD_SET( iStdOut, &rfs ); + select( iStdOut+1, &rfs, NULL, &rfs, NULL ); + size_t iRet = ::read( iStdOut, (char *)pBuf+iTotal, nBytes-iTotal ); + printf("--read=%d / %d--\n", iRet, iTotal+iRet ); + iTotal += iRet; + if( iTotal == nBytes ) + return iTotal; + } + */ +} + +size_t Bu::Process::readErr( void *pBuf, size_t nBytes ) +{ + return ::read( iStdErr, pBuf, nBytes ); } size_t Bu::Process::write( const void *pBuf, size_t nBytes ) diff --git a/src/process.h b/src/process.h index dbf1553..025b295 100644 --- a/src/process.h +++ b/src/process.h @@ -30,6 +30,7 @@ namespace Bu virtual void close(); virtual size_t read( void *pBuf, size_t nBytes ); + virtual size_t readErr( void *pBuf, size_t nBytes ); virtual size_t write( const void *pBuf, size_t nBytes ); virtual long tell(); diff --git a/src/tests/procs.cpp b/src/tests/procs.cpp index 53e5142..c233040 100644 --- a/src/tests/procs.cpp +++ b/src/tests/procs.cpp @@ -4,12 +4,16 @@ int main( int agrc, char *argv[] ) { - Bu::Process p( argv[1], argv+1 ); + //Bu::Process p( argv[1], argv+1 ); + Bu::Process p("mplayer", "mplayer", "dvd://", "-framedrop", + "-ao", "null", "-nosound", "-vf", "framestep=I,cropdetect" "-sstep", + "197", NULL ); char buf[1000]; for(;;) { int iSize = p.read( buf, 1000 ); + printf("::read=%d::\n", iSize ); if( iSize == 0 ) break; fwrite( buf, iSize, 1, stdout ); -- cgit v1.2.3