Util: Add additional System operator support

This commit is contained in:
Riyyi
2021-09-17 01:13:07 +02:00
parent 2fca315557
commit 06367a05b8
2 changed files with 58 additions and 2 deletions
+55 -2
View File
@@ -77,9 +77,58 @@ System System::operator()(const std::vector<std::string_view>& 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<std::string>& 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];
+3
View File
@@ -30,7 +30,10 @@ public:
System operator()(const std::vector<std::string>& arguments);
System operator()(const std::vector<std::string_view>& arguments);
System operator+(System rhs);
System operator|(System rhs);
System operator||(System rhs);
System operator&&(System rhs);
void print(const std::vector<std::string>& arguments);