CppCommon  1.0.4.1
C++ Common Library
system_process_pipes.cpp

Process pipes example

#include "system/process.h"
#include <algorithm>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
if (argc > 1)
{
std::string message = "test message";
std::string endline = CppCommon::Environment::EndLine();
std::cout << "Executing child process..." << std::endl;
CppCommon::Process child = CppCommon::Process::Execute(argv[1], nullptr, nullptr, nullptr, &input, &output, &error);
input.Write(message);
input.Write(endline);
std::string out = output.ReadAllText();
std::string err = error.ReadAllText();
int result = child.Wait();
std::cout << "Executed child process! Result = " << result << std::endl;
std::cout << "stdout: " << out << std::endl;
std::cout << "stderr: " << err << std::endl;
return 0;
}
std::string line;
if (getline(std::cin, line))
{
std::cout << line << std::endl;
std::reverse(line.begin(), line.end());
std::cerr << line << std::endl;
}
return 123;
}
static std::string EndLine()
Get text end line separator.
Pipe.
Definition: pipe.h:31
size_t Write(const void *buffer, size_t size) override
Write a byte buffer into the pipe.
Definition: pipe.cpp:208
std::string ReadAllText()
Read all text.
Definition: reader.cpp:32
Process abstraction.
Definition: process.h:38
static Process Execute(const std::string &command, const std::vector< std::string > *arguments=nullptr, const std::map< std::string, std::string > *envars=nullptr, const std::string *directory=nullptr, Pipe *input=nullptr, Pipe *output=nullptr, Pipe *error=nullptr)
Execute a new process.
Definition: process.cpp:549
int Wait()
Wait the process to exit.
Definition: process.cpp:541
Environment management definition.
Process definition.