File indexing completed on 2025-01-05 05:23:53

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 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 "charbytearraycolumnhtmlrenderer.hpp"
0010 
0011 // Okteta core
0012 #include <Okteta/AbstractByteArrayModel>
0013 #include <Okteta/CharCodec>
0014 #include <Okteta/Character>
0015 // Qt
0016 #include <QTextStream>
0017 
0018 namespace Kasten {
0019 
0020 CharByteArrayColumnHtmlRenderer::CharByteArrayColumnHtmlRenderer(
0021     const Okteta::AbstractByteArrayModel* byteArrayModel, Okteta::Address offset,
0022     const Okteta::CoordRange& coordRange,
0023     int noOfBytesPerLine,
0024     const QString& charCodecName, QChar substituteChar, QChar undefinedChar)
0025     : AbstractByteArrayColumnHtmlRenderer(byteArrayModel, offset, coordRange, noOfBytesPerLine)
0026     , mCharCodec(Okteta::CharCodec::createCodec(charCodecName))
0027     , mSubstituteChar(substituteChar)
0028     , mUndefinedChar(undefinedChar)
0029 {
0030 }
0031 
0032 CharByteArrayColumnHtmlRenderer::~CharByteArrayColumnHtmlRenderer() = default;
0033 
0034 void CharByteArrayColumnHtmlRenderer::renderLine(QTextStream* stream, bool isSubline) const
0035 {
0036     Q_UNUSED(isSubline)
0037 
0038     *stream << "<td nowrap><tt>";
0039 
0040     int p = 0;
0041     int pEnd = mNoOfBytesPerLine;
0042     // correct boundaries
0043     if (mRenderLine == mCoordRange.start().line()) {
0044         p = mCoordRange.start().pos();
0045     }
0046     if (mRenderLine == mCoordRange.end().line()) {
0047         pEnd = mCoordRange.end().pos() + 1;
0048     }
0049 
0050     const QChar tabChar = QLatin1Char('\t');
0051     const QChar returnChar = QLatin1Char('\n');
0052 
0053     *stream << whiteSpace(p);
0054 
0055     // draw individual chars
0056     for (; p < pEnd; ++p, ++mOffset) {
0057         const Okteta::Character byteChar = mCharCodec->decode(mByteArrayModel->byte(mOffset));
0058 
0059         const QChar streamChar = byteChar.isUndefined() ?      Okteta::Character(mUndefinedChar) :
0060                                  (!byteChar.isPrint()
0061                                   || byteChar == tabChar
0062                                   || byteChar == returnChar) ? Okteta::Character(mSubstituteChar) :
0063                                                                byteChar;
0064         *stream << htmlEscaped(streamChar);
0065     }
0066 
0067     *stream << whiteSpace(mNoOfBytesPerLine - p);
0068 
0069     ++mRenderLine;
0070 
0071     *stream << "</tt></td>";
0072 }
0073 
0074 }