File indexing completed on 2024-05-05 17:57:59

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2022 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 "ibm874charcodec.hpp"
0010 
0011 // lib
0012 #include <character.hpp>
0013 // Qt
0014 #include <QString>
0015 
0016 namespace Okteta {
0017 
0018 bool IBM874CharCodec::encode(Byte* byte, const QChar& _char) const
0019 {
0020     const ushort unicodeValue = _char.unicode();
0021 
0022     if (unicodeValue <= 0x007f) {
0023         *byte = unicodeValue;
0024         return true;
0025     }
0026 
0027     constexpr unsigned char unmatched = 0xFF; // value not in target set
0028     const unsigned char _byte =
0029         (0x0E01 <= unicodeValue) && (unicodeValue <= 0x0E3A) ? (unicodeValue - 0x0E01 + 0xA1) :
0030         (0x0E3F <= unicodeValue) && (unicodeValue <= 0x0E5B) ? (unicodeValue - 0x0E3F + 0xDF) :
0031         (unicodeValue == 0x20AC) ?                              0x80 :
0032         (unicodeValue == 0x00A0) ?                              0xA0 :
0033         /* else */                                              unmatched;
0034 
0035     // not covered?
0036     if (_byte == unmatched) {
0037         return false;
0038     }
0039 
0040     *byte = _byte;
0041 
0042     return true;
0043 }
0044 
0045 Character IBM874CharCodec::decode(Byte byte) const
0046 {
0047     const ushort unicode =
0048         (byte <= 0x7f)
0049             ?  ushort(byte) :
0050         (0x80 == byte)
0051             ?  ushort(0x20AC) :
0052         (0xA0 == byte)
0053             ?  ushort(0x00A0) :
0054         (0xA1 <= byte && byte <= 0xDA)
0055             ? ushort(0x0E01 - 0xA1 + byte) :
0056         (0xDF <= byte && byte <= 0xFB)
0057             ? ushort(0x0E3F - 0xDF + byte) :
0058         /* else */ 0xFFFD;
0059 
0060     if (unicode == 0xFFFD) {
0061         return {QChar(0), true};
0062     }
0063 
0064     return {QChar(unicode)};
0065 }
0066 
0067 bool IBM874CharCodec::canEncode(const QChar& _char) const
0068 {
0069     const ushort charUnicode = _char.unicode();
0070     return
0071         (charUnicode <= 0x007f) ||
0072         (0x0E01 <= charUnicode && charUnicode <= 0x0E3A) ||
0073         (0x0E3F <= charUnicode && charUnicode <= 0x0E5B) ||
0074         (charUnicode == 0x20AC) || (charUnicode == 0x00A0);
0075 }
0076 
0077 const QString& IBM874CharCodec::name() const
0078 {
0079     return codecName();
0080 }
0081 
0082 const QString& IBM874CharCodec::codecName()
0083 {
0084     static const QString name = QStringLiteral("IBM874");
0085     return name;
0086 }
0087 
0088 }