2 This file is part of Konsole, an X terminal.
3 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <QtCore/QBitRef>
26 #include <QtCore/QHash>
27 #include <QtCore/QVector>
30 #include <ktemporaryfile.h>
33 #include "BlockArray.h"
34 #include "Character.h"
41 An extendable tmpfile(1) based buffer.
48 virtual ~HistoryFile();
50 virtual void add(const unsigned char* bytes
, int len
);
51 virtual void get(unsigned char* bytes
, int len
, int loc
);
54 //mmaps the file in read-only mode
58 //returns true if the file is mmap'ed
65 KTemporaryFile tmpFile
;
67 //pointer to start of mmap'ed file data, or 0 if the file is not mmap'ed
70 //incremented whenver 'add' is called and decremented whenever
72 //this is used to detect when a large number of lines are being read and processed from the history
73 //and automatically mmap the file for better performance (saves the overhead of many lseek-read calls).
76 //when readWriteBalance goes below this threshold, the file will be mmap'ed automatically
77 static const int MAP_THRESHOLD
= -1000;
81 //////////////////////////////////////////////////////////////////////
83 //////////////////////////////////////////////////////////////////////
84 // Abstract base class for file and buffer versions
85 //////////////////////////////////////////////////////////////////////
91 HistoryScroll(HistoryType
*);
92 virtual ~HistoryScroll();
94 virtual bool hasScroll();
97 virtual int getLines() = 0;
98 virtual int getLineLen(int lineno
) = 0;
99 virtual void getCells(int lineno
, int colno
, int count
, Character res
[]) = 0;
100 virtual bool isWrappedLine(int lineno
) = 0;
102 // backward compatibility (obsolete)
103 Character
getCell(int lineno
, int colno
) { Character res
; getCells(lineno
,colno
,1,&res
); return res
; }
106 virtual void addCells(const Character a
[], int count
) = 0;
107 // convenience method - this is virtual so that subclasses can take advantage
108 // of QVector's implicit copying
109 virtual void addCellsVector(const QVector
<Character
>& cells
)
111 addCells(cells
.data(),cells
.size());
114 virtual void addLine(bool previousWrapped
=false) = 0;
117 // FIXME: Passing around constant references to HistoryType instances
118 // is very unsafe, because those references will no longer
119 // be valid if the history scroll is deleted.
121 const HistoryType
& getType() { return *m_histType
; }
124 HistoryType
* m_histType
;
130 //////////////////////////////////////////////////////////////////////
131 // File-based history (e.g. file log, no limitation in length)
132 //////////////////////////////////////////////////////////////////////
134 class HistoryScrollFile
: public HistoryScroll
137 HistoryScrollFile(const QString
&logFileName
);
138 virtual ~HistoryScrollFile();
140 virtual int getLines();
141 virtual int getLineLen(int lineno
);
142 virtual void getCells(int lineno
, int colno
, int count
, Character res
[]);
143 virtual bool isWrappedLine(int lineno
);
145 virtual void addCells(const Character a
[], int count
);
146 virtual void addLine(bool previousWrapped
=false);
149 int startOfLine(int lineno
);
151 QString m_logFileName
;
152 HistoryFile index
; // lines Row(int)
153 HistoryFile cells
; // text Row(Character)
154 HistoryFile lineflags
; // flags Row(unsigned char)
158 //////////////////////////////////////////////////////////////////////
159 // Buffer-based history (limited to a fixed nb of lines)
160 //////////////////////////////////////////////////////////////////////
161 class HistoryScrollBuffer
: public HistoryScroll
164 typedef QVector
<Character
> HistoryLine
;
166 HistoryScrollBuffer(unsigned int maxNbLines
= 1000);
167 virtual ~HistoryScrollBuffer();
169 virtual int getLines();
170 virtual int getLineLen(int lineno
);
171 virtual void getCells(int lineno
, int colno
, int count
, Character res
[]);
172 virtual bool isWrappedLine(int lineno
);
174 virtual void addCells(const Character a
[], int count
);
175 virtual void addCellsVector(const QVector
<Character
>& cells
);
176 virtual void addLine(bool previousWrapped
=false);
178 void setMaxNbLines(unsigned int nbLines
);
179 unsigned int maxNbLines() { return _maxLineCount
; }
183 int bufferIndex(int lineNumber
);
185 HistoryLine
* _historyBuffer
;
186 QBitArray _wrappedLine
;
191 //QVector<histline*> m_histBuffer;
192 //QBitArray m_wrappedLine;
193 //unsigned int m_maxNbLines;
194 //unsigned int m_nbLines;
195 //unsigned int m_arrayIndex;
199 /*class HistoryScrollBufferV2 : public HistoryScroll
202 virtual int getLines();
203 virtual int getLineLen(int lineno);
204 virtual void getCells(int lineno, int colno, int count, Character res[]);
205 virtual bool isWrappedLine(int lineno);
207 virtual void addCells(const Character a[], int count);
208 virtual void addCells(const QVector<Character>& cells);
209 virtual void addLine(bool previousWrapped=false);
215 //////////////////////////////////////////////////////////////////////
216 // Nothing-based history (no history :-)
217 //////////////////////////////////////////////////////////////////////
218 class HistoryScrollNone
: public HistoryScroll
222 virtual ~HistoryScrollNone();
224 virtual bool hasScroll();
226 virtual int getLines();
227 virtual int getLineLen(int lineno
);
228 virtual void getCells(int lineno
, int colno
, int count
, Character res
[]);
229 virtual bool isWrappedLine(int lineno
);
231 virtual void addCells(const Character a
[], int count
);
232 virtual void addLine(bool previousWrapped
=false);
235 //////////////////////////////////////////////////////////////////////
236 // BlockArray-based history
237 //////////////////////////////////////////////////////////////////////
238 class HistoryScrollBlockArray
: public HistoryScroll
241 HistoryScrollBlockArray(size_t size
);
242 virtual ~HistoryScrollBlockArray();
244 virtual int getLines();
245 virtual int getLineLen(int lineno
);
246 virtual void getCells(int lineno
, int colno
, int count
, Character res
[]);
247 virtual bool isWrappedLine(int lineno
);
249 virtual void addCells(const Character a
[], int count
);
250 virtual void addLine(bool previousWrapped
=false);
253 BlockArray m_blockArray
;
254 QHash
<int,size_t> m_lineLengths
;
257 //////////////////////////////////////////////////////////////////////
259 //////////////////////////////////////////////////////////////////////
265 virtual ~HistoryType();
268 * Returns true if the history is enabled ( can store lines of output )
269 * or false otherwise.
271 virtual bool isEnabled() const = 0;
273 * Returns true if the history size is unlimited.
275 bool isUnlimited() const { return maximumLineCount() == 0; }
277 * Returns the maximum number of lines which this history type
278 * can store or 0 if the history can store an unlimited number of lines.
280 virtual int maximumLineCount() const = 0;
282 virtual HistoryScroll
* scroll(HistoryScroll
*) const = 0;
285 class HistoryTypeNone
: public HistoryType
290 virtual bool isEnabled() const;
291 virtual int maximumLineCount() const;
293 virtual HistoryScroll
* scroll(HistoryScroll
*) const;
296 class HistoryTypeBlockArray
: public HistoryType
299 HistoryTypeBlockArray(size_t size
);
301 virtual bool isEnabled() const;
302 virtual int maximumLineCount() const;
304 virtual HistoryScroll
* scroll(HistoryScroll
*) const;
311 class HistoryTypeFile
: public HistoryType
314 HistoryTypeFile(const QString
& fileName
=QString());
316 virtual bool isEnabled() const;
317 virtual const QString
& getFileName() const;
318 virtual int maximumLineCount() const;
320 virtual HistoryScroll
* scroll(HistoryScroll
*) const;
327 class HistoryTypeBuffer
: public HistoryType
329 friend class HistoryScrollBuffer
;
332 HistoryTypeBuffer(unsigned int nbLines
);
334 virtual bool isEnabled() const;
335 virtual int maximumLineCount() const;
337 virtual HistoryScroll
* scroll(HistoryScroll
*) const;
340 unsigned int m_nbLines
;
347 #endif // TEHISTORY_H