Pipe streams supported for MacOS too.

This commit is contained in:
Matthias 2020-06-26 16:09:46 -07:00
parent 1da49fc50a
commit a4c02357c3
3 changed files with 37 additions and 7 deletions

View File

@ -1106,10 +1106,22 @@ size_t
InputPipe::read (char *b, size_t n)
{
tl_assert (m_file != NULL);
size_t ret = fread (b, 1, n, m_file);
if (ret < n) {
if (ferror (m_file)) {
throw FilePReadErrorException (m_source, ferror (m_file));
bool retry = true;
size_t ret = 0;
while (retry) {
retry = false;
ret = fread (b, 1, n, m_file);
if (ret < n) {
if (ferror (m_file)) {
if (errno != EINTR) {
throw FilePReadErrorException (m_source, errno);
} else if (ret == 0) {
retry = true;
clearerr (m_file);
}
}
}
}
@ -1138,7 +1150,7 @@ OutputPipe::OutputPipe (const std::string &path)
OutputPipe::~OutputPipe ()
{
if (m_file != NULL) {
fclose (m_file);
pclose (m_file);
m_file = NULL;
}
}
@ -1147,10 +1159,11 @@ void
OutputPipe::write (const char *b, size_t n)
{
tl_assert (m_file != NULL);
size_t ret = fwrite (b, 1, n, m_file);
if (ret < n) {
if (ferror (m_file)) {
throw FilePWriteErrorException (m_source, ferror (m_file));
if (ferror (m_file) && errno != EINTR) {
throw FilePReadErrorException (m_source, errno);
}
}
}

View File

@ -52,6 +52,23 @@ TEST(InputPipe2)
EXPECT_NE (ret, 0);
}
TEST(OutputPipe1)
{
std::string tf = tmp_file ("pipe_out");
{
tl::OutputPipe pipe ("cat >" + tf);
tl::OutputStream str (pipe);
str << "Hello, world!";
}
{
tl::InputStream is (tf);
std::string s = is.read_all ();
EXPECT_EQ (s, "Hello, world!");
}
}
TEST(TextOutputStream)
{
std::string fn = tmp_file ("test.txt");

BIN
testdata/net_tracer/t6_all_nets.oas.gz.4 vendored Normal file

Binary file not shown.