123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "TString.hpp"
- #include "ExceptionSystem.hpp"
- #undef __BASE_FILE__
- #define __BASE_FILE__ TEXT("TString.cpp")
- TString TStringBuilder(PCSTR LpMultiByteStr, UINT CodePage) {
- #ifdef _UNICODE
- TString TStr;
- int RequiredSize = MultiByteToWideChar(CodePage,
- NULL,
- LpMultiByteStr,
- -1,
- NULL,
- 0);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- TStr.resize(RequiredSize);
- if (!MultiByteToWideChar(CP_ACP,
- NULL,
- LpMultiByteStr,
- -1,
- TStr.data(),
- RequiredSize))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- while (TStr.back() == L'\x00')
- TStr.pop_back();
- return TStr;
- #else
- if (CodePage == CP_ACP) {
- return TString(LpMultiByteStr);
- } else {
- std::wstring WideCharStr;
- TString TStr;
- int RequiredSize = MultiByteToWideChar(CodePage,
- NULL,
- LpMultiByteStr,
- -1,
- NULL,
- 0);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- WideCharStr.resize(RequiredSize);
- if (!MultiByteToWideChar(CP_ACP,
- NULL,
- LpMultiByteStr,
- -1,
- WideCharStr.data(),
- RequiredSize))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- while (WideCharStr.back() == L'\x00')
- WideCharStr.pop_back();
- RequiredSize = WideCharToMultiByte(CP_ACP,
- NULL,
- WideCharStr.c_str(),
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- TStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CP_ACP,
- NULL,
- WideCharStr.c_str(),
- -1,
- TStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (TStr.back() == '\x00')
- TStr.pop_back();
- return TStr;
- }
- #endif
- }
- TString TStringBuilder(PCWSTR LpWideCharStr) {
- #ifdef _UNICODE
- return TString(LpWideCharStr);
- #else
- TString TStr;
- int RequiredSize = WideCharToMultiByte(CP_ACP,
- NULL,
- LpWideCharStr,
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- TStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CP_ACP,
- NULL,
- LpWideCharStr,
- -1,
- TStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (TStr.back() == '\x00')
- TStr.pop_back();
- return TStr;
- #endif
- }
- TString TStringBuilder(const std::string& RefMultiByteStr, UINT CodePage) {
- return TStringBuilder(RefMultiByteStr.c_str(), CodePage);
- }
- TString TStringBuilder(const std::wstring& RefWideCharStr) {
- return TStringBuilder(RefWideCharStr.c_str());
- }
- std::string TStringEncode(PTSTR LpTString, UINT CodePage) {
- std::string MultiByteStr;
- int RequiredSize;
- #ifdef _UNICODE
- RequiredSize = WideCharToMultiByte(CodePage,
- NULL,
- LpTString,
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- MultiByteStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CodePage,
- NULL,
- LpTString,
- -1,
- MultiByteStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (MultiByteStr.back() == '\x00')
- MultiByteStr.pop_back();
- #else
- if (CodePage == CP_ACP)
- return RefTStr;
- std::wstring WideCharStr;
- RequiredSize = MultiByteToWideChar(CP_ACP,
- NULL,
- LpTString,
- -1,
- NULL,
- 0);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- WideCharStr.resize(RequiredSize);
- if (!MultiByteToWideChar(CP_ACP,
- NULL,
- LpTString,
- -1,
- WideCharStr.data(),
- RequiredSize))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- while (WideCharStr.back() == L'\x00')
- WideCharStr.pop_back();
- RequiredSize = WideCharToMultiByte(CodePage,
- NULL,
- WideCharStr.c_str(),
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- MultiByteStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CodePage,
- NULL,
- WideCharStr.c_str(),
- -1,
- MultiByteStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (MultiByteStr.back() == '\x00')
- MultiByteStr.pop_back();
- #endif
- return MultiByteStr;
- }
- std::string TStringEncode(const TString& RefTStr, UINT CodePage) {
- std::string MultiByteStr;
- int RequiredSize;
- #ifdef _UNICODE
- RequiredSize = WideCharToMultiByte(CodePage,
- NULL,
- RefTStr.c_str(),
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- MultiByteStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CodePage,
- NULL,
- RefTStr.c_str(),
- -1,
- MultiByteStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (MultiByteStr.back() == '\x00')
- MultiByteStr.pop_back();
- #else
- if (CodePage == CP_ACP)
- return RefTStr;
- std::wstring WideCharStr;
- RequiredSize = MultiByteToWideChar(CP_ACP,
- NULL,
- RefTStr.c_str(),
- -1,
- NULL,
- 0);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- WideCharStr.resize(RequiredSize);
- if (!MultiByteToWideChar(CP_ACP,
- NULL,
- RefTStr.c_str(),
- -1,
- WideCharStr.data(),
- RequiredSize))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("MultiByteToWideChar failed."));
- while (WideCharStr.back() == L'\x00')
- WideCharStr.pop_back();
- RequiredSize = WideCharToMultiByte(CodePage,
- NULL,
- WideCharStr.c_str(),
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- if (RequiredSize == 0)
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- MultiByteStr.resize(RequiredSize);
- if (!WideCharToMultiByte(CodePage,
- NULL,
- WideCharStr.c_str(),
- -1,
- MultiByteStr.data(),
- RequiredSize,
- NULL,
- NULL))
- throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
- TEXT("WideCharToMultiByte failed."));
- while (MultiByteStr.back() == '\x00')
- MultiByteStr.pop_back();
- #endif
- return MultiByteStr;
- }
- TString TStringDecode(PCSTR LpMultiByteStr, UINT CodePage) {
- return TStringBuilder(LpMultiByteStr, CodePage);
- }
- TString TStringDecode(const std::string& RefMultiByteStr, UINT CodePage) {
- return TStringBuilder(RefMultiByteStr.c_str(), CodePage);
- }
|