Skip to content

polysquare/cpp-subprocess

Repository files navigation

CPP Subprocess

Some C++ subprocess handling classes.

Status

Build Coverage

Finding a binary in $PATH

#include <string>
#include <vector>

#include <boost/algorithm/string.hpp>

#include <cpp-subprocess/locatebinary.h>
#include <cpp-subprocess/operating_system.h>

#include <cstdlib>

namespace ps = polysquare::subprocess;

char *paths = ::getenv ("PATH");

std::vector <std::string> paths;
boost::split (paths, boost::is_any_of (";"), boost::token_compress_on);

std::string binary;

try
{
    binary = ps::locateBinary ("echo", paths, ps::MakeOperatingSystem ());
}
catch (std::runtime_error const &error)
{
    std::cerr << error.what ();
}

Launching a new binary asynchronously and capturing its input

#include <cpp-subprocess/launch.h>
#include <cpp-subprocess/operating_system.h>

namespace ps = polysquare::subprocess;

int stderr[2], stdout[2];

pipe (stderr);
pipe (stdout);

pid_t pid = 0;
try
{
    char const **argv = {
        "hello",
        nullptr
    };
    pid = ps::launchBinary ("/usr/bin/echo"
                            argv,
                            environ,
                            stderr[1],
                            stdout[1],
                            ps::MakeOperatingSystem ());

}
catch (std::exception const &error)
{
    std::cerr << error.what ();
};

stderr[0] and stdout[0] can be used with read to read the output of the subprocess.

Waiting for an asynchronously launched binary to finish

#include <cpp-subprocess/launch.h>

try
{
    ps::waitForProcessEnd (pid, ps::MakeOperatingSystem ());
}
catch (std::runtime_error const &error)
{
    std::cerr << error.what ();
}

Launching a subprocess synchronously

#include <cpp-subprocess/launch.h>
#include <cpp-subprocess/operating_system.h>

namespace ps = polysquare::subprocess;

int stderr[2], stdout[2];

pipe (stderr);
pipe (stdout);

pid_t pid = 0;
try
{
    char const **argv = {
        "hello",
        nullptr
    };
    pid = ps::launchBinaryAndWaitForReturn ("/usr/bin/echo"
                                            argv,
                                            environ,
                                            stderr[1],
                                            stdout[1],
                                            ps::MakeOperatingSystem ());

}
catch (std::exception const &error)
{
    std::cerr << error.what ();
};

Reading a file descriptor to a std::vector

#include <cpp-subprocess/readfd.h>

try
{
    auto os = ps::MakeOperatingSystem ()
    std::vector <string> lines = ps::ReadFDToLines (fd, os);
}

About

Some C++ subprocess handling classes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published