📄 vk_process.cpp
字号:
QString::null if canReadLineStderr() returns false. By default, the text is interpreted to be in Latin-1 encoding. If you need other codecs, you can set a different codec with QTextCodec::setCodecForCStrings(). */ QString VKProcess::readLineStderr() { QByteArray a( 256 ); VKMembuf *buf = membufStderr(); if ( !buf->scanNewline( &a ) ) { if ( !canReadLineStderr() ) return QString::null; if ( !buf->scanNewline( &a ) ) return QString( buf->readAll() ); } uint size = a.size(); buf->consumeBytes( size, 0 ); // get rid of terminating \n or \r\n if ( size>0 && a.at( size - 1 ) == '\n' ) { if ( size>1 && a.at( size - 2 ) == '\r' ) a.at( size - 2 ) = '\0'; else a.at( size - 1 ) = '\0'; } return QString( a ); } /* void VKProcess::launchFinished() This signal is emitted when the process was started with launch(). If the start was successful, this signal is emitted after all the data has been written to standard input. If the start failed, then this signal is emitted immediately. This signal is especially useful if you want to know when you can safely delete the VKProcess object when you are not interested in reading from standard output or standard error. */ /* Runs the process and writes the data buf to the process's standard input. If all the data is written to standard input, standard input is closed. The command is searched for in the path for executable programs; you can also use an absolute path in the command itself. If env is null, then the process is started with the same environment as the starting process. If env is non-null, then the values in the string list are interpreted as environment setttings of the form {key=value} and the process is started with these environment settings. For convenience, there is a small exception to this rule under Unix: if env does not contain any settings for the environment variable LD_LIBRARY_PATH, then this variable is inherited from the starting process. Returns true if the process could be started; otherwise returns false. Note that you should not use the slots writeToStdin() and closeStdin() on processes started with launch(), since the result is not well-defined. If you need these slots, use start() instead. The process may or may not read the buf data sent to its standard input. You can call this function even when a process that was started with this instance is still running. Be aware that if you do this the standard input of the process that was launched first will be closed, with any pending data being deleted, and the process will be left to run out of your control. Similarly, if the process could not be started the standard input will be closed and the pending data deleted. (On operating systems that have zombie processes, Qt will also wait() on the old process.) The object emits the signal launchFinished() when this function call is finished. If the start was successful, this signal is emitted after all the data has been written to standard input. If the start failed, then this signal is emitted immediately. */ bool VKProcess::launch( const QByteArray& buf, QStringList *env ) { if ( start( env ) ) { if ( !buf.isEmpty() ) { connect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); writeToStdin( buf ); } else { closeStdin(); emit launchFinished(); } return true; } else { emit launchFinished(); return false; } } /* The data buf is written to standard input with writeToStdin() using the QString::local8Bit() representation of the strings. */ bool VKProcess::launch( const QString& buf, QStringList *env ) { if ( start( env ) ) { if ( !buf.isEmpty() ) { connect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); writeToStdin( buf ); } else { closeStdin(); emit launchFinished(); } return true; } else { emit launchFinished(); return false; } } /* Private slot - used by the launch() functions to close standard input. */ void VKProcess::closeStdinLaunch() { disconnect( this, SIGNAL(wroteToStdin()), this, SLOT(closeStdinLaunch()) ); closeStdin(); emit launchFinished(); } /* void VKProcess::readyReadStdout() This signal is emitted when the process has written data to standard output. You can read the data with readStdout(). Note that this signal is only emitted when there is new data and not when there is old, but unread data. In the slot connected to this signal, you should always read everything that is available at that moment to make sure that you don't lose any data. */ /* void VKProcess::readyReadStderr() This signal is emitted when the process has written data to standard error. You can read the data with readStderr(). Note that this signal is only emitted when there is new data and not when there is old, but unread data. In the slot connected to this signal, you should always read everything that is available at that moment to make sure that you don't lose any data. */ /* void VKProcess::processExited() This signal is emitted when the process has exited. */ /* void VKProcess::wroteToStdin() This signal is emitted if the data sent to standard input (via writeToStdin()) was actually written to the process. This does not imply that the process really read the data, since this class only detects when it was able to write the data to the operating system. But it is now safe to close standard input without losing pending data. */ /* The string buf is handled as text using the QString::local8Bit() representation. */ void VKProcess::writeToFDin( const QString& buf ) { QByteArray tmp = buf.local8Bit(); tmp.resize( buf.length() ); writeToFDin( tmp ); } /* The string buf is handled as text using the QString::local8Bit() representation. */ void VKProcess::writeToStdin( const QString& buf ) { QByteArray tmp = buf.local8Bit(); tmp.resize( buf.length() ); writeToStdin( tmp ); } /* Under Windows the implementation is not so nice: it is not that easy to detect when one of the signals should be emitted; therefore there are some timers that query the information. To keep it a little efficient, use the timers only when they are needed. They are needed, if you are interested in the signals. So use connectNotify() and disconnectNotify() to keep track of your interest. */ void VKProcess::connectNotify( const char * signal ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::connectNotify(): signal %s has been connected", signal );#endif if ( !ioRedirection ) if ( qstrcmp( signal, SIGNAL(readyReadFDout()) )==0 || qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 || qstrcmp( signal, SIGNAL(readyReadStderr()) )==0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::connectNotify(): set ioRedirection to true" );#endif setIoRedirection( true ); return; } if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::connectNotify(): set notifyOnExit to true" );#endif setNotifyOnExit( true ); return; } if ( !wroteToFDinConnected && qstrcmp( signal, SIGNAL(wroteToFDin()) )==0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::connectNotify(): set wroteToFDinConnected to true" );#endif setWroteFDinConnected( true ); return; } if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::connectNotify(): set wroteToStdinConnected to true" );#endif setWroteStdinConnected( true ); return; } } void VKProcess::disconnectNotify( const char * ) { if ( ioRedirection && receivers( SIGNAL(readyReadFDout()) ) ==0 && receivers( SIGNAL(readyReadStdout()) ) ==0 && receivers( SIGNAL(readyReadStderr()) ) ==0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::disconnectNotify(): set ioRedirection to false" );#endif setIoRedirection( false ); } if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::disconnectNotify(): set notifyOnExit to false" );#endif setNotifyOnExit( false ); } if ( wroteToFDinConnected && receivers( SIGNAL(wroteToFDin()) ) == 0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::disconnectNotify(): set wroteToFDinConnected to false" );#endif setWroteFDinConnected( false ); } if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) {#if defined(VK_PROCESS_DEBUG) qDebug( "VKProcess::disconnectNotify(): set wroteToStdinConnected to false" );#endif setWroteStdinConnected( false ); } } VKMembuf* VKProcess::membufFDout() { if ( d->proc && d->proc->socketFDout ) { /* Apparently, there is not consistency among different operating systems on how to use FIONREAD. FreeBSD, Linux and Solaris all expect the 3rd argument to ioctl() to be an int, which is normally 32-bit even on 64-bit machines. IRIX, on the other hand, expects a size_t, which is 64-bit on 64-bit machines. So, the solution is to use size_t initialized to zero to make sure all bits are set to zero, preventing underflow with the FreeBSD/Linux/Solaris ioctls. */ size_t nbytes = 0; if ( ::ioctl(d->proc->socketFDout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) socketRead( d->proc->socketFDout ); } return &d->bufFDout; } VKMembuf* VKProcess::membufStdout() { if ( d->proc && d->proc->socketStdout ) { /* Apparently, there is not consistency among different operating systems on how to use FIONREAD. FreeBSD, Linux and Solaris all expect the 3rd argument to ioctl() to be an int, which is normally 32-bit even on 64-bit machines. IRIX, on the other hand, expects a size_t, which is 64-bit on 64-bit machines. So, the solution is to use size_t initialized to zero to make sure all bits are set to zero, preventing underflow with the FreeBSD/Linux/Solaris ioctls. */ size_t nbytes = 0; if ( ::ioctl(d->proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) socketRead( d->proc->socketStdout ); } return &d->bufStdout; } VKMembuf* VKProcess::membufStderr() { if ( d->proc && d->proc->socketStderr ) { /* Apparently, there is not consistency among different operating systems on how to use FIONREAD. FreeBSD, Linux and Solaris all expect the 3rd argument to ioctl() to be an int, which is normally 32-bit even on 64-bit machines. IRIX, on the other hand, expects a size_t, which is 64-bit on 64-bit machines. So, the solution is to use size_t initialized to zero to make sure all bits are set to zero, preventing underflow with the FreeBSD/Linux/Solaris ioctls. */ size_t nbytes = 0; if ( ::ioctl(d->proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) socketRead( d->proc->socketStderr ); } return &d->bufStderr; } /* Destroys the instance. If the process is running, it is <b>not</b> terminated! The standard input, standard output and standard error of the process are closed. You can connect the destroyed() signal to the kill() slot, if you want the process to be terminated automatically when the instance is destroyed. */ VKProcess::~VKProcess() { delete d; } /* Tries to run a process for the command and arguments that were specified with setArguments(), addArgument() or that were specified in the constructor. The command is searched for in the path for executable programs; you can also use an absolute path in the command itself. If env is null, then the process is started with the same environment as the starting process. If env is non-null, then the values in the stringlist are interpreted as environment setttings of the form {key=value} and the process is started in these environment settings. For convenience, there is a small exception to this rule: under Unix, if env does not contain any settings for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -