File indexing completed on 2024-06-23 05:48:51
0001 /* 0002 This file is part of the Okteta Kasten module, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2009, 2023 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "utf16codec.hpp" 0010 0011 // tool 0012 #include "../types/utf16.hpp" 0013 #include "../poddata.hpp" 0014 // KF 0015 #include <KLocalizedString> 0016 // Qt 0017 #include <QTextCodec> 0018 #include <QTextEncoder> 0019 #include <QTextDecoder> 0020 0021 namespace Okteta { 0022 0023 Utf16Codec::Utf16Codec() 0024 : AbstractTypeCodec(i18nc("@label:textbox", "UTF-16")) 0025 { 0026 auto* utf16Codec = QTextCodec::codecForName("UTF-16"); 0027 mUtf16Encoder.reset(utf16Codec->makeEncoder(QTextCodec::IgnoreHeader)); 0028 mUtf16Decoder.reset(utf16Codec->makeDecoder(QTextCodec::IgnoreHeader)); 0029 } 0030 0031 Utf16Codec::~Utf16Codec() = default; 0032 0033 QVariant Utf16Codec::value(const PODData& data, int* byteCount) const 0034 { 0035 const char* pointer = (char*)data.pointer(2); 0036 0037 if (pointer) { 0038 const QString utf16 = mUtf16Decoder->toUnicode(pointer, 2); 0039 if (utf16.size() == 1 && utf16[0] != QChar(QChar::ReplacementCharacter)) { 0040 *byteCount = 2; 0041 return QVariant::fromValue<Utf16>(Utf16(utf16[0])); 0042 } 0043 } 0044 *byteCount = 0; 0045 return QVariant(); 0046 } 0047 0048 QByteArray Utf16Codec::valueToBytes(const QVariant& value) const 0049 { 0050 const QChar valueChar = value.value<Utf16>().value; 0051 0052 return mUtf16Encoder->fromUnicode(valueChar); 0053 } 0054 0055 bool Utf16Codec::areEqual(const QVariant& value, QVariant& otherValue) const 0056 { 0057 return (value.value<Utf16>().value == otherValue.value<Utf16>().value); 0058 } 0059 0060 }