diff --git a/.gitmodules b/.gitmodules index 853ef1e..0ef977e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -20,3 +20,6 @@ [submodule "src/ui/impl/webview/lib/webviewpp"] path = src/ui/impl/webview/lib/webviewpp url = https://github.com/Soundux/webviewpp +[submodule "lib/tiny-process-library"] + path = lib/tiny-process-library + url = https://gitlab.com/eidheim/tiny-process-library/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 09d523e..1da0520 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,8 +50,9 @@ endif() add_subdirectory(src/ui/impl/webview/lib/webviewpp EXCLUDE_FROM_ALL) add_subdirectory(lib/nativefiledialog-extended EXCLUDE_FROM_ALL) +add_subdirectory(lib/tiny-process-library EXCLUDE_FROM_ALL) add_subdirectory(lib/InstanceGuard/Source EXCLUDE_FROM_ALL) -target_link_libraries(soundux PUBLIC webview nfd InstanceGuard) +target_link_libraries(soundux PUBLIC webview nfd InstanceGuard tiny-process-library) # [[ Build Frontend ]] if (MSVC) diff --git a/lib/tiny-process-library b/lib/tiny-process-library new file mode 160000 index 0000000..15e4f77 --- /dev/null +++ b/lib/tiny-process-library @@ -0,0 +1 @@ +Subproject commit 15e4f77f8254e4b093f6be128db50fe4b6bee120 diff --git a/src/helper/misc/misc.cpp b/src/helper/misc/misc.cpp index 4257d24..96f6af8 100644 --- a/src/helper/misc/misc.cpp +++ b/src/helper/misc/misc.cpp @@ -1,9 +1,9 @@ #include "misc.hpp" -#include #include #include #include #include +#include #include #include @@ -24,6 +24,14 @@ namespace Soundux::Helpers } return result; } + bool exec(const std::string &command, std::string &result) + { + result.clear(); + TinyProcessLib::Process process( + command, "", [&](const char *data, std::size_t dataLen) { result += std::string(data, dataLen); }); + + return process.get_exit_status() == 0; + } #if defined(_WIN32) std::wstring widen(const std::string &s) { @@ -38,23 +46,6 @@ namespace Soundux::Helpers } #endif #if defined(__linux__) - bool exec(const std::string &command, std::string &result) - { - result.clear(); - - std::array buffer; - auto *pipe = popen(command.c_str(), "r"); - if (!pipe) - { - throw std::runtime_error("popen failed"); - } - while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) - { - result += buffer.data(); - } - - return pclose(pipe) == 0; - } std::optional getPpid(int pid) { std::filesystem::path path("/proc/" + std::to_string(pid)); diff --git a/src/helper/misc/misc.hpp b/src/helper/misc/misc.hpp index 65110f2..86f7d9c 100644 --- a/src/helper/misc/misc.hpp +++ b/src/helper/misc/misc.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -12,8 +13,8 @@ namespace Soundux #endif #if defined(__linux__) std::optional getPpid(int pid); - bool exec(const std::string &command, std::string &result); #endif + bool exec(const std::string &command, std::string &result); std::vector splitByNewLine(const std::string &str); } // namespace Helpers } // namespace Soundux \ No newline at end of file