From 06367a05b8f853fd1bf551d314cbbefd201044b6 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 17 Sep 2021 01:13:07 +0200 Subject: [PATCH] Util: Add additional System operator support --- src/util/system.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++-- src/util/system.h | 3 +++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/util/system.cpp b/src/util/system.cpp index 057b6db..8d2e2ba 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -77,9 +77,58 @@ System System::operator()(const std::vector& arguments) return System(stringArguments); } +// Shell equivalent ; +System System::operator+(System rhs) +{ + auto lhs = *this; + + lhs.exec(); + rhs.m_output.append(lhs.m_output); + rhs.m_error.append(lhs.m_error); + rhs.exec(); + + return rhs; +} + System System::operator|(System rhs) { - rhs.exec(exec().output()); + auto lhs = *this; + + lhs.exec(); + rhs.exec(lhs.m_output); + + return rhs; +} + +System System::operator||(System rhs) +{ + auto lhs = *this; + + lhs.exec(); + if (lhs.m_status == 0) { + return lhs; + } + + rhs.m_output.append(lhs.m_output); + rhs.m_error.append(lhs.m_error); + rhs.exec(); + + return rhs; +} + +System System::operator&&(System rhs) +{ + auto lhs = *this; + + lhs.exec(); + if (lhs.m_status > 0) { + return lhs; + } + + rhs.m_output.append(lhs.m_output); + rhs.m_error.append(lhs.m_error); + rhs.exec(); + return rhs; } @@ -103,6 +152,10 @@ void System::print(const std::vector& arguments) System System::exec(std::string input) { + if (m_arguments.empty()) { + return *this; + } + int stdinFd[2]; int stdoutFd[2]; int stderrFd[2]; @@ -146,6 +199,7 @@ System System::exec(std::string input) } // Parent default: + m_arguments.clear(); break; } @@ -170,7 +224,6 @@ System System::exec(std::string input) void System::readFromFileDescriptor(int fileDescriptor[2], std::string& output) { close(fileDescriptor[WriteFileDescriptor]); - output.clear(); constexpr int bufferSize = 4096; char buffer[bufferSize]; diff --git a/src/util/system.h b/src/util/system.h index b57990a..cdac3e4 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -30,7 +30,10 @@ public: System operator()(const std::vector& arguments); System operator()(const std::vector& arguments); + System operator+(System rhs); System operator|(System rhs); + System operator||(System rhs); + System operator&&(System rhs); void print(const std::vector& arguments);