mr-edd.co.uk :: horsing around with the C++ programming language

pexl

On the odd occasion, I've found that I've wanted to capture the output of a particular program. The way you do this natively on UNIX and Windows is very different and in each case, it's bloody fiddly.

So I set out to create a library with a platform neutral interface that allows the creation of external processes and interactions with them.

The latest tag in the mercurial repository is 0.1.0, which is stable but only contains code for UNIX. Subsequent revisions contain experimental Windows code.

Recent activity

Code

Clone the repository using mercurial:

> hg clone http://bitbucket.org/edd/pexl

Or get a zip file of the code.

Quick start

To launch a process in the working directory "." and forward its stdout to std::cout:

#include <pexl/exec.hpp>
#include <iostream>

int main()
{
    const char *args[] = { "ls", "-al" }; // $ ls -al
    return pexl::exec(args, args + 2, ".", pexl::out = std::cout);
}

Perhaps we want to capture the errors in a string. We can forward the child process' stderr to a stringstream:

std::list<std::string> args;
args.push_back("find");
args.push_back("/");
args.push_back("-name");
args.push_back("*.txt");  // $ find / -name *.txt

std::ostringstream errors;
int return_code = pexl::exec(args.begin(), args.end(), ".", 
                             pexl::out = std::cout, pexl::err = errors);

Or perhaps we'd like to feed the child's stdin the contents of an std::ifstream:

using namespace pexl;
std::ifstream childinput("childinput.txt");
int return_code = exec(args.begin(), args.end(), ".", 
                       in = childinput, out = std::cout);

To make interaction with existing code easier, pexl can interact with functors as well as C++ streams, meaning you won't have to write your own streambuffer to read/write data from/to a child process.

Further reading

Comments

Nadav

[09/08/2008 at 12:57:00]

Cool dude. I will look into it.

Andreone

[25/08/2009 at 10:11:00]

Hello,

This sounds very promising! But I’m coding for Windows, so I can’t use it ... yet? :)
Do you have any news on the Windows port?

Edd

[05/09/2009 at 13:22:00]

Hi Andreone. There is a Windows port on it’s way, but doing this on Windows is a little trickier, so it’s taking some time!

alfC

[14/03/2010 at 10:23:18]

it seems to be very similar to pstreams http://pstreams.sourceforge.net/. Is that so?

Edd

[14/03/2010 at 11:00:19]

I hadn't seen that one, thanks.

Yes they are certainly in the same ball park. The key differences are:

- licensing (Boost Software License vs LGPL)
- Windows support (coming "soon" to pexl :)
- I also plan to add multiplexing support to pexl (it's already in the preliminary Windows code), allowing you to interact with multiple processes in a single thread.

(optional)
(optional)
(required, hint)

Links can be added like [this one -> http://www.mr-edd.co.uk], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.