/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: EncoderAdapter.cpp Date: 2021-8-19 Author: Reece ***/ #include #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 {}; } if (((page == ECodePage::eSysUnk) && (GetInternalCodePage() == ECodePage::eUTF8)) || (page == ECodePage::eUTF8)) { auto readable = std::min(length, utf8Max); if (utf8 && in) { std::memcpy(utf8, in, readable); } return {length, utf8 ? utf8Max : length}; } 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 {}; } if (((page == ECodePage::eSysUnk) && (GetInternalCodePage() == ECodePage::eUTF8)) || (page == ECodePage::eUTF8)) { auto readable = std::min(length, utf8Max); if (utf8 && in) { std::memcpy(utf8, in, readable); } return {length, utf8 ? utf8Max : length}; } 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 {}; } if (((page == ECodePage::eSysUnk) && (GetInternalCodePage() == ECodePage::eUTF8)) || (page == ECodePage::eUTF8)) { auto readable = std::min(utf8Length, cpLen); if (utf8 && cp) { std::memcpy(cp, utf8, readable); } return {utf8Length, utf8 ? cpLen : utf8Length}; } ret = Win32UTF8ToCp(page, utf8, utf8Length, cp, cpLen); if (!ret.first) { ret = STLUTF8ToCp(page, utf8, utf8Length, cp, cpLen); } return ret; } }