File indexing completed on 2024-05-19 05:28:14

0001 /*
0002     This file is part of Konsole, an X terminal.
0003     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0004 
0005     Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301  USA.
0018 */
0019 
0020 #ifndef BLOCKARRAY_H
0021 #define BLOCKARRAY_H
0022 
0023 #include <cstdio>
0024 #include <unistd.h>
0025 
0026 constexpr auto QTERMWIDGET_BLOCKSIZE = 1 << 12;
0027 constexpr auto ENTRIES = ((QTERMWIDGET_BLOCKSIZE - sizeof(size_t)) / sizeof(unsigned char));
0028 
0029 namespace Konsole
0030 {
0031 
0032 struct Block {
0033     constexpr Block()
0034     {
0035         size = 0;
0036     }
0037     unsigned char data[ENTRIES];
0038     size_t size;
0039 };
0040 
0041 // ///////////////////////////////////////////////////////
0042 
0043 class BlockArray
0044 {
0045 public:
0046     /**
0047      * Creates a history file for holding
0048      * maximal size blocks. If more blocks
0049      * are requested, then it drops earlier
0050      * added ones.
0051      */
0052     BlockArray();
0053 
0054     /// destructor
0055     ~BlockArray();
0056 
0057     /**
0058      * adds the Block at the end of history.
0059      * This may drop other blocks.
0060      *
0061      * The ownership on the block is transfered.
0062      * An unique index number is returned for accessing
0063      * it later (if not yet dropped then)
0064      *
0065      * Note, that the block may be dropped completely
0066      * if history is turned off.
0067      */
0068     size_t append(Block *block);
0069 
0070     /**
0071      * gets the block at the index. Function may return
0072      * 0 if the block isn't available any more.
0073      *
0074      * The returned block is strictly readonly as only
0075      * maped in memory - and will be invalid on the next
0076      * operation on this class.
0077      */
0078     const Block *at(size_t index);
0079 
0080     /**
0081      * reorders blocks as needed. If newsize is null,
0082      * the history is emptied completely. The indices
0083      * returned on append won't change their semantic,
0084      * but they may not be valid after this call.
0085      */
0086     bool setHistorySize(size_t newsize);
0087 
0088     size_t newBlock();
0089 
0090     Block *lastBlock() const;
0091 
0092     /**
0093      * Convenient function to set the size in KBytes
0094      * instead of blocks
0095      */
0096     bool setSize(size_t newsize);
0097 
0098     size_t len() const
0099     {
0100         return length;
0101     }
0102 
0103     bool has(size_t index) const;
0104 
0105     size_t getCurrent() const
0106     {
0107         return current;
0108     }
0109 
0110 private:
0111     void unmap();
0112     void increaseBuffer();
0113     void decreaseBuffer(size_t newsize);
0114     void moveBlock(FILE *fion, int cursor, int newpos, char *buffer2);
0115 
0116     size_t size;
0117     // current always shows to the last inserted block
0118     size_t current;
0119     size_t index;
0120 
0121     Block *lastmap;
0122     size_t lastmap_index;
0123     Block *lastblock;
0124 
0125     int ion;
0126     size_t length;
0127 
0128     int blocksize = 0;
0129 };
0130 
0131 }
0132 
0133 #endif