mirror of
https://github.com/arun11299/cpp-subprocess.git
synced 2025-08-04 20:36:20 -04:00
WIP: Add windows compatibility (#30)
* add package files * add windows compat to subprocess.hpp * add test modifications * repair test_read_all
This commit is contained in:
parent
de5f791d04
commit
5d92f48492
1
.clang-format
Normal file
1
.clang-format
Normal file
@ -0,0 +1 @@
|
|||||||
|
BreakBeforeBraces: Stroustrup
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
build*
|
||||||
|
.vscode
|
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.9)
|
||||||
|
|
||||||
|
project(subprocess CXX)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
add_subdirectory(test)
|
819
subprocess.hpp
819
subprocess.hpp
File diff suppressed because it is too large
Load Diff
8
test/CMakeLists.txt
Normal file
8
test/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
add_executable(test_subprocess test_subprocess.cc)
|
||||||
|
|
||||||
|
add_test(
|
||||||
|
NAME test_subprocess
|
||||||
|
COMMAND $<TARGET_FILE:test_subprocess>
|
||||||
|
)
|
@ -1,19 +1,27 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "../subprocess.hpp"
|
#include "../subprocess.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace subprocess;
|
using namespace subprocess;
|
||||||
|
|
||||||
void test_exename()
|
void test_exename()
|
||||||
{
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
auto ret = call({"--version"}, executable{"cmake"}, shell{false});
|
||||||
|
#else
|
||||||
auto ret = call({"-l"}, executable{"ls"}, shell{false});
|
auto ret = call({"-l"}, executable{"ls"}, shell{false});
|
||||||
|
#endif
|
||||||
std::cout << ret << std::endl;
|
std::cout << ret << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_input()
|
void test_input()
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
|
auto p = Popen({"cmake", "--version"}, output{PIPE}, input{PIPE});
|
||||||
|
#else
|
||||||
auto p = Popen({"grep", "f"}, output{PIPE}, input{PIPE});
|
auto p = Popen({"grep", "f"}, output{PIPE}, input{PIPE});
|
||||||
const char* msg = "one\ntwo\nthree\nfour\nfive\n";
|
const char *msg = "one\ntwo\nthree\nfour\nfive\n";
|
||||||
p.send(msg, strlen(msg));
|
p.send(msg, strlen(msg));
|
||||||
|
#endif
|
||||||
auto res = p.communicate(nullptr, 0);
|
auto res = p.communicate(nullptr, 0);
|
||||||
std::cout << res.first.buf.data() << std::endl;
|
std::cout << res.first.buf.data() << std::endl;
|
||||||
}
|
}
|
||||||
@ -22,7 +30,8 @@ void test_piping()
|
|||||||
{
|
{
|
||||||
auto cat = Popen({"cat", "../subprocess.hpp"}, output{PIPE});
|
auto cat = Popen({"cat", "../subprocess.hpp"}, output{PIPE});
|
||||||
auto grep = Popen({"grep", "template"}, input{cat.output()}, output{PIPE});
|
auto grep = Popen({"grep", "template"}, input{cat.output()}, output{PIPE});
|
||||||
auto cut = Popen({"cut", "-d,", "-f", "1"}, input{grep.output()}, output{PIPE});
|
auto cut =
|
||||||
|
Popen({"cut", "-d,", "-f", "1"}, input{grep.output()}, output{PIPE});
|
||||||
auto res = cut.communicate().first;
|
auto res = cut.communicate().first;
|
||||||
std::cout << res.buf.data() << std::endl;
|
std::cout << res.buf.data() << std::endl;
|
||||||
}
|
}
|
||||||
@ -45,7 +54,10 @@ void test_sleep()
|
|||||||
|
|
||||||
while (p.poll() == -1) {
|
while (p.poll() == -1) {
|
||||||
std::cout << "Waiting..." << std::endl;
|
std::cout << "Waiting..." << std::endl;
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#else
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl;
|
std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl;
|
||||||
@ -56,7 +68,7 @@ void test_read_all()
|
|||||||
Popen p = Popen({"echo","12345678"}, output{PIPE});
|
Popen p = Popen({"echo","12345678"}, output{PIPE});
|
||||||
|
|
||||||
std::vector<char> buf(6);
|
std::vector<char> buf(6);
|
||||||
int rbytes = util::read_all(fileno(p.output()), buf);
|
int rbytes = util::read_all(p.output(), buf);
|
||||||
|
|
||||||
std::string out(buf.begin(), buf.end());
|
std::string out(buf.begin(), buf.end());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user