Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Platform/Windows/CodePage.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#if qStroika_Foundation_Common_Platform_Windows
7#include <Windows.h>
8#else
9#error "WINDOWS REQUIRED FOR THIS MODULE"
10#endif
11
13#include "Stroika/Foundation/Execution/Throw.h"
14
15#include "CodePage.h"
16
17using namespace Stroika::Foundation;
20
21/*
22 ********************************************************************************
23 *************** Characters::Platform::Windows::BSTRStringToUTF8 ****************
24 ********************************************************************************
25 */
26string Characters::Platform::Windows::BSTRStringToUTF8 (const BSTR bstr)
27{
28 if (bstr == nullptr) {
29 return string{};
30 }
31 else {
32 int srcStrLen = ::SysStringLen (bstr);
33 int stringLength = ::WideCharToMultiByte (WellKnownCodePages::kUTF8, 0, bstr, srcStrLen, nullptr, 0, nullptr, nullptr);
34 string result;
35 result.resize (stringLength);
36 Verify (::WideCharToMultiByte (WellKnownCodePages::kUTF8, 0, bstr, srcStrLen, Containers::Start (result), stringLength, nullptr,
37 nullptr) == stringLength);
38 return result;
39 }
40}
41
42/*
43 ********************************************************************************
44 *************** Characters::Platform::Windows::UTF8StringToBSTR ****************
45 ********************************************************************************
46 */
47BSTR Characters::Platform::Windows::UTF8StringToBSTR (const char* ws)
48{
49 RequireNotNull (ws);
50 size_t wsLen = ::strlen (ws);
51 int stringLength = ::MultiByteToWideChar (CP_UTF8, 0, ws, static_cast<int> (wsLen), nullptr, 0);
52 BSTR result = ::SysAllocStringLen (nullptr, stringLength);
53 if (result == nullptr) [[unlikely]] {
54 Execution::Throw (bad_alloc{});
55 }
56 Verify (::MultiByteToWideChar (WellKnownCodePages::kUTF8, 0, ws, static_cast<int> (wsLen), result, stringLength) == stringLength);
57 return result;
58}
59
60/*
61 ********************************************************************************
62 ******************* Characters::Platform::Windows::BSTR2wstring ****************
63 ********************************************************************************
64 */
65wstring Characters::Platform::Windows::BSTR2wstring (VARIANT b)
66{
67 if (b.vt == VT_BSTR) {
68 return BSTR2wstring (b.bstrVal);
69 }
70 else {
71 return wstring{};
72 }
73}
74
75/*
76 ********************************************************************************
77 **************************** PlatformCodePageConverter *************************
78 ********************************************************************************
79 */
80void PlatformCodePageConverter::MapToUNICODE (const char* inMBChars, size_t inMBCharCnt, wchar_t* outChars, size_t* outCharCnt) const
81{
82 Require (inMBCharCnt == 0 or inMBChars != nullptr);
83 RequireNotNull (outCharCnt);
84 Require (*outCharCnt == 0 or outChars != nullptr);
85 // *outCharCnt = ::MultiByteToWideChar (fCodePage, MB_ERR_INVALID_CHARS, inMBChars, inMBCharCnt, outChars, *outCharCnt);
86 *outCharCnt = ::MultiByteToWideChar (fCodePage_, 0, inMBChars, static_cast<int> (inMBCharCnt), outChars, static_cast<int> (*outCharCnt));
87#if 0
88// enable to debug cases (e.g. caused when you read a CRLF file with fstream
89// in text mode, and get - somehow - stuff that triggers this ??? - with convert to
90// xml???). Anyhow - get error#102 - BUF_NOT_BIG-ENUF or osmeting like that...
91//
92// Debug when its happening again -- LGP 2008-09-02
93 if (*outCharCnt == 0) {
94 DWORD x = GetLastError ();
95 int breaker = 1;
96 }
97#endif
98}
99
100void PlatformCodePageConverter::MapFromUNICODE (const wchar_t* inChars, size_t inCharCnt, char* outChars, size_t* outCharCnt) const
101{
102 Require (inCharCnt == 0 or inChars != nullptr);
103 RequireNotNull (outCharCnt);
104 Require (*outCharCnt == 0 or outChars != nullptr);
105 *outCharCnt =
106 ::WideCharToMultiByte (fCodePage_, 0, inChars, static_cast<int> (inCharCnt), outChars, static_cast<int> (*outCharCnt), nullptr, nullptr);
107}
#define RequireNotNull(p)
Definition Assertions.h:347
#define Verify(c)
Definition Assertions.h:419
CONTAINER::value_type * Start(CONTAINER &c)
For a contiguous container (such as a vector or basic_string) - find the pointer to the start of the ...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43