/
thomas-king
/
Raze
Обзор
Документация
Войти
/
thomas-king
/
Raze
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
lib/Library/Shell.cpp
65 строк
1 KB
Ace Rodstin
Add progress indicator. Fix caching.
22 ноя 2023, 21:48
22 ноя 2023, 21:48
0b09a0a
Код
Авторство
О чём код?
// // Shell.cpp // Library // // Created by Ace Rodstin on 11/23/23. // Updated by Ace Rodstin on 11/24/23. // // Copyright (C) Ace Rodstin 2023. All rights reserved. // #include "Library/Shell.h" using namespace lib; string Shell::execute(string command) { string output = ""; array<char, 128> buffer {}; string cmd = command + " 2>&1"; // redirect stderr auto pipe = popen(cmd.c_str(), "r"); if (pipe == nullptr) { throw ServiceError(ServiceError::Reason::EXECUTION_FAILED); } while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) { output += buffer.data(); } auto result = pclose(pipe); if (result != 0) { throw ServiceError(ServiceError::Reason::RESULT_FAILURE, output); } return output; } Shell::ServiceError::ServiceError(Reason reason, string output): reason { reason }, output { output } {} string Shell::ServiceError::message() const { string result = ""; switch (reason) { case Reason::EXECUTION_FAILED: result = "Execution failed"; break; case Reason::RESULT_FAILURE: result = "Result is failure"; break; }; return result; } Shell::ServiceError::Reason Shell::ServiceError::getReason() const { return reason; } string Shell::ServiceError::getOutput() const { return output; }