Skip to content

Commit

Permalink
Bypass including Windows.h (MultiByteToWideChar/WideCharToMultiByte) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastLe9enD authored May 10, 2021
1 parent 369b36d commit 1ab8d3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
- **[@traversaro](https://github.com/traversaro)** - Added vcpkg support and reported a bunch of bugs
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
- **[@beastle9end](https://github.com/beastle9end)** - Made Windows.h include bypass
<br>
# Contact
Expand Down
21 changes: 14 additions & 7 deletions include/toml++/toml_default_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,16 @@ TOML_NAMESPACE_END;
// implementations of windows wide string nonsense
#if TOML_WINDOWS_COMPAT

TOML_DISABLE_WARNINGS;
#include <windows.h> // fuckkkk :(
TOML_ENABLE_WARNINGS;
namespace winapi
{
extern "C"
{
int __stdcall WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar,
const char* lpMultiByteStr, int cbMultiByte, const char* lpDefaultChar, int* lpUsedDefaultChar);
int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte,
const wchar_t* lpWideCharStr, int cchWideChar);
}
}

TOML_IMPL_NAMESPACE_START
{
Expand All @@ -218,13 +225,13 @@ TOML_IMPL_NAMESPACE_START
return {};

std::string s;
const auto len = WideCharToMultiByte(
const auto len = winapi::WideCharToMultiByte(
65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr
);
if (len)
{
s.resize(static_cast<size_t>(len));
WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len, nullptr, nullptr);
winapi::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len, nullptr, nullptr);
}
return s;
}
Expand All @@ -237,11 +244,11 @@ TOML_IMPL_NAMESPACE_START
return {};

std::wstring s;
const auto len = MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
const auto len = winapi::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
if (len)
{
s.resize(static_cast<size_t>(len));
MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
winapi::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
}
return s;
}
Expand Down
21 changes: 14 additions & 7 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11963,9 +11963,16 @@ TOML_NAMESPACE_END;
// implementations of windows wide string nonsense
#if TOML_WINDOWS_COMPAT

TOML_DISABLE_WARNINGS;
#include <windows.h> // fuckkkk :(
TOML_ENABLE_WARNINGS;
namespace winapi
{
extern "C"
{
int __stdcall WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar,
const char* lpMultiByteStr, int cbMultiByte, const char* lpDefaultChar, int* lpUsedDefaultChar);
int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte,
const wchar_t* lpWideCharStr, int cchWideChar);
}
}

TOML_IMPL_NAMESPACE_START
{
Expand All @@ -11977,13 +11984,13 @@ TOML_IMPL_NAMESPACE_START
return {};

std::string s;
const auto len = WideCharToMultiByte(
const auto len = winapi::WideCharToMultiByte(
65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr
);
if (len)
{
s.resize(static_cast<size_t>(len));
WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len, nullptr, nullptr);
winapi::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len, nullptr, nullptr);
}
return s;
}
Expand All @@ -11996,11 +12003,11 @@ TOML_IMPL_NAMESPACE_START
return {};

std::wstring s;
const auto len = MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
const auto len = winapi::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
if (len)
{
s.resize(static_cast<size_t>(len));
MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
winapi::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
}
return s;
}
Expand Down

0 comments on commit 1ab8d3d

Please sign in to comment.