Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konsole / src / History.h
blob640038ee647da91a74afe7e8d53c2b5bbd44f7d4
1 /*
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
18 02110-1301 USA.
21 #ifndef TEHISTORY_H
22 #define TEHISTORY_H
24 // Qt
25 #include <QtCore/QBitRef>
26 #include <QtCore/QHash>
27 #include <QtCore/QVector>
29 // KDE
30 #include <ktemporaryfile.h>
32 // Konsole
33 #include "BlockArray.h"
34 #include "Character.h"
36 namespace Konsole
39 #if 1
41 An extendable tmpfile(1) based buffer.
44 class HistoryFile
46 public:
47 HistoryFile();
48 virtual ~HistoryFile();
50 virtual void add(const unsigned char* bytes, int len);
51 virtual void get(unsigned char* bytes, int len, int loc);
52 virtual int len();
54 //mmaps the file in read-only mode
55 void map();
56 //un-mmaps the file
57 void unmap();
58 //returns true if the file is mmap'ed
59 bool isMapped();
62 private:
63 int ion;
64 int length;
65 KTemporaryFile tmpFile;
67 //pointer to start of mmap'ed file data, or 0 if the file is not mmap'ed
68 char* fileMap;
70 //incremented whenver 'add' is called and decremented whenever
71 //'get' is called.
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).
74 int readWriteBalance;
76 //when readWriteBalance goes below this threshold, the file will be mmap'ed automatically
77 static const int MAP_THRESHOLD = -1000;
79 #endif
81 //////////////////////////////////////////////////////////////////////
83 //////////////////////////////////////////////////////////////////////
84 // Abstract base class for file and buffer versions
85 //////////////////////////////////////////////////////////////////////
86 class HistoryType;
88 class HistoryScroll
90 public:
91 HistoryScroll(HistoryType*);
92 virtual ~HistoryScroll();
94 virtual bool hasScroll();
96 // access to history
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; }
105 // adding lines.
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; }
123 protected:
124 HistoryType* m_histType;
128 #if 1
130 //////////////////////////////////////////////////////////////////////
131 // File-based history (e.g. file log, no limitation in length)
132 //////////////////////////////////////////////////////////////////////
134 class HistoryScrollFile : public HistoryScroll
136 public:
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);
148 private:
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
163 public:
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; }
182 private:
183 int bufferIndex(int lineNumber);
185 HistoryLine* _historyBuffer;
186 QBitArray _wrappedLine;
187 int _maxLineCount;
188 int _usedLines;
189 int _head;
191 //QVector<histline*> m_histBuffer;
192 //QBitArray m_wrappedLine;
193 //unsigned int m_maxNbLines;
194 //unsigned int m_nbLines;
195 //unsigned int m_arrayIndex;
196 //bool m_buffFilled;
199 /*class HistoryScrollBufferV2 : public HistoryScroll
201 public:
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);
211 };*/
213 #endif
215 //////////////////////////////////////////////////////////////////////
216 // Nothing-based history (no history :-)
217 //////////////////////////////////////////////////////////////////////
218 class HistoryScrollNone : public HistoryScroll
220 public:
221 HistoryScrollNone();
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
240 public:
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);
252 protected:
253 BlockArray m_blockArray;
254 QHash<int,size_t> m_lineLengths;
257 //////////////////////////////////////////////////////////////////////
258 // History type
259 //////////////////////////////////////////////////////////////////////
261 class HistoryType
263 public:
264 HistoryType();
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
287 public:
288 HistoryTypeNone();
290 virtual bool isEnabled() const;
291 virtual int maximumLineCount() const;
293 virtual HistoryScroll* scroll(HistoryScroll *) const;
296 class HistoryTypeBlockArray : public HistoryType
298 public:
299 HistoryTypeBlockArray(size_t size);
301 virtual bool isEnabled() const;
302 virtual int maximumLineCount() const;
304 virtual HistoryScroll* scroll(HistoryScroll *) const;
306 protected:
307 size_t m_size;
310 #if 1
311 class HistoryTypeFile : public HistoryType
313 public:
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;
322 protected:
323 QString m_fileName;
327 class HistoryTypeBuffer : public HistoryType
329 friend class HistoryScrollBuffer;
331 public:
332 HistoryTypeBuffer(unsigned int nbLines);
334 virtual bool isEnabled() const;
335 virtual int maximumLineCount() const;
337 virtual HistoryScroll* scroll(HistoryScroll *) const;
339 protected:
340 unsigned int m_nbLines;
343 #endif
347 #endif // TEHISTORY_H