From b0bd9db0ae8357dc0a14985b2f414495ce57aa2b Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 21 Apr 2025 21:18:26 +0800 Subject: [PATCH] Add options to wchar / char convert --- wchar_util.cpp | 8 ++++---- wchar_util.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/wchar_util.cpp b/wchar_util.cpp index 9a6e086..9ad6a41 100644 --- a/wchar_util.cpp +++ b/wchar_util.cpp @@ -72,8 +72,8 @@ unsigned long wchar_util::getWideCharToMultiByteOptions(const unsigned long ori_ return ori_options; } -bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp) { - DWORD opt = getMultiByteToWideCharOptions(MB_ERR_INVALID_CHARS, cp); +bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp, unsigned long options) { + DWORD opt = getMultiByteToWideCharOptions(options, cp); int wlen = MultiByteToWideChar(cp, opt, inp.c_str(), inp.length(), nullptr, 0); if (!wlen) { return false; @@ -91,8 +91,8 @@ bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp return true; } -bool wchar_util::wstr_to_str(std::string& out, std::wstring inp, unsigned int cp) { - DWORD opt = getWideCharToMultiByteOptions(WC_ERR_INVALID_CHARS, cp); +bool wchar_util::wstr_to_str(std::string& out, std::wstring inp, unsigned int cp, unsigned long options) { + DWORD opt = getWideCharToMultiByteOptions(options, cp); int len = WideCharToMultiByte(cp, opt, inp.c_str(), inp.length(), nullptr, 0, nullptr, nullptr); if (!len) { return false; diff --git a/wchar_util.h b/wchar_util.h index 9cb7c4d..ff896fb 100644 --- a/wchar_util.h +++ b/wchar_util.h @@ -1,6 +1,7 @@ #ifndef _UTIL_WCHAR_UTIL_H #define _UTIL_WCHAR_UTIL_H #include +#include namespace wchar_util { #if _WIN32 @@ -25,7 +26,7 @@ namespace wchar_util { * @param cp Code page of input string * @returns true if successed */ - bool str_to_wstr(std::wstring& out, std::string inp, unsigned int cp); + bool str_to_wstr(std::wstring& out, std::string inp, unsigned int cp, unsigned long options = MB_ERR_INVALID_CHARS); /** * @brief Convert wstring to string * @param out Output string @@ -33,7 +34,7 @@ namespace wchar_util { * @param cp Code page of output string * @returns true if successed */ - bool wstr_to_str(std::string& out, std::wstring inp, unsigned int cp); + bool wstr_to_str(std::string& out, std::wstring inp, unsigned int cp, unsigned long options = WC_ERR_INVALID_CHARS); /** * @brief Get unicode version argv by using win api * @param argv Result argv (need free memory by calling freeArgv.