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: 2003, 2008 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 "charbytearraycolumntextrenderer.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 CharByteArrayColumnTextRenderer::CharByteArrayColumnTextRenderer(
0021     const Okteta::AbstractByteArrayModel* byteArrayModel, Okteta::Address offset,
0022     const Okteta::CoordRange& coordRange,
0023     int noOfBytesPerLine, int byteSpacingWidth, int noOfGroupedBytes,
0024     const QString& charCodecName, QChar substituteChar, QChar undefinedChar)
0025     : AbstractByteArrayColumnTextRenderer(byteArrayModel, offset, coordRange, noOfBytesPerLine)
0026     , mCharCodec(Okteta::CharCodec::createCodec(charCodecName))
0027     , mSubstituteChar(substituteChar)
0028     , mUndefinedChar(undefinedChar)
0029 {
0030     setWidths(1, byteSpacingWidth, noOfGroupedBytes);
0031 }
0032 
0033 CharByteArrayColumnTextRenderer::~CharByteArrayColumnTextRenderer()
0034 {
0035     delete mCharCodec;
0036 }
0037 
0038 void CharByteArrayColumnTextRenderer::renderLine(QTextStream* stream, bool isSubline) const
0039 {
0040     Q_UNUSED(isSubline)
0041 
0042     int p = 0;
0043     int pEnd = mNoOfBytesPerLine;
0044     // correct boundaries
0045     if (mRenderLine == mCoordRange.start().line()) {
0046         p = mCoordRange.start().pos();
0047     }
0048     if (mRenderLine == mCoordRange.end().line()) {
0049         pEnd = mCoordRange.end().pos() + 1;
0050     }
0051 
0052     const QChar tabChar = QLatin1Char('\t');
0053     const QChar returnChar = QLatin1Char('\n');
0054 
0055     // draw individual chars
0056     uint e = 0;
0057     for (; p < pEnd; ++p, ++mOffset) {
0058         // get next position
0059         const uint t = mLinePositions[p];
0060         // clear spacing
0061         *stream << whiteSpace(t - e);
0062 
0063         // print char
0064         const Okteta::Character byteChar = mCharCodec->decode(mByteArrayModel->byte(mOffset));
0065 
0066         const QChar streamChar = byteChar.isUndefined() ?      Okteta::Character(mUndefinedChar) :
0067                                  (!byteChar.isPrint()
0068                                   || byteChar == tabChar
0069                                   || byteChar == returnChar) ? Okteta::Character(mSubstituteChar) :
0070                                                                byteChar;
0071         *stream << streamChar;
0072 
0073         e = t + 1;
0074     }
0075 
0076     *stream << whiteSpace(mNoOfCharsPerLine - e);
0077 
0078     ++mRenderLine;
0079 }
0080 
0081 }