You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
852 B
C++

#include <shell.h>
std::string shell_cmd(char const *cmd)
{
std::array<char, 128> buffer;
std::string resp;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe)
throw std::runtime_error("\n\033[1;31mThat was unexpected!\033[0m");
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
resp += buffer.data();
}
trim(resp);
return resp;
}
void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(),
s.end());
}
void trim(std::string &s)
{
ltrim(s);
rtrim(s);
}