Browse Source

Util: Add additional System operator support

master
Riyyi 3 years ago
parent
commit
06367a05b8
  1. 57
      src/util/system.cpp
  2. 3
      src/util/system.h

57
src/util/system.cpp

@ -77,9 +77,58 @@ System System::operator()(const std::vector<std::string_view>& arguments)
return System(stringArguments); 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) 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; return rhs;
} }
@ -103,6 +152,10 @@ void System::print(const std::vector<std::string>& arguments)
System System::exec(std::string input) System System::exec(std::string input)
{ {
if (m_arguments.empty()) {
return *this;
}
int stdinFd[2]; int stdinFd[2];
int stdoutFd[2]; int stdoutFd[2];
int stderrFd[2]; int stderrFd[2];
@ -146,6 +199,7 @@ System System::exec(std::string input)
} }
// Parent // Parent
default: default:
m_arguments.clear();
break; break;
} }
@ -170,7 +224,6 @@ System System::exec(std::string input)
void System::readFromFileDescriptor(int fileDescriptor[2], std::string& output) void System::readFromFileDescriptor(int fileDescriptor[2], std::string& output)
{ {
close(fileDescriptor[WriteFileDescriptor]); close(fileDescriptor[WriteFileDescriptor]);
output.clear();
constexpr int bufferSize = 4096; constexpr int bufferSize = 4096;
char buffer[bufferSize]; char buffer[bufferSize];

3
src/util/system.h

@ -30,7 +30,10 @@ public:
System operator()(const std::vector<std::string>& arguments); System operator()(const std::vector<std::string>& arguments);
System operator()(const std::vector<std::string_view>& 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);
System operator&&(System rhs);
void print(const std::vector<std::string>& arguments); void print(const std::vector<std::string>& arguments);

Loading…
Cancel
Save