#include <QChildProcess.h>
Inheritance diagram for FX::QChildProcess:
This handy class allows one process to portably communicate with a child process using the standard Unix stdin
, stdout
and stderr
character streams. This can be useful for invoking compilers and other non-TnFOX command-line based programs.
QChildProcess is a synchronous i/o device like FX:QBlkSocket - in other words, reads from it block until data is available. To avoid this, check atEnd() before reading - however generally, you want it to block because you will have allocated a special thread whose sole purpose is to wait on incoming data and asynchronously do something useful with it. One action could be to post a message to some control in the GUI for example. You should note that if you don't empty the output stream from the child process regularly, most operating systems will suspend the child thread until you do.
The two readable streams, stdout
and stderr
, are read simultaneously and stored internally by QChildProcess. This allows you to read say only stdout
and the stderr
pipe won't get blocked up. On close() (or destruction), the calling thread blocks until the child process exits. The child process will be forcibly terminated if the parent process should die for any reason before close() - this ensures zombie child processes don't multiply. If you want to remove the child from management, call detach().
If a child process should hang, you can wait for a certain time period for it to produce output or to exit and if not then invoke terminate().
To use is pretty easy - simply construct one and set the path to the command you desire. Calling open() invokes the process, and thereafter readBlock() and writeBlock() work as expected. Calling close() will wait for the process to terminate before returning. Should the process exit, readBlock() will start returning zero bytes instead of blocking for more data to arrive.
Unix programs output two streams: stdout
and stderr
. You can switch between which is active, or whether to merge them, as the read channel using setReadChannel().
Definition at line 75 of file QChildProcess.h.
Public Types | |
StdOut | |
StdErr | |
Combined | |
enum | ReadChannel { StdOut, StdErr, Combined } |
typedef FXfval | Offset |
Default | |
Unix | |
MacOS | |
MSDOS | |
NoTranslation | |
UTF8 | |
UTF16 | |
UTF16LE | |
UTF32 | |
UTF32LE | |
enum | CRLFType { Default, Unix, MacOS, MSDOS } |
enum | UnicodeType { NoTranslation, UTF8, UTF16, UTF16LE, UTF32, UTF32LE } |
Public Member Functions | |
QChildProcess () | |
QChildProcess (const FXString &command, const FXString &args=FXString::nullStr(), ReadChannel channel=Combined) | |
~QChildProcess () | |
const FXString & | command () const |
void | setCommand (const FXString &command) |
FXString | arguments () const |
void | setArguments (const FXString &args) |
FXString | workingDir () const |
void | setWorkingDir (const FXString &dir) |
ReadChannel | readChannel () const throw () |
void | setReadChannel (ReadChannel channel) |
FXlong | returnCode () const throw () |
void | detach () |
bool | waitForExit (FXuint waitfor=FXINFINITE) |
bool | terminate () |
bool | open (FXuint mode=IO_ReadWrite) |
void | close () |
void | flush () |
FXfval | size () const |
FXuval | readBlock (char *data, FXuval maxlen) |
FXuval | writeBlock (const char *data, FXuval maxlen) |
int | ungetch (int) |
virtual bool | isSynchronous () const |
virtual void | truncate (FXfval) |
virtual FXfval | at () const |
virtual bool | at (FXfval) |
virtual bool | atEnd () const |
virtual FXuval | readBlockFrom (char *data, FXuval maxlen, FXfval pos) |
FXuval | readBlockFrom (FXuchar *data, FXuval maxlen, FXfval pos) |
virtual FXuval | writeBlockTo (FXfval pos, const char *data, FXuval maxlen) |
FXuval | writeBlockTo (FXfval pos, const FXuchar *data, FXuval maxlen) |
FXuint | flags () const |
FXuint | mode () const |
FXuint | state () const |
CRLFType | crlfFormat () const |
void | setCRLFFormat (CRLFType type) |
UnicodeType | unicodeTranslation () const |
void | setUnicodeTranslation (UnicodeType type) |
bool | isBuffered () const |
bool | isRaw () const |
bool | isTranslated () const |
bool | isUTF16Translated () const |
bool | isUTF32Translated () const |
bool | isReadable () const |
bool | isWriteable () const |
bool | isWritable () const |
bool | isReadWrite () const |
bool | isClosed () const |
bool | isInactive () const |
bool | isOpen () const |
virtual const FXACL & | permissions () const |
virtual void | setPermissions (const FXACL &) |
FXuval | readBlock (FXuchar *data, FXuval maxlen) |
FXuval | writeBlock (const FXuchar *data, FXuval maxlen) |
virtual FXuval | readLine (char *data, FXuval maxlen) |
virtual int | getch () |
virtual int | putch (int c) |
FXfval | shredData (FXfval offset, FXfval len=(FXfval)-1) |
Static Public Member Functions | |
static bool | waitForData (QIODeviceS **signalled, FXuint no, QIODeviceS **list, FXuint waitfor=FXINFINITE) |
static FXuint | waitForDataMax () throw () |
static UnicodeType | determineUnicodeType (FXuchar *data, FXuval len) throw () |
static FXuval | applyCRLF (FXuchar *FXRESTRICT output, const FXuchar *FXRESTRICT input, FXuval outputlen, FXuval &inputlen, CRLFType crlftype=Default, UnicodeType utftype=NoTranslation) |
static FXuval | removeCRLF (FXuchar *FXRESTRICT output, const FXuchar *FXRESTRICT input, FXuval outputlen, FXuval &inputlen, UnicodeType utftype=NoTranslation) |
Protected Member Functions | |
void | setFlags (int f) |
void | setMode (int m) |
void | setState (int s) |
Protected Attributes | |
FXfval | ioIndex |
Friends | |
FXAPI FXStream & | operator<< (FXStream &s, QIODevice &i) |
FXAPI FXStream & | operator>> (FXStream &s, QIODevice &i) |