fix: windows parsing

This commit is contained in:
2026-03-16 17:41:45 +04:00
parent 8befc60016
commit 182974d61a
6 changed files with 95 additions and 31 deletions

View File

@@ -3,26 +3,13 @@
#include "windows_parser.hpp"
#include <Windows.h>
#include <iostream>
#include <memory>
#include <shellapi.h>
#include <stdexcept>
#include <string>
std::string utf8_from_wstring(const std::wstring &w) {
if (w.empty())
return {};
int needed = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (needed <= 0) {
throw std::runtime_error("WideCharToMultiByte sizing failed (" + std::to_string(::GetLastError()) + ")");
}
std::string out;
out.resize(static_cast<size_t>(needed - 1));
int written =
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, out.data(), needed - 1, nullptr, nullptr);
if (written <= 0) {
throw std::runtime_error("WideCharToMultiByte convert failed (" + std::to_string(::GetLastError()) + ")");
}
return out;
}
using namespace std::string_literals;
struct local_free_deleter {
void operator()(void *ptr) const {
@@ -33,28 +20,81 @@ struct local_free_deleter {
}
};
void parse_windows_arguments(std::vector<std::string> &parsed_arguments) {
std::string windows_error_message(DWORD error_code) {
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&messageBuffer), 0, nullptr);
if (size == 0 || !messageBuffer) {
return "Unknown Error ("s + std::to_string(error_code) + ")";
}
std::unique_ptr<char, local_free_deleter> smartBuffer(messageBuffer);
std::string result(smartBuffer.get(), size);
result.erase(result.find_last_not_of(" \n\r\t") + 1);
return result;
}
std::string utf8_from_wstring(const std::wstring &w) {
if (w.empty())
return {};
int needed = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (needed <= 0) {
throw std::runtime_error("WideCharToMultiByte sizing failed ("s + windows_error_message(::GetLastError()) +
")");
}
std::string out;
out.resize(needed - 1);
int written =
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, w.c_str(), -1, out.data(), needed, nullptr, nullptr);
if (written <= 0) {
throw std::runtime_error(
"WideCharToMultiByte convert failed, Error("s + windows_error_message(::GetLastError()) + ")" +
" Size (Needed): " + std::to_string(needed) + " Size (Written): " + std::to_string(written) +
" Size (Allocated): " + std::to_string(out.size()));
}
return out;
}
void parse_windows_arguments(std::vector<std::string> &parsed_arguments,
std::function<void(std::string)> const &setProgramName) {
int argc_w;
std::unique_ptr<LPWSTR[], local_free_deleter> argv_w(CommandLineToArgvW(GetCommandLineW(), &argc_w));
if (argv_w == nullptr) {
throw std::runtime_error("CommandLineToArgvW failed");
}
for (int i = 0; i < argc_w; i++) {
std::string arg = utf8_from_wstring(argv_w[i]);
parsed_arguments.emplace_back(arg);
if (argc_w <= 0) {
return;
}
setProgramName(utf8_from_wstring(argv_w[0]));
for (int i = 1; i < argc_w; i++) {
try {
std::string arg = utf8_from_wstring(argv_w[i]);
parsed_arguments.emplace_back(arg);
} catch (std::runtime_error e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
}
namespace argument_parser {
windows_parser::windows_parser() {
parse_windows_arguments(parsed_arguments);
parse_windows_arguments(parsed_arguments,
[this](std::string const &program_name) { this->program_name = program_name; });
}
} // namespace argument_parser
namespace argument_parser::v2 {
windows_parser::windows_parser() {
parse_windows_arguments(ref_parsed_args());
parse_windows_arguments(ref_parsed_args(),
[this](std::string const &program_name) { this->set_program_name(program_name); });
}
} // namespace argument_parser::v2