mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-07-08 01:30:39 +08:00
Add options to wchar / char convert
This commit is contained in:
@@ -72,8 +72,8 @@ unsigned long wchar_util::getWideCharToMultiByteOptions(const unsigned long ori_
|
|||||||
return ori_options;
|
return ori_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp) {
|
bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp, unsigned long options) {
|
||||||
DWORD opt = getMultiByteToWideCharOptions(MB_ERR_INVALID_CHARS, cp);
|
DWORD opt = getMultiByteToWideCharOptions(options, cp);
|
||||||
int wlen = MultiByteToWideChar(cp, opt, inp.c_str(), inp.length(), nullptr, 0);
|
int wlen = MultiByteToWideChar(cp, opt, inp.c_str(), inp.length(), nullptr, 0);
|
||||||
if (!wlen) {
|
if (!wlen) {
|
||||||
return false;
|
return false;
|
||||||
@@ -91,8 +91,8 @@ bool wchar_util::str_to_wstr(std::wstring& out, std::string inp, unsigned int cp
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wchar_util::wstr_to_str(std::string& out, std::wstring inp, unsigned int cp) {
|
bool wchar_util::wstr_to_str(std::string& out, std::wstring inp, unsigned int cp, unsigned long options) {
|
||||||
DWORD opt = getWideCharToMultiByteOptions(WC_ERR_INVALID_CHARS, cp);
|
DWORD opt = getWideCharToMultiByteOptions(options, cp);
|
||||||
int len = WideCharToMultiByte(cp, opt, inp.c_str(), inp.length(), nullptr, 0, nullptr, nullptr);
|
int len = WideCharToMultiByte(cp, opt, inp.c_str(), inp.length(), nullptr, 0, nullptr, nullptr);
|
||||||
if (!len) {
|
if (!len) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef _UTIL_WCHAR_UTIL_H
|
#ifndef _UTIL_WCHAR_UTIL_H
|
||||||
#define _UTIL_WCHAR_UTIL_H
|
#define _UTIL_WCHAR_UTIL_H
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
namespace wchar_util {
|
namespace wchar_util {
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
@@ -25,7 +26,7 @@ namespace wchar_util {
|
|||||||
* @param cp Code page of input string
|
* @param cp Code page of input string
|
||||||
* @returns true if successed
|
* @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
|
* @brief Convert wstring to string
|
||||||
* @param out Output string
|
* @param out Output string
|
||||||
@@ -33,7 +34,7 @@ namespace wchar_util {
|
|||||||
* @param cp Code page of output string
|
* @param cp Code page of output string
|
||||||
* @returns true if successed
|
* @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
|
* @brief Get unicode version argv by using win api
|
||||||
* @param argv Result argv (need free memory by calling freeArgv.
|
* @param argv Result argv (need free memory by calling freeArgv.
|
||||||
|
|||||||
Reference in New Issue
Block a user