Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

PipeStream Class Reference

Runs an external program, and provides access to the result. More...

#include <PipeStream.h>

Inheritance diagram for PipeStream:

InputByteStream List of all members.

Public Member Functions

 PipeStream (string cmd, string envs="") throw (InputByteStreamError)
 Creates a stream which returns the output of a given command.
 ~PipeStream ()
string getResult (bool allOfFile=false, bool gobbleRest=true) throw (InputByteStreamError)
 Returns the contents of the stream as a string.
virtual void close (void)
 Closes the stream, and reaps the process status.
int getTerminationStatus (void)
 Returns the status of the command at the end of the pipe.
pid_t getPid (void) const
 Returns the PID of the running process.

Detailed Description

Runs an external program, and provides access to the result.

This runs a given command in a forked process, optionally adjusting the environment as it does so. The output from the command is available using the methods of an InputByteStream, plus an additional one which retrieves the entire result as a string.


Constructor & Destructor Documentation

PipeStream::PipeStream string  cmd,
string  envs = ""
throw (InputByteStreamError)
 

Creates a stream which returns the output of a given command.

The constructor open a pipe with the given command, and prepares for the results to be read by this class's accessor methods.

If the second argument is omitted, then a default list of environment variables, namely PATH HOME LOGNAME SHELL TMPDIR, are inherited from the current environment. If present, it should be a space-separated list of environment variables with optional values: if no equal sign is present, the variable's value is inherited from the current environment. For example, if this list is given as in the form env1=val1 env2= env3, then env1 would receive the value val1, env2 would receive a null value, and env3 would be inherited.

If the command exits with a non-zero exit status, this is noted on stderr, but any output from the command is still returned.

If there's no way of doing this on a particular platform, throw InputByteStreamError always.

Parameters:
cmd the command to be run
envs if specified, this is a list of environment variables; if it is an empty string, "" (the default), then a default list of variables is inherited
Exceptions:
InputByteStreamError on any errors

PipeStream::~PipeStream  ) 
 


Member Function Documentation

void PipeStream::close void   )  [virtual]
 

Closes the stream, and reaps the process status.

If the process has not already terminated, this method attempts to kill it, by sending first HUP then KILL. If there is some reason why we can't reap the status -- because it has not died or for, say, some permissions reason -- we set the returned status to -1. Any data not read from the pipe is discarded.

Reimplemented from InputByteStream.

pid_t PipeStream::getPid void   )  const [inline]
 

Returns the PID of the running process.

After the process has finished, this will return zero.

Returns:
the process's PID, or zero if it has finished

string PipeStream::getResult bool  allOfFile = false,
bool  gobbleRest = true
throw (InputByteStreamError)
 

Returns the contents of the stream as a string.

If some of the stream has already been read by other methods of this class or its parent, it cannot be re-read. Any trailing whitespace, including end-of-line characters, is stripped.

It's possible that the returned string will be incomplete, if the process failed somehow; irrespective of whether this happened, this returns what it can, and if this condition matters to you, you should check the termination status with getTerminationStatus.

Parameters:
allOfFile if true, then all of the file is read into the string; if false (the default), only the first line is returned, not including the newline or carriage-return which ends the line
gobbleRest if true (the default) then gobble the rest of the file; has an effect only if allOfFile was false
Returns:
the stream contents (or some of it, depending on the parameter values) as a string
Exceptions:
InputByteStreamError if there is some problem reading the stream

int PipeStream::getTerminationStatus void   ) 
 

Returns the status of the command at the end of the pipe.

Since this is only available after the process has completed, this invokes close on the stream first. Thus you should not invoke this method until after you have extracted all of the command's output that you want. If close is unable to terminate the process for some reason, this returns -1.

Returns:
the exit status of the process.


The documentation for this class was generated from the following files:
Generated on Sun Aug 21 18:21:08 2005 for dvi2bitmap by doxygen 1.3.8