1 /* 2 This file is part of the Okteta Kasten module, made within the KDE community. 3 4 SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org> 5 6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 7 */ 8 9 #include "utf8codec.hpp" 10 11 // tool 12 #include "../types/utf8.hpp" 13 #include "../poddata.hpp" 14 // KF 15 #include <KLocalizedString> 16 // Qt 17 #include <QTextCodec> 18 19 namespace Okteta { 20 Utf8Codec()21Utf8Codec::Utf8Codec() 22 : AbstractTypeCodec(i18nc("@label:textbox", "UTF-8")) 23 , mUtf8Codec(QTextCodec::codecForName("UTF-8")) 24 {} 25 26 Utf8Codec::~Utf8Codec() = default; 27 value(const PODData & data,int * byteCount) const28QVariant Utf8Codec::value(const PODData& data, int* byteCount) const 29 { 30 // UTF-8 31 // interpreted as a sequence of bytes, there is no endian problem 32 // source: https://unicode.org/faq/utf_bom.html#3 33 const QChar replacementChar(QChar::ReplacementCharacter); 34 const char* originalData = (const char*)data.originalData(); 35 const int maxUtf8DataSize = data.size(); 36 37 QString utf8; 38 bool isUtf8 = false; 39 *byteCount = 0; 40 for (int i = 1; i <= maxUtf8DataSize; ++i) { 41 utf8 = mUtf8Codec->toUnicode(originalData, i); 42 if (utf8.size() == 1 && utf8[0] != replacementChar) { 43 isUtf8 = true; 44 *byteCount = i; 45 break; 46 } 47 } 48 49 return isUtf8 ? QVariant::fromValue<Utf8>(Utf8(utf8[0])) : QVariant(); 50 } 51 valueToBytes(const QVariant & value) const52QByteArray Utf8Codec::valueToBytes(const QVariant& value) const 53 { 54 const QChar valueChar = value.value<Utf8>().value; 55 56 return mUtf8Codec->fromUnicode(valueChar); 57 } 58 areEqual(const QVariant & value,QVariant & otherValue) const59bool Utf8Codec::areEqual(const QVariant& value, QVariant& otherValue) const 60 { 61 return (value.value<Utf8>().value == otherValue.value<Utf8>().value); 62 } 63 64 } 65