The libexecstream library

Overview

Libexecstream is a C++ library that allows you to run a child process and have its input, output and error avaliable as standard C++ streams.

Like this:

#include <exec-stream.h>
#include <string>
...
try {
    exec_stream_t es( "perl", "" ); // run perl without any arguments 
    es.in() << "print \"hello world\";"; // and make it print "hello world" 
    es.close_in();                        // after the input was closed 
    std::string hello, world;
    es.out() >> hello; // read the first word of output 
    es.out() >> world; // read the second word 
}catch( std::exception const & e ) {
    std::cerr << "error: "  <<  e.what()  <<  "\n";
}

Features:

  • Works on Linux and Windows
  • Uses threads
  • Does not depend on any other non-standard library
  • Distributed as source code only, requires you to compile and link one file into your program
  • BSD-style license

Another example:

#include <exec-stream.h>
...
exec_stream_t es;
try {
    // run command to print network configuration, depending on the operating system
    #ifdef _WIN32
        es.start( "ipconfig", "/all" );
    #else
        es.start( "ifconfig", "-a" );
    #endif
    
    std::string s;
    while( std::getline( es.out(), s ).good() ) {
        // do something with s
    }
}catch( std::exception const & e ) {
    std::cerr << "error: "  <<  e.what()  <<  "\n";
}

For more examples see the file test/exec-stream-test.cpp in the source distribution. The interface provided by the library is documented in the reference.

Download

Installation

Libexecstream is provided in source code form only. In order to use it, you need to compile and link one file, exec-stream.cpp, into your program.

On Linux, libexecstream was tested on Red Hat 9 with gcc compiler. Versions of gcc prior to 3.0 will not work. Make sure that exec-stream.h is found somewhere on the include path, compile exec-stream.cpp as usual, link your program with -lpthread. GCC must be configured with --enable-threads, which is by default on most Linux distributions.

On Windows, libexecstream was tested on XP and 95 flavors with VC++ 7 compiler. VC++ 6 will not work. Make sure that exec-stream.h is found somewhere on the include path, compile exec-stream.cpp as usual, link you program with multi-threaded runtime.

Example makefiles for Windows and Linux (used to build the testsute) are provided in the test directory of the source distribution.

The exec-stream.cpp file includes several platform-dependent implementation files. Selection of platform-specific implementation is done at compile time: when _WIN32 macro is defined (usually by windows compiler) win32 implementation is included, when that macro is not defined, posix implementation is included.

Header file exec-stream.h defines interface of the library and uses only standard C++. It does not include any platform-specific header files.