libfilezilla
process.cpp

A simple demonstration of using fz::processThis example spawns the timer_fizzbuzz.cpp demo and controls it via the redirected IO, in blocking mode.

#include <iostream>
#include <string>
namespace {
// Helper function to extract a directory from argv[0] so that the
// demos can be run independent of the current working directory.
fz::native_string get_program_dir(int argc, char ** argv)
{
std::string_view path;
if (argc > 0) {
path = argv[0];
#ifdef FZ_WINDOWS
auto delim = path.find_last_of("/\\");
#else
auto delim = path.find_last_of("/");
#endif
if (delim == std::string::npos) {
path = std::string_view();
}
else {
path = path.substr(0, delim + 1);
}
}
return fz::to_native(path);
}
#ifdef FZ_WINDOWS
auto suffix = fzT(".exe");
#else
auto suffix = fzT("");
#endif
}
int main(int argc, char *argv[])
{
// Start the timer_fizzbuzz demo which should be in the same directory as the process demo
if (!p.spawn(get_program_dir(argc, argv) + fzT("timer_fizzbuzz") + suffix)) {
std::cerr << "Could not spawn process" << std::endl;
return 1;
}
std::cout << "Spawned process" << std::endl;
// Send a line to the process
if (!p.write("6\n")) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
return 1;
}
std::cout << "Waiting on process to print woof..." << std::endl;
// We don't want to do anything else while waiting, so we don't need a thread to receive data asynchronously
std::string input;
bool done = false;
while (!done) {
char buf[100];
fz::rwresult r = p.read(buf, 100);
if (!r) {
std::cerr << "Could not read from process" << std::endl;
return 1;
}
else if (!r.value_) {
std::cerr << "Unexpected EOF from process" << std::endl;
return 1;
}
input += std::string(buf, r.value_);
// Extract complete lines from the input
auto delim = input.find_first_of("\r\n");
while (delim != std::string::npos) {
std::string line = input.substr(0, delim);
input = input.substr(delim + 1);
delim = input.find_first_of("\r\n");
if (!line.empty()) {
std::cout << "Received line from process: " << line << std::endl;
if (line == "woof") {
done = true;
}
}
}
}
// Send a line to the process
if (!p.write("0\n")) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
return 1;
}
std::cout << "Told process to quit." << std::endl;
while (true) {
char buf[100];
fz::rwresult r = p.read(buf, 100);
if (!r) {
std::cerr << "Could not read from process" << std::endl;
return 1;
}
else if (!r.value_) {
std::cerr << "Received the expected EOF from process" << std::endl;
break;
}
}
return 0;
}
The process class manages an asynchronous process with redirected IO.
Definition: process.hpp:61
rwresult write(void const *buffer, size_t len)
Write data data process.
rwresult read(void *buffer, size_t len)
Read data from process.
bool spawn(native_string const &cmd, std::vector< native_string > const &args=std::vector< native_string >(), io_redirection redirect_mode=io_redirection::redirect)
Start the process.
Holds the result of read/write operations.
Definition: fsresult.hpp:74
size_t value_
Undefined if error_ is not none.
Definition: fsresult.hpp:116
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:34
native_string to_native(std::string_view const &in)
Converts std::string to native_string.
Header for the #process class.
#define fzT(x)
Macro for a string literal in system-native character type. Note: Macro definition changes depending...
Definition: string.hpp:257