File indexing completed on 2024-04-21 16:34:02

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003, 2008-2009 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 #ifndef OKTETA_BYTEARRAYTABLELAYOUT_HPP
0010 #define OKTETA_BYTEARRAYTABLELAYOUT_HPP
0011 
0012 // lib
0013 #include "oktetagui_export.hpp"
0014 #include "coordrange.hpp"
0015 // Okteta core
0016 #include <Okteta/AddressRange>
0017 
0018 // TODO: rename things like startOffset and firstLineOffset, not really descriptive
0019 namespace Okteta {
0020 
0021 /**@short the logical layout of a byte array table for a view
0022  *
0023  * Given the values for
0024  * * number of bytes per line,
0025  * * a possible offset of the first line displayed,
0026  * * a possible offset of the displayed bytearray, and
0027  * * length of the byte array as well as
0028  * * offset in the byte array
0029  * * the number of lines per page jump
0030  * the following values are calculated:
0031  * * starting line of display,
0032  * * starting position in this line,
0033  * * final line of display,
0034  * * final position in this line, and
0035  * * the total number of lines (is final line +1 or 0)
0036  *
0037  * This layout sees the buffer as a continuous stream of byte,
0038  * thus uses each line after the start from the begin to the end.
0039  *
0040  * If the buffer is empty the end coord will be set one pos left to the start coord
0041  * to easen the cursor handling.
0042  *
0043  * @author Friedrich W. H.  Kossebau
0044  */
0045 class OKTETAGUI_EXPORT ByteArrayTableLayout
0046 {
0047 public:
0048     ByteArrayTableLayout(Size noOfBytesPerLine, Address firstLineOffset, Address startOffset, Address byteArrayOffset, Size byteArrayLength);
0049     ByteArrayTableLayout() = delete;
0050     ByteArrayTableLayout(const ByteArrayTableLayout&) = delete;
0051 
0052     ~ByteArrayTableLayout();
0053 
0054     ByteArrayTableLayout& operator=(const ByteArrayTableLayout&) = delete;
0055 
0056 public: // given values
0057     /** */
0058     Address startOffset() const;
0059     /** */
0060     Address firstLineOffset() const;
0061     /** returns number of bytes per line */
0062     Size noOfBytesPerLine() const;
0063     /** returns the offset of the start of the displayed byte array section */
0064     Address byteArrayOffset() const;
0065     /** returns the length of the displayed byte array section */
0066     Size length() const;
0067     /** returns number of lines per visual page */
0068     LineSize noOfLinesPerPage() const;
0069 
0070 public: // calculated values
0071     Line startLine() const;
0072     LinePosition firstStartLinePosition() const;
0073     /** returns the coord of the start */
0074     Coord startCoord() const;
0075 
0076     Line finalLine() const;
0077     LinePosition lastFinalLinePosition() const;
0078     /** returns the coord of the end */
0079     Coord finalCoord() const;
0080 
0081     Address lastByteArrayOffset() const;
0082     /** tells how much lines this layout needs (incl. blank leading lines due to mStartOffset and mFirstLineOffset) */
0083     LineSize noOfLines() const;
0084 
0085 public: // value calculation service
0086     /** calculates the index of the coord
0087      * If the coord is before the first coord the first index is returned,
0088      * if the coord is behind the last coord the last index is returned
0089      */
0090     Address indexAtCCoord(const Coord& coord) const;
0091     /** calculates the index of the first pos in line.
0092      * If the line is below the first line the first index is returned,
0093      * if the line is above the last line the last index is returned
0094      */
0095     Address indexAtCFirstLinePosition(Line line) const;
0096     /** calculates the index of last pos in line
0097      * If the line is below the first line the first index is returned,
0098      * if the line is above the last line the last index is returned
0099      */
0100     Address indexAtCLastLinePosition(Line line) const;
0101     /** calculates the line in which index is found
0102      * If the index is below the first index the first line is returned,
0103      * if the index is above the last index the last line is returned
0104      */
0105     Line lineAtCIndex(Address index) const;
0106     /** calculates the coord in which index is found
0107      * If the index is below the first index the first coord is returned,
0108      * if the index is above the last index the last coord is returned
0109      */
0110     Coord coordOfCIndex(Address index) const;
0111 
0112     /** calculates the index of coord. if coord is invalid the behaviour is undefinded */
0113     Address indexAtCoord(const Coord& coord) const;
0114     /** calculates the index of the first pos in line. if line is invalid the behaviour is undefinded */
0115     Address indexAtFirstLinePosition(Line line) const;
0116     /** calculates the index of last pos in line. if line is invalid the behaviour is undefinded */
0117     Address indexAtLastLinePosition(Line line) const;
0118     /** calculates the line in which index is found. if index is invalid the behaviour is undefinded */
0119     Line lineAtIndex(Address index) const;
0120     /** calculates the coord in which index is found. if index is invalid the behaviour is undefinded */
0121     Coord coordOfIndex(Address index) const;
0122     /** calculates the range of coords in which the indizes are found. if indizes are invalid the behaviour is undefinded */
0123     CoordRange coordRangeOfIndizes(const AddressRange& indizes) const;
0124 
0125     /** returns the used positions in line */
0126     LinePositionRange linePositions(Line line) const;
0127     /** returns the first Pos in line. if line is invalid the behaviour is undefinded */
0128     LinePosition firstLinePosition(Line line) const;
0129     /** returns the last Pos in line. if line is invalid the behaviour is undefinded */
0130     LinePosition lastLinePosition(Line line) const;
0131     /** returns the valid Pos or the first Pos in line. if coord is invalid the behaviour is undefinded */
0132     LinePosition firstLinePosition(const Coord& coord) const;
0133     /** returns the valid Pos or the last Pos in line. if coord is invalid the behaviour is undefinded */
0134     LinePosition lastLinePosition(const Coord& coord) const;
0135     /** returns true if the line has content */
0136     bool hasContent(Line line) const;
0137     /** returns true if the coord is the first in it's line. if coord is invalid the behaviour is undefinded */
0138     bool atFirstLinePosition(const Coord& coord) const;
0139     /** returns true if the coord is the last in it's line. if coord is invalid the behaviour is undefinded */
0140     bool atLastLinePosition(const Coord& coord) const;
0141 
0142     /** returns the index if valid or the nearest valid index */
0143     Address correctIndex(Address index) const;
0144     /** returns the coord if valid or the nearest valid coord */
0145     Coord correctCoord(const Coord& coord) const;
0146 
0147 public: // modification access; return true if changes
0148     /** sets mStartOffset, returns true if changed */
0149     bool setStartOffset(Address startOffset);
0150     /** sets mStartOffset, returns true if changed */
0151     bool setFirstLineOffset(Address firstLineOffset);
0152     /** sets number of bytes per line, returns true if changed */
0153     bool setNoOfBytesPerLine(LineSize noOfBytesPerLine);
0154     /** sets offset in the data to display, returns true if changed */
0155     bool setByteArrayOffset(Address byteArrayOffset);
0156     /** sets length of data to display, returns true if changed */
0157     bool setLength(Size length);
0158     /** sets number of lines per page, 1 as default */
0159     void setNoOfLinesPerPage(LineSize noOfLinesPerPage);
0160 
0161 private:
0162     /** calculates the start coord by startoffset and number of bytes per line */
0163     OKTETAGUI_NO_EXPORT void calcStart();
0164     /** calculates the final coord by startoffset, length, and number of bytes per line */
0165     OKTETAGUI_NO_EXPORT void calcEnd();
0166 
0167 private:
0168     /** how many chars per line */
0169     Size mNoOfBytesPerLine;
0170     /** starting offset of the first displayed line */
0171     Address mFirstLineOffset;
0172     /** starting offset of the displayed bytearray */
0173     Address mStartOffset;
0174     /** */
0175     Address mRelativeStartOffset;
0176     /** offset in the given bytearray */
0177     Address mByteArrayOffset;
0178     /** last offset in the displayed bytearray section */
0179     Address mLastByteArrayOffset;
0180     /** number of lines that are moved by page up/down */
0181     LineSize mNoOfLinesPerPage;
0182 
0183 private: // calculated values, buffered
0184     /** */
0185     CoordRange mCoordRange;
0186 };
0187 
0188 inline Address ByteArrayTableLayout::startOffset()         const { return mStartOffset; }
0189 inline Address ByteArrayTableLayout::firstLineOffset()     const { return mFirstLineOffset; }
0190 inline Size ByteArrayTableLayout::noOfBytesPerLine()       const { return mNoOfBytesPerLine; }
0191 inline Address ByteArrayTableLayout::byteArrayOffset()     const { return mByteArrayOffset; }
0192 inline Size ByteArrayTableLayout::length()                 const { return mLastByteArrayOffset - mByteArrayOffset + 1; }
0193 inline Address ByteArrayTableLayout::lastByteArrayOffset() const { return mLastByteArrayOffset; }
0194 
0195 inline Coord ByteArrayTableLayout::finalCoord()               const { return mCoordRange.end(); }
0196 inline Coord ByteArrayTableLayout::startCoord()               const { return mCoordRange.start(); }
0197 inline LinePosition ByteArrayTableLayout::firstStartLinePosition() const { return startCoord().pos(); }
0198 inline LinePosition ByteArrayTableLayout::lastFinalLinePosition() const { return finalCoord().pos(); }
0199 inline Line ByteArrayTableLayout::startLine()                 const { return startCoord().line(); }
0200 inline Line ByteArrayTableLayout::finalLine()                 const { return finalCoord().line(); }
0201 inline LineSize ByteArrayTableLayout::noOfLinesPerPage()      const { return mNoOfLinesPerPage; }
0202 inline LineSize ByteArrayTableLayout::noOfLines()             const { return mByteArrayOffset > mLastByteArrayOffset ? 0 : finalLine() + 1; }
0203 
0204 }
0205 
0206 #endif