2021-10-24 22:04:23 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//# {{
|
|
|
|
#include "preprocessor.h"
|
|
|
|
#if !TOML_IMPLEMENTATION
|
|
|
|
#error This is an implementation-only header.
|
|
|
|
#endif
|
|
|
|
//# }}
|
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
#if TOML_ENABLE_WINDOWS_COMPAT
|
2021-10-24 22:04:23 +00:00
|
|
|
#include "std_string.h"
|
|
|
|
#include "header_start.h"
|
|
|
|
|
|
|
|
#ifndef _WINDOWS_
|
|
|
|
#if TOML_INCLUDE_WINDOWS_H
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
|
|
|
|
extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int CodePage,
|
|
|
|
unsigned long dwFlags,
|
|
|
|
const wchar_t* lpWideCharStr,
|
|
|
|
int cchWideChar,
|
|
|
|
char* lpMultiByteStr,
|
|
|
|
int cbMultiByte,
|
|
|
|
const char* lpDefaultChar,
|
|
|
|
int* lpUsedDefaultChar);
|
|
|
|
|
|
|
|
extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage,
|
|
|
|
unsigned long dwFlags,
|
|
|
|
const char* lpMultiByteStr,
|
|
|
|
int cbMultiByte,
|
|
|
|
wchar_t* lpWideCharStr,
|
|
|
|
int cchWideChar);
|
|
|
|
|
|
|
|
#endif // TOML_INCLUDE_WINDOWS_H
|
|
|
|
#endif // _WINDOWS_
|
|
|
|
|
|
|
|
TOML_IMPL_NAMESPACE_START
|
|
|
|
{
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-26 18:03:56 +00:00
|
|
|
std::string narrow(std::wstring_view str)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
const auto len =
|
|
|
|
::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);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-26 18:03:56 +00:00
|
|
|
std::wstring widen(std::string_view str)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::wstring s;
|
|
|
|
const auto len = ::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);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TOML_HAS_CHAR8
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-26 18:03:56 +00:00
|
|
|
std::wstring widen(std::u8string_view str)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return widen(std::string_view{ reinterpret_cast<const char*>(str.data()), str.length() });
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TOML_HAS_CHAR8
|
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
|
|
|
|
#include "header_end.h"
|
2021-10-30 14:26:05 +00:00
|
|
|
#endif // TOML_ENABLE_WINDOWS_COMPAT
|