<commit_before>CPP Subprocess
==============
Some C++ subprocess handling classes.

Status
======
[![Build Status](https://travis-ci.org/polysquare/cpp-subprocess.svg?branch=master)](https://travis-ci.org/polysquare/cpp-subprocess)
[![Coverage Status](https://coveralls.io/repos/polysquare/cpp-subprocess/badge.png?branch=master)](https://coveralls.io/r/polysquare/cpp-subprocess?branch=master)

Usage
====

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 <string>
--------------------------------------------

    #include <cpp-subprocess/readfd.h>

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

<commit_msg>README: Fix trailing whitespace<commit_after>CPP Subprocess
==============
Some C++ subprocess handling classes.

Status
======
[![Build Status](https://travis-ci.org/polysquare/cpp-subprocess.svg?branch=master)](https://travis-ci.org/polysquare/cpp-subprocess)
[![Coverage Status](https://coveralls.io/repos/polysquare/cpp-subprocess/badge.png?branch=master)](https://coveralls.io/r/polysquare/cpp-subprocess?branch=master)

Usage
====

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 <string>
--------------------------------------------

    #include <cpp-subprocess/readfd.h>

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


{'fraction_non_alphanumeric': 0.1818576388888889, 'fraction_numerical': 0.003472222222222222, 'mean_word_length': 1.5452871870397644, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 33, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3032471', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1871, 'n_words': 397}