File indexing completed on 2025-01-19 05:20:18

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007 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 "bytearraycharsstreamencoder.hpp"
0010 
0011 // lib
0012 #include <bytearrayview.hpp>
0013 // Okteta core
0014 #include <Okteta/AbstractByteArrayModel>
0015 #include <Okteta/Character>
0016 #include <Okteta/CharCodec>
0017 // KF
0018 #include <KLocalizedString>
0019 // Qt
0020 #include <QTextStream>
0021 
0022 namespace Kasten {
0023 
0024 CharsStreamEncoderSettings::CharsStreamEncoderSettings() = default;
0025 
0026 ByteArrayCharsStreamEncoder::ByteArrayCharsStreamEncoder()
0027     : AbstractByteArrayStreamEncoder(i18nc("name of the encoding target", "Characters"), QStringLiteral("text/plain"))
0028 {}
0029 
0030 ByteArrayCharsStreamEncoder::~ByteArrayCharsStreamEncoder() = default;
0031 
0032 bool ByteArrayCharsStreamEncoder::encodeDataToStream(QIODevice* device,
0033                                                      const ByteArrayView* byteArrayView,
0034                                                      const Okteta::AbstractByteArrayModel* byteArrayModel,
0035                                                      const Okteta::AddressRange& range)
0036 {
0037     bool success = true;
0038 
0039     // settings
0040     mSettings.codecName = byteArrayView->charCodingName();
0041     mSettings.undefinedChar = byteArrayView->undefinedChar();
0042     mSettings.substituteChar = byteArrayView->substituteChar();
0043 
0044     // encode
0045     QTextStream textStream(device);
0046 
0047     Okteta::CharCodec* charCodec = Okteta::CharCodec::createCodec(mSettings.codecName);
0048     const QChar tabChar = QLatin1Char('\t');
0049     const QChar returnChar = QLatin1Char('\n');
0050 
0051     for (Okteta::Address i = range.start(); i <= range.end(); ++i) {
0052         const Okteta::Character byteChar = charCodec->decode(byteArrayModel->byte(i));
0053 
0054         const QChar streamChar = byteChar.isUndefined() ?      mSettings.undefinedChar :
0055                                  (!byteChar.isPrint()
0056                                   || byteChar == tabChar
0057                                   || byteChar == returnChar) ? mSettings.substituteChar :
0058                                                                (QChar)byteChar;
0059         textStream << streamChar;
0060     }
0061 
0062     // clean up
0063     delete charCodec;
0064 
0065     return success;
0066 }
0067 
0068 }
0069 
0070 #include "moc_bytearraycharsstreamencoder.cpp"