| 1 | /* |
| 2 | This file is part of Konsole, an X terminal. |
| 3 | Copyright (C) 2000 by Stephan Kulow <coolo@kde.org> |
| 4 | |
| 5 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef BLOCKARRAY_H |
| 24 | #define BLOCKARRAY_H |
| 25 | |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | //#error Do not use in KDE 2.1 |
| 29 | |
| 30 | #define BlockSize (1 << 12) |
| 31 | #define ENTRIES ((BlockSize - sizeof(size_t) ) / sizeof(unsigned char)) |
| 32 | |
| 33 | namespace Konsole { |
| 34 | |
| 35 | struct Block { |
| 36 | Block() { |
| 37 | size = 0; |
| 38 | } |
| 39 | unsigned char data[ENTRIES]; |
| 40 | size_t size; |
| 41 | }; |
| 42 | |
| 43 | // /////////////////////////////////////////////////////// |
| 44 | |
| 45 | class BlockArray { |
| 46 | public: |
| 47 | /** |
| 48 | * Creates a history file for holding |
| 49 | * maximal size blocks. If more blocks |
| 50 | * are requested, then it drops earlier |
| 51 | * added ones. |
| 52 | */ |
| 53 | BlockArray(); |
| 54 | |
| 55 | /// destructor |
| 56 | ~BlockArray(); |
| 57 | |
| 58 | /** |
| 59 | * adds the Block at the end of history. |
| 60 | * This may drop other blocks. |
| 61 | * |
| 62 | * The ownership on the block is transfered. |
| 63 | * An unique index number is returned for accessing |
| 64 | * it later (if not yet dropped then) |
| 65 | * |
| 66 | * Note, that the block may be dropped completely |
| 67 | * if history is turned off. |
| 68 | */ |
| 69 | size_t append(Block * block); |
| 70 | |
| 71 | /** |
| 72 | * gets the block at the index. Function may return |
| 73 | * 0 if the block isn't available any more. |
| 74 | * |
| 75 | * The returned block is strictly readonly as only |
| 76 | * maped in memory - and will be invalid on the next |
| 77 | * operation on this class. |
| 78 | */ |
| 79 | const Block * at(size_t index); |
| 80 | |
| 81 | /** |
| 82 | * reorders blocks as needed. If newsize is null, |
| 83 | * the history is emptied completely. The indices |
| 84 | * returned on append won't change their semantic, |
| 85 | * but they may not be valid after this call. |
| 86 | */ |
| 87 | bool setHistorySize(size_t newsize); |
| 88 | |
| 89 | size_t newBlock(); |
| 90 | |
| 91 | Block * lastBlock() const; |
| 92 | |
| 93 | /** |
| 94 | * Convenient function to set the size in KBytes |
| 95 | * instead of blocks |
| 96 | */ |
| 97 | bool setSize(size_t newsize); |
| 98 | |
| 99 | size_t len() const { |
| 100 | return length; |
| 101 | } |
| 102 | |
| 103 | bool has(size_t index) const; |
| 104 | |
| 105 | size_t getCurrent() const { |
| 106 | return current; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | void unmap(); |
| 111 | void increaseBuffer(); |
| 112 | void decreaseBuffer(size_t newsize); |
| 113 | |
| 114 | size_t size; |
| 115 | // current always shows to the last inserted block |
| 116 | size_t current; |
| 117 | size_t index; |
| 118 | |
| 119 | Block * lastmap; |
| 120 | size_t lastmap_index; |
| 121 | Block * lastblock; |
| 122 | |
| 123 | int ion; |
| 124 | size_t length; |
| 125 | |
| 126 | }; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |