123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #pragma once
- #include <tchar.h>
- #include <stdio.h>
- #include <windows.h>
- #include <string>
- #include <utility>
- #include <stdexcept>
- #include <system_error>
- namespace std {
- struct xstring_extension {};
- #if defined(_UNICODE) || defined(UNICODE)
- class xstring final : public wstring {
- public:
- using wstring::wstring;
- using wstring::operator=;
- xstring(const wstring& wstr) : wstring(wstr) {}
- xstring(wstring&& wstr) : wstring(std::move(wstr)) {}
- xstring(xstring_extension, const string& str, DWORD CodePage = CP_ACP) {
- auto len = MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(static_cast<size_t>(len) - 1);
- len = MultiByteToWideChar(CodePage, 0, str.c_str(), -1, data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- xstring(xstring_extension, const char* lpstr, DWORD CodePage = CP_ACP) {
- auto len = MultiByteToWideChar(CodePage, 0, lpstr, -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(static_cast<size_t>(len) - 1);
- len = MultiByteToWideChar(CodePage, 0, lpstr, -1, data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- #else
- class xstring final : public string {
- public:
- using string::string;
- using string::operator=;
- xstring(const string& str) : string(str) {}
- xstring(string&& str) : string(std::move(str)) {}
- xstring(xstring_extension, const string& str, DWORD CodePage = CP_ACP) {
- if (CodePage == CP_ACP || CodePage == GetACP()) {
- assign(str);
- } else {
- std::wstring wstr;
- auto len = MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- wstr.resize(len - 1);
- len = MultiByteToWideChar(CodePage, 0, str.c_str(), -1, wstr.data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(len - 1);
- len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- }
- xstring(xstring_extension, const char* lpstr, DWORD CodePage = CP_ACP) {
- if (CodePage == CP_ACP || CodePage == GetACP()) {
- assign(lpstr);
- } else {
- std::wstring wstr;
- auto len = MultiByteToWideChar(CodePage, 0, lpstr, -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- wstr.resize(len - 1);
- len = MultiByteToWideChar(CodePage, 0, lpstr, -1, wstr.data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(len - 1);
- len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- }
- xstring(xstring_extension, const wstring& wstr) {
- auto len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(len - 1);
- len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- xstring(xstring_extension, const wchar_t* lpwstr) {
- auto len = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- resize(len - 1);
- len = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- }
- #endif
- std::string explicit_string(DWORD CodePage = CP_ACP) const {
- #if defined(_UNICODE) || defined(UNICODE)
- std::string str;
- auto len = WideCharToMultiByte(CodePage, 0, c_str(), -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- str.resize(static_cast<size_t>(len) - 1);
- len = WideCharToMultiByte(CodePage, 0, c_str(), -1, str.data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- return str;
- #else
- if (CodePage == CP_ACP || CodePage == GetACP()) {
- return *this;
- } else {
- std::string str;
- std::wstring wstr;
- auto len = MultiByteToWideChar(CP_ACP, 0, c_str(), -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- wstr.resize(len - 1);
- len = MultiByteToWideChar(CP_ACP, 0, c_str(), -1, wstr.data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- len = WideCharToMultiByte(CodePage, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- str.resize(len - 1);
- len = WideCharToMultiByte(CodePage, 0, wstr.c_str(), -1, str.data(), len, NULL, NULL);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- return str;
- }
- #endif
- }
- std::wstring explicit_wstring() const {
- #if defined(_UNICODE) || defined(UNICODE)
- return *this;
- #else
- std::wstring wstr;
- auto len = MultiByteToWideChar(CP_ACP, 0, c_str(), -1, NULL, 0);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- wstr.resize(len - 1);
- len = MultiByteToWideChar(CP_ACP, 0, c_str(), -1, wstr.data(), len);
- if (len == 0) {
- auto err = GetLastError();
- throw std::system_error(err, std::system_category());
- }
- return wstr;
- #endif
- }
- template<typename... __Ts>
- static xstring format(const xstring& Format, __Ts&&... Args) {
- xstring s;
-
- auto len = _sctprintf(Format.c_str(), std::forward<__Ts>(Args)...);
- if (len == -1) {
- throw std::invalid_argument("_sctprintf failed.");
- }
- s.resize(len);
- _sntprintf_s(s.data(), s.length() + 1, _TRUNCATE, Format.c_str(), std::forward<__Ts>(Args)...);
- return s;
- }
- template<typename... __Ts>
- static xstring format(PCTSTR lpszFormat, __Ts&& ... Args) {
- xstring s;
- auto len = _sctprintf(lpszFormat, std::forward<__Ts>(Args)...);
- if (len == -1) {
- throw std::invalid_argument("_sctprintf failed.");
- }
- s.resize(len);
- _sntprintf_s(s.data(), s.length() + 1, _TRUNCATE, lpszFormat, std::forward<__Ts>(Args)...);
- return s;
- }
- };
- }
|