Dv::Util::iopstream Class Reference

Start a process with its standard input/output connected to an iostream. More...

#include <iopstream.h>

Inheritance diagram for Dv::Util::iopstream:
Inheritance graph
[legend]
Collaboration diagram for Dv::Util::iopstream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 iopstream (const std::string &command, unsigned int min_debug_level=1) throw (std::runtime_error)
 Create an input/output stream from/to a process running command.
int close () throw (std::runtime_error)
 Close stream.
 ~iopstream () throw (std::runtime_error)
 Destructor.

Private Member Functions

void start () throw (std::runtime_error)
 Start up a new process.

Detailed Description

Start a process with its standard input/output connected to an iostream.

Definition at line 16 of file iopstream.h.


Constructor & Destructor Documentation

Dv::Util::iopstream::iopstream ( const std::string &  command,
unsigned int  min_debug_level = 1 
) throw (std::runtime_error)

Create an input/output stream from/to a process running command.

The process is created using execl with argument

 /bin/sh -c command
 

Data written to the stream will appear on the standard input of the process. The standard output of the process can be read from the stream.

Parameters:
command which will be run using /bin/sh -c command
min_debug_level for when the object is connected to a debug master
See also:
Dv::Debugable
Exceptions:
std::runtime_error if anything goes wrong.
Dv::Util::iopstream::~iopstream (  )  throw (std::runtime_error)

Destructor.

Exceptions:
std::runtime_error if anything goes wrong.

Member Function Documentation

int Dv::Util::iopstream::close (  )  throw (std::runtime_error)

Close stream.

Wait for command to finish, then return its exit status. After a close, the stream is unusable.

Returns:
exit status of command as returned by main().
Warning:
A negative exit status gets converted to a positive number, i.e. -1 becomes 255.
Exceptions:
std::runtime_error if command did not exit normally or any other problem.
void Dv::Util::iopstream::start (  )  throw (std::runtime_error) [private]

Start up a new process.

Returns:
pid iff the process is running
0 in the child process if the process is not running a command
Exceptions:
std::runtime_error if fork() failed.
std::runtime_error if execl() failed.
std::logic_error if the process is already running
  • If the process command is not empty, it will be executed using
       /bin/sh -c exec command
    
    Example:
       Dv::Util::Process proc("ping www.bla.com");
       proc.start();
    
  • If the process command is empty, a fork() will be executed and the function will return false in the child process. This supports code such as the following.
       Dv::Util::Process p;
       if (! p.start())
         return f(...); // in the child
       p.wait(); // in the parent
    
    See also:
    Dv::Util::Process::Process

Reimplemented from Dv::Util::Process.


The documentation for this class was generated from the following file:

dvutil-1.0.10 [ 5 December, 2009]