스타일 정리 및 컴파일 오류 제거

This commit is contained in:
2025-08-31 02:29:42 +09:00
parent f3974bb86f
commit e26d20a869
6 changed files with 97 additions and 89 deletions

View File

@@ -7,32 +7,35 @@ namespace utils {
// @brief 명령어가 실행 가능한지 체크합니다 // @brief 명령어가 실행 가능한지 체크합니다
// @param cmd 실행할 명령 // @param cmd 실행할 명령
// @return int error_code // @return int error_code
int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector<std::string>& args = std::vector<std::string>()); int ValidateCommand(
boost::asio::io_context& ctx, std::string cmd,
const std::vector<std::string>& args = std::vector<std::string>());
// @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다
// @param cmd 실행할 명령 // @param cmd 실행할 명령
// @param args 아규먼트 // @param args 아규먼트
// @return int error_code // @return int error_code
int ExecuteCommand(boost::asio::io_context& ctx, int ExecuteCommand(
const std::string& cmd, boost::asio::io_context& ctx, const std::string& cmd,
const std::vector<std::string>& args = std::vector<std::string>()); const std::vector<std::string>& args = std::vector<std::string>());
// @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다
// @param cmd 실행할 명령 // @param cmd 실행할 명령
// @param result 실행결과 // @param result 실행결과
// @return int error_code // @return int error_code
int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result); int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd,
std::string& result);
// @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다
// @param cmd 실행할 명령 // @param cmd 실행할 명령
// @param args 아규먼트 // @param args 아규먼트
// @param result 실행결과 // @param result 실행결과
// @return int error_code // @return int error_code
int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector<std::string>& args, int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd,
std::string& result); const std::vector<std::string>& args, std::string& result);
// @brief 명령어를 쉘에서 실행하고 결과를 파이프로 연결하여 반환합니다 // @brief 명령어를 쉘에서 실행하고 결과를 파이프로 연결하여 반환합니다
// @param cmd 실행할 명령 // @param cmd 실행할 명령
// @param args 아규먼트 // @param args 아규먼트
// @return boost::process::popen // @return boost::process::popen
boost::process::popen OpenPipe(boost::asio::io_context& ctx, boost::process::popen OpenPipe(
const std::string& cmd, boost::asio::io_context& ctx, const std::string& cmd,
const std::vector<std::string>& args = std::vector<std::string>()); const std::vector<std::string>& args = std::vector<std::string>());
} // namespace utils } // namespace utils
#endif #endif

View File

@@ -5,7 +5,7 @@
namespace utils { namespace utils {
int InstallYtdlp(); int InstallYtdlp(boost::asio::io_context& ctx);
int CheckUpdate(boost::asio::io_context& ctx); int CheckUpdate(boost::asio::io_context& ctx);

View File

@@ -3,54 +3,52 @@
#include "utils/update_checker.h" #include "utils/update_checker.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
boost::asio::io_context ctx; boost::asio::io_context ctx;
boost::system::error_code ec; boost::system::error_code ec;
char buf[8192]; char buf[8192];
std::string output; std::string output;
utils::CheckUpdate(ctx); utils::CheckUpdate(ctx);
#ifdef WIN32 #ifdef WIN32
/*try { /*try {
auto ytdlp_pipe = utils::OpenPipe(ctx, auto ytdlp_pipe = utils::OpenPipe(ctx,
"yt-dlp.exe", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", "yt-dlp.exe", { "-o", "-", "--quiet", "--ignore-errors", "-f",
"https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); "bestaudio", "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" });
while (true) { while (true) {
boost::system::error_code read_ec; boost::system::error_code read_ec;
size_t bytes_read = size_t bytes_read =
boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 8192), boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf,
read_ec); 8192), read_ec); if (bytes_read > 0) { std::cout.write(buf, bytes_read);
if (bytes_read > 0) { }
std::cout.write(buf, bytes_read); if (read_ec == boost::asio::error::eof || read_ec) {
} break;
if (read_ec == boost::asio::error::eof || read_ec) { }
break; }
} }
} catch (const boost::process::system_error& e) {
} std::string error = e.what();
catch (const boost::process::system_error& e) { return -1;
std::string error = e.what(); }*/
return -1;
}*/
#else #else
auto ytdlp_pipe = utils::OpenPipe( auto ytdlp_pipe = utils::OpenPipe(
"yt-dlp", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", ctx, "yt-dlp",
"https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); {"-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio",
"https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR"});
while (true) { while (true) {
boost::system::error_code read_ec; boost::system::error_code read_ec;
size_t bytes_read = size_t bytes_read =
boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 16384), boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 8192), read_ec);
read_ec); if (bytes_read > 0) {
if (bytes_read > 0) { std::cout.write(buf, bytes_read);
std::cout.write(buf, bytes_read); }
} if (read_ec == boost::asio::error::eof || read_ec) {
if (read_ec == boost::asio::error::eof || read_ec) { break;
break; }
} }
}
#endif #endif
return 0; return 0;
} }

View File

@@ -4,7 +4,8 @@
namespace utils { namespace utils {
int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector<std::string>& args) { int ValidateCommand(boost::asio::io_context& ctx, std::string cmd,
const std::vector<std::string>& args) {
try { try {
ExecuteCommand(ctx, cmd, args); ExecuteCommand(ctx, cmd, args);
} catch (const boost::process::system_error& e) { } catch (const boost::process::system_error& e) {
@@ -19,30 +20,31 @@ int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd,
return ExecuteCommand(ctx, cmd, args, ignored); return ExecuteCommand(ctx, cmd, args, ignored);
} }
int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result) { int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd,
std::string& result) {
std::vector<std::string> ignored; std::vector<std::string> ignored;
return ExecuteCommand(ctx, cmd, ignored, result); return ExecuteCommand(ctx, cmd, ignored, result);
} }
int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector<std::string>& args, int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd,
std::string& result) { const std::vector<std::string>& args, std::string& result) {
try { try {
boost::system::error_code ec; boost::system::error_code ec;
boost::process::popen proc( boost::process::popen proc(
ctx, cmd, args, boost::process::process_stdio{ {}, nullptr, nullptr }); ctx, cmd, args, boost::process::process_stdio{{}, nullptr, nullptr});
boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec); boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec);
proc.wait(); proc.wait();
return proc.exit_code(); return proc.exit_code();
} } catch (const boost::process::system_error& e) {
catch (const boost::process::system_error& e) { return -1;
return -1;
} }
} }
boost::process::popen OpenPipe(boost::asio::io_context& ctx, const std::string& cmd, boost::process::popen OpenPipe(boost::asio::io_context& ctx,
const std::string& cmd,
const std::vector<std::string>& args) { const std::vector<std::string>& args) {
return boost::process::popen( return boost::process::popen(
ctx, cmd, args, boost::process::process_stdio{nullptr, nullptr, nullptr}); ctx, cmd, args, boost::process::process_stdio{nullptr, nullptr, nullptr});

View File

@@ -57,7 +57,8 @@ void DownloadFileFromHTTPS(std::string url, std::string filename) {
boost::beast::flat_buffer buffer; boost::beast::flat_buffer buffer;
// 응답 파서 만들기 // 응답 파서 만들기
boost::beast::http::response_parser<boost::beast::http::dynamic_body> parser; boost::beast::http::response_parser<boost::beast::http::dynamic_body>
parser;
// body 제한 해제 (무제한) // body 제한 해제 (무제한)
parser.body_limit((std::numeric_limits<std::uint64_t>::max)()); parser.body_limit((std::numeric_limits<std::uint64_t>::max)());

View File

@@ -6,7 +6,7 @@
namespace utils { namespace utils {
int InstallYtdlp() { int InstallYtdlp(boost::asio::io_context& ctx) {
BOOST_LOG_TRIVIAL(warning) << "ytdlp is unavailable. downloading ytdlp..."; BOOST_LOG_TRIVIAL(warning) << "ytdlp is unavailable. downloading ytdlp...";
#ifdef WIN32 #ifdef WIN32
DownloadFileFromHTTPS( DownloadFileFromHTTPS(
@@ -16,21 +16,22 @@ int InstallYtdlp() {
DownloadFileFromHTTPS( DownloadFileFromHTTPS(
"https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp", "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp",
"yt-dlp"); "yt-dlp");
ExecuteCommand(boost::process::environment::find_executable("chmod").c_str(), ExecuteCommand(ctx,
boost::process::environment::find_executable("chmod").string(),
{"+x", "yt-dlp"}); {"+x", "yt-dlp"});
#endif #endif
return 0; return 0;
} }
static int FindNewLinePos(std::string& string, int start_pos) { static int FindNewLinePos(std::string& string, int start_pos) {
int newline_pos = start_pos; int newline_pos = start_pos;
while (newline_pos < string.size()) { while (newline_pos < string.size()) {
if (string[newline_pos] == '\n') { if (string[newline_pos] == '\n') {
return newline_pos; return newline_pos;
}
newline_pos++;
} }
return newline_pos; newline_pos++;
}
return newline_pos;
} }
int CheckUpdate(boost::asio::io_context& ctx) { int CheckUpdate(boost::asio::io_context& ctx) {
@@ -39,27 +40,30 @@ int CheckUpdate(boost::asio::io_context& ctx) {
int newline_pos = 0; int newline_pos = 0;
#ifdef WIN32 #ifdef WIN32
if (ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output) != 0) { if (ExecuteCommand(ctx, "yt-dlp.exe", {"--version", "--newline"}, output) !=
InstallYtdlp(); 0) {
ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output); InstallYtdlp(ctx);
ExecuteCommand(ctx, "yt-dlp.exe", {"--version", "--newline"}, output);
} }
BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); BOOST_LOG_TRIVIAL(info) << "yt-dlp version: "
<< output.substr(0, output.size() - 1);
output = ""; output = "";
ExecuteCommand(ctx, "yt-dlp.exe", { "-U", "--newline" }, output); ExecuteCommand(ctx, "yt-dlp.exe", {"-U", "--newline"}, output);
#else #else
if (ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output) != 0) { if (ExecuteCommand(ctx, "yt-dlp", {"--version", "--newline"}, output) != 0) {
InstallYtdlp(); InstallYtdlp(ctx);
ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output); ExecuteCommand(ctx, "yt-dlp", {"--version", "--newline"}, output);
} }
BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); BOOST_LOG_TRIVIAL(info) << "yt-dlp version: "
<< output.substr(0, output.size() - 1);
output = ""; output = "";
ExecuteCommand(ctx, "yt-dlp", { "-U", "--newline" }, output); ExecuteCommand(ctx, "yt-dlp", {"-U", "--newline"}, output);
#endif #endif
while (newline_pos < output.size()) { while (newline_pos < output.size()) {
old_newline_pos = newline_pos; old_newline_pos = newline_pos;
newline_pos = FindNewLinePos(output, newline_pos); newline_pos = FindNewLinePos(output, newline_pos);
BOOST_LOG_TRIVIAL(info) << output.substr(old_newline_pos, newline_pos - 1); BOOST_LOG_TRIVIAL(info) << output.substr(old_newline_pos, newline_pos - 1);
newline_pos++; newline_pos++;
} }
return 0; return 0;