AuroraRuntime/Source/Locale/Encoding/EncoderAdapter.cpp

138 lines
3.3 KiB
C++
Raw Normal View History

2021-09-06 10:58:08 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: EncoderAdapter.cpp
Date: 2021-8-19
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2021-09-06 10:58:08 +00:00
#include "../Locale.hpp"
#include "Encoding.hpp"
#include "EncoderIConv.hpp"
#include "EncoderNSL.hpp"
#include "ConvertInternal.hpp"
#include "EncoderAdapter.hpp"
namespace Aurora::Locale::Encoding
{
EncoderAdapter::EncoderAdapter()
{
}
EncoderAdapter::~EncoderAdapter()
{
}
void EncoderAdapter::Init(ECodePage page, bool decode)
{
this->page = page;
this->decode = decode;
}
AuStreamReadWrittenPair_t EncoderAdapter::CPToUTF8(void *in, AuUInt32 length, void *utf8, AuUInt32 utf8Max)
{
AuStreamReadWrittenPair_t ret {};
if (!length)
{
return {};
}
if (!in)
{
return {};
}
2021-09-06 10:58:08 +00:00
if (((page == ECodePage::eSysUnk) &&
(GetInternalCodePage() == ECodePage::eUTF8)) ||
(page == ECodePage::eUTF8))
{
auto readable = std::min(length, utf8Max);
2021-09-06 13:03:45 +00:00
if (utf8 && in)
{
std::memcpy(utf8, in, readable);
}
return {length, utf8 ? utf8Max : length};
2021-09-06 10:58:08 +00:00
}
ret = Win32CPToUTF8(page, in, length, utf8, utf8Max);
if (!ret.first)
{
ret = STLCPToUTF8(page, in, length, utf8, utf8Max);
}
return ret;
}
AuStreamReadWrittenPair_t EncoderAdapter::CPToUTF8(const void *in, AuUInt32 length, void *utf8, AuUInt32 utf8Max)
{
AuStreamReadWrittenPair_t ret {};
if (!length)
{
return {};
}
if (!in)
{
return {};
}
2021-09-06 10:58:08 +00:00
if (((page == ECodePage::eSysUnk) &&
(GetInternalCodePage() == ECodePage::eUTF8)) ||
(page == ECodePage::eUTF8))
{
auto readable = std::min(length, utf8Max);
2021-09-06 13:03:45 +00:00
if (utf8 && in)
{
std::memcpy(utf8, in, readable);
}
return {length, utf8 ? utf8Max : length};
2021-09-06 10:58:08 +00:00
}
ret = Win32CPToUTF8(page, in, length, utf8, utf8Max);
if (!ret.first)
{
ret = STLCPToUTF8(page, in, length, utf8, utf8Max);
}
return ret;
}
AuStreamReadWrittenPair_t EncoderAdapter::UTF8ToCp(const void *utf8, AuUInt32 utf8Length, void *cp, AuUInt32 cpLen)
{
AuStreamReadWrittenPair_t ret {};
if (!utf8)
{
return {};
}
if (!utf8Length)
{
return {};
}
2021-09-06 10:58:08 +00:00
if (((page == ECodePage::eSysUnk) &&
(GetInternalCodePage() == ECodePage::eUTF8)) ||
(page == ECodePage::eUTF8))
{
auto readable = std::min(utf8Length, cpLen);
2021-09-06 13:03:45 +00:00
if (utf8 && cp)
{
std::memcpy(cp, utf8, readable);
}
return {utf8Length, utf8 ? cpLen : utf8Length};
2021-09-06 10:58:08 +00:00
}
ret = Win32UTF8ToCp(page, utf8, utf8Length, cp, cpLen);
if (!ret.first)
{
ret = STLUTF8ToCp(page, utf8, utf8Length, cp, cpLen);
}
return ret;
}
}