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 | |
---|
36 | struct Block { |
---|
37 | Block() { size = 0; } |
---|
38 | unsigned char data[ENTRIES]; |
---|
39 | size_t size; |
---|
40 | }; |
---|
41 | |
---|
42 | // /////////////////////////////////////////////////////// |
---|
43 | |
---|
44 | class BlockArray { |
---|
45 | public: |
---|
46 | /** |
---|
47 | * Creates a history file for holding |
---|
48 | * maximal size blocks. If more blocks |
---|
49 | * are requested, then it drops earlier |
---|
50 | * added ones. |
---|
51 | */ |
---|
52 | BlockArray(); |
---|
53 | |
---|
54 | /// destructor |
---|
55 | ~BlockArray(); |
---|
56 | |
---|
57 | /** |
---|
58 | * adds the Block at the end of history. |
---|
59 | * This may drop other blocks. |
---|
60 | * |
---|
61 | * The ownership on the block is transfered. |
---|
62 | * An unique index number is returned for accessing |
---|
63 | * it later (if not yet dropped then) |
---|
64 | * |
---|
65 | * Note, that the block may be dropped completely |
---|
66 | * if history is turned off. |
---|
67 | */ |
---|
68 | size_t append(Block *block); |
---|
69 | |
---|
70 | /** |
---|
71 | * gets the block at the index. Function may return |
---|
72 | * 0 if the block isn't available any more. |
---|
73 | * |
---|
74 | * The returned block is strictly readonly as only |
---|
75 | * maped in memory - and will be invalid on the next |
---|
76 | * operation on this class. |
---|
77 | */ |
---|
78 | const Block *at(size_t index); |
---|
79 | |
---|
80 | /** |
---|
81 | * reorders blocks as needed. If newsize is null, |
---|
82 | * the history is emptied completely. The indices |
---|
83 | * returned on append won't change their semantic, |
---|
84 | * but they may not be valid after this call. |
---|
85 | */ |
---|
86 | bool setHistorySize(size_t newsize); |
---|
87 | |
---|
88 | size_t newBlock(); |
---|
89 | |
---|
90 | Block *lastBlock() const; |
---|
91 | |
---|
92 | /** |
---|
93 | * Convenient function to set the size in KBytes |
---|
94 | * instead of blocks |
---|
95 | */ |
---|
96 | bool setSize(size_t newsize); |
---|
97 | |
---|
98 | size_t len() const { return length; } |
---|
99 | |
---|
100 | bool has(size_t index) const; |
---|
101 | |
---|
102 | size_t getCurrent() const { return current; } |
---|
103 | |
---|
104 | private: |
---|
105 | void unmap(); |
---|
106 | void increaseBuffer(); |
---|
107 | void decreaseBuffer(size_t newsize); |
---|
108 | |
---|
109 | size_t size; |
---|
110 | // current always shows to the last inserted block |
---|
111 | size_t current; |
---|
112 | size_t index; |
---|
113 | |
---|
114 | Block *lastmap; |
---|
115 | size_t lastmap_index; |
---|
116 | Block *lastblock; |
---|
117 | |
---|
118 | int ion; |
---|
119 | size_t length; |
---|
120 | |
---|
121 | }; |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | #endif |
---|