This commit is contained in:
2025-12-18 00:07:04 -05:00
parent e81030d9e3
commit 6257e7dffb
2 changed files with 42 additions and 2 deletions

View File

@@ -1,7 +1,33 @@
#include "process_.h"
#ifdef WIN32
#include <cstddef>
#include <string>
#include <windows.h>
#endif
namespace anm2ed::util
{
#ifdef WIN32
namespace
{
std::wstring utf8_to_wstring(const char* string)
{
if (!string) return {};
auto requiredSize = MultiByteToWideChar(CP_UTF8, 0, string, -1, nullptr, 0);
if (requiredSize <= 0) return {};
std::wstring wide(static_cast<std::size_t>(requiredSize), L'\0');
MultiByteToWideChar(CP_UTF8, 0, string, -1, wide.data(), requiredSize);
if (!wide.empty() && wide.back() == L'\0') wide.pop_back();
return wide;
}
}
#endif
Process::Process(const char* command, const char* mode) { open(command, mode); }
Process::~Process() { close(); }
@@ -11,7 +37,9 @@ namespace anm2ed::util
close();
#ifdef WIN32
pipe = _popen(command, mode);
auto wideCommand = utf8_to_wstring(command);
auto wideMode = utf8_to_wstring(mode);
pipe = _wpopen(wideCommand.c_str(), wideMode.c_str());
#else
pipe = popen(command, mode);
#endif
@@ -52,4 +80,4 @@ namespace anm2ed::util
FILE* Process::get() const { return pipe; }
Process::operator bool() const { return pipe != nullptr; }
}
}