GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
History.h
Go to the documentation of this file.
1 /*
2  This file is part of Konsole, an X terminal.
3  Copyright (C) 1997-1998, 2013 by Lars Doelle <[email protected]>
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 HISTORY_H
24 #define HISTORY_H
25 
26 // Qt
27 #include <QtCore/QBitRef>
28 #include <QtCore/QHash>
29 #include <QtCore>
30 
31 // Konsole
32 #include "unix/BlockArray.h"
33 #include "unix/Character.h"
34 
35 
37 {
38 public:
39  HistoryFile();
40  virtual ~HistoryFile();
41 
42  virtual void add(const unsigned char* bytes, int len);
43  virtual void get(unsigned char* bytes, int len, int loc);
44  virtual int len();
45 
46  //mmaps the file in read-only mode
47  void map();
48  //un-mmaps the file
49  void unmap();
50  //returns true if the file is mmap'ed
51  bool isMapped();
52 
53 
54 private:
55  int ion;
56  int length;
57  QTemporaryFile tmpFile;
58 
59  //pointer to start of mmap'ed file data, or 0 if the file is not mmap'ed
60  char* fileMap;
61 
62  //incremented whenver 'add' is called and decremented whenever
63  //'get' is called.
64  //this is used to detect when a large number of lines are being read and processed from the history
65  //and automatically mmap the file for better performance (saves the overhead of many lseek-read calls).
67 
68  //when readWriteBalance goes below this threshold, the file will be mmap'ed automatically
69  static const int MAP_THRESHOLD = -1000;
70 };
71 
72 //////////////////////////////////////////////////////////////////////
73 
74 //////////////////////////////////////////////////////////////////////
75 // Abstract base class for file and buffer versions
76 //////////////////////////////////////////////////////////////////////
77 class HistoryType;
78 
80 {
81 public:
83  virtual ~HistoryScroll();
84 
85  virtual bool hasScroll();
86 
87  // access to history
88  virtual int getLines() = 0;
89  virtual int getLineLen(int lineno) = 0;
90  virtual void getCells(int lineno, int colno, int count, Character res[]) = 0;
91  virtual bool isWrappedLine(int lineno) = 0;
92 
93  // backward compatibility (obsolete)
94  Character getCell(int lineno, int colno) { Character res; getCells(lineno,colno,1,&res); return res; }
95 
96  // adding lines.
97  virtual void addCells(const Character a[], int count) = 0;
98  // convenience method - this is virtual so that subclasses can take advantage
99  // of QVector's implicit copying
100  virtual void addCellsVector(const QVector<Character>& cells)
101  {
102  addCells(cells.data(),cells.size());
103  }
104 
105  virtual void addLine(bool previousWrapped=false) = 0;
106 
107  //
108  // FIXME: Passing around constant references to HistoryType instances
109  // is very unsafe, because those references will no longer
110  // be valid if the history scroll is deleted.
111  //
112  const HistoryType& getType() { return *m_histType; }
113 
114 protected:
116 
117 };
118 
119 
120 //////////////////////////////////////////////////////////////////////
121 // File-based history (e.g. file log, no limitation in length)
122 //////////////////////////////////////////////////////////////////////
123 
125 {
126 public:
127  HistoryScrollFile(const QString &logFileName);
128  virtual ~HistoryScrollFile();
129 
130  virtual int getLines();
131  virtual int getLineLen(int lineno);
132  virtual void getCells(int lineno, int colno, int count, Character res[]);
133  virtual bool isWrappedLine(int lineno);
134 
135  virtual void addCells(const Character a[], int count);
136  virtual void addLine(bool previousWrapped=false);
137 
138 private:
139  int startOfLine(int lineno);
140 
141  QString m_logFileName;
142  HistoryFile index; // lines Row(int)
143  HistoryFile cells; // text Row(Character)
144  HistoryFile lineflags; // flags Row(unsigned char)
145 };
146 
147 
148 //////////////////////////////////////////////////////////////////////
149 // Buffer-based history (limited to a fixed nb of lines)
150 //////////////////////////////////////////////////////////////////////
152 {
153 public:
154  typedef QVector<Character> HistoryLine;
155 
156  HistoryScrollBuffer(unsigned int maxNbLines = 1000);
157  virtual ~HistoryScrollBuffer();
158 
159  virtual int getLines();
160  virtual int getLineLen(int lineno);
161  virtual void getCells(int lineno, int colno, int count, Character res[]);
162  virtual bool isWrappedLine(int lineno);
163 
164  virtual void addCells(const Character a[], int count);
165  virtual void addCellsVector(const QVector<Character>& cells);
166  virtual void addLine(bool previousWrapped=false);
167 
168  void setMaxNbLines(unsigned int nbLines);
169  unsigned int maxNbLines() { return _maxLineCount; }
170 
171 
172 private:
173  int bufferIndex(int lineNumber);
174 
175  HistoryLine* _historyBuffer;
176  QBitArray _wrappedLine;
179  int _head;
180 
181  //QVector<histline*> m_histBuffer;
182  //QBitArray m_wrappedLine;
183  //unsigned int m_maxNbLines;
184  //unsigned int m_nbLines;
185  //unsigned int m_arrayIndex;
186  //bool m_buffFilled;
187 };
188 
189 /*class HistoryScrollBufferV2 : public HistoryScroll
190 {
191 public:
192  virtual int getLines();
193  virtual int getLineLen(int lineno);
194  virtual void getCells(int lineno, int colno, int count, Character res[]);
195  virtual bool isWrappedLine(int lineno);
196 
197  virtual void addCells(const Character a[], int count);
198  virtual void addCells(const QVector<Character>& cells);
199  virtual void addLine(bool previousWrapped=false);
200 
201 };*/
202 
203 
204 //////////////////////////////////////////////////////////////////////
205 // Nothing-based history (no history :-)
206 //////////////////////////////////////////////////////////////////////
208 {
209 public:
211  virtual ~HistoryScrollNone();
212 
213  virtual bool hasScroll();
214 
215  virtual int getLines();
216  virtual int getLineLen(int lineno);
217  virtual void getCells(int lineno, int colno, int count, Character res[]);
218  virtual bool isWrappedLine(int lineno);
219 
220  virtual void addCells(const Character a[], int count);
221  virtual void addLine(bool previousWrapped=false);
222 };
223 
224 //////////////////////////////////////////////////////////////////////
225 // BlockArray-based history
226 //////////////////////////////////////////////////////////////////////
228 {
229 public:
231  virtual ~HistoryScrollBlockArray();
232 
233  virtual int getLines();
234  virtual int getLineLen(int lineno);
235  virtual void getCells(int lineno, int colno, int count, Character res[]);
236  virtual bool isWrappedLine(int lineno);
237 
238  virtual void addCells(const Character a[], int count);
239  virtual void addLine(bool previousWrapped=false);
240 
241 protected:
243  QHash<int,size_t> m_lineLengths;
244 };
245 
246 //////////////////////////////////////////////////////////////////////
247 // History type
248 //////////////////////////////////////////////////////////////////////
249 
251 {
252 public:
253  HistoryType();
254  virtual ~HistoryType();
255 
256  /**
257  * Returns true if the history is enabled ( can store lines of output )
258  * or false otherwise.
259  */
260  virtual bool isEnabled() const = 0;
261  /**
262  * Returns true if the history size is unlimited.
263  */
264  bool isUnlimited() const { return maximumLineCount() == 0; }
265  /**
266  * Returns the maximum number of lines which this history type
267  * can store or 0 if the history can store an unlimited number of lines.
268  */
269  virtual int maximumLineCount() const = 0;
270 
271  virtual HistoryScroll* scroll(HistoryScroll *) const = 0;
272 };
273 
275 {
276 public:
277  HistoryTypeNone();
278 
279  virtual bool isEnabled() const;
280  virtual int maximumLineCount() const;
281 
282  virtual HistoryScroll* scroll(HistoryScroll *) const;
283 };
284 
286 {
287 public:
288  HistoryTypeBlockArray(size_t size);
289 
290  virtual bool isEnabled() const;
291  virtual int maximumLineCount() const;
292 
293  virtual HistoryScroll* scroll(HistoryScroll *) const;
294 
295 protected:
296  size_t m_size;
297 };
298 
299 
301 {
302 public:
303  HistoryTypeFile(const QString& fileName=QString());
304 
305  virtual bool isEnabled() const;
306  virtual const QString& getFileName() const;
307  virtual int maximumLineCount() const;
308 
309  virtual HistoryScroll* scroll(HistoryScroll *) const;
310 
311 protected:
312  QString m_fileName;
313 };
314 
315 
317 {
318 public:
319  HistoryTypeBuffer(unsigned int nbLines);
320 
321  virtual bool isEnabled() const;
322  virtual int maximumLineCount() const;
323 
324  virtual HistoryScroll* scroll(HistoryScroll *) const;
325 
326 protected:
327  unsigned int m_nbLines;
328 };
329 
330 #endif // HISTORY_H
virtual bool hasScroll()
Definition: History.cpp:424
virtual int maximumLineCount() const =0
Returns the maximum number of lines which this history type can store or 0 if the history can store a...
bool isMapped()
Definition: History.cpp:128
virtual HistoryScroll * scroll(HistoryScroll *) const
Definition: History.cpp:550
virtual void addLine(bool previousWrapped=false)
Definition: History.cpp:266
virtual void getCells(int lineno, int colno, int count, Character res[])=0
virtual ~HistoryScrollNone()
Definition: History.cpp:420
HistoryFile index
Definition: History.h:142
virtual void addCells(const Character a[], int count)
Definition: History.cpp:502
HistoryScroll(HistoryType *)
Definition: History.cpp:182
virtual ~HistoryScroll()
Definition: History.cpp:187
virtual int getLines()=0
QString m_logFileName
Definition: History.h:141
virtual void addCells(const Character a[], int count)=0
A single character in the terminal which consists of a unicode character value, foreground and backgr...
Definition: Character.h:55
virtual bool hasScroll()
Definition: History.cpp:192
QVector< Character > HistoryLine
Definition: History.h:154
virtual const QString & getFileName() const
Definition: History.cpp:656
virtual void getCells(int lineno, int colno, int count, Character res[])
Definition: History.cpp:486
virtual int len()
Definition: History.cpp:173
virtual void addCells(const Character a[], int count)
Definition: History.cpp:308
BlockArray m_blockArray
Definition: History.h:242
unsigned int maxNbLines()
Definition: History.h:169
HistoryLine * _historyBuffer
Definition: History.h:175
virtual bool isWrappedLine(int lineno)=0
QTemporaryFile tmpFile
Definition: History.h:57
virtual int getLines()
Definition: History.cpp:220
virtual bool isWrappedLine(int lineno)
Definition: History.cpp:439
virtual void getCells(int lineno, int colno, int count, Character res[])
Definition: History.cpp:256
Character getCell(int lineno, int colno)
Definition: History.h:94
HistoryScrollBlockArray(size_t size)
Definition: History.cpp:458
virtual void addLine(bool previousWrapped=false)
Definition: History.cpp:452
virtual HistoryScroll * scroll(HistoryScroll *) const =0
virtual ~HistoryScrollBlockArray()
Definition: History.cpp:464
virtual ~HistoryType()
Definition: History.cpp:535
virtual bool isEnabled() const
Returns true if the history is enabled ( can store lines of output ) or false otherwise.
Definition: History.cpp:545
virtual int getLines()
Definition: History.cpp:321
virtual int getLines()
Definition: History.cpp:429
virtual ~HistoryFile()
Definition: History.cpp:96
virtual int maximumLineCount() const
Returns the maximum number of lines which this history type can store or 0 if the history can store a...
Definition: History.cpp:556
virtual void addCellsVector(const QVector< Character > &cells)
Definition: History.cpp:294
virtual bool isEnabled() const
Returns true if the history is enabled ( can store lines of output ) or false otherwise.
Definition: History.cpp:592
virtual int maximumLineCount() const
Returns the maximum number of lines which this history type can store or 0 if the history can store a...
Definition: History.cpp:693
int length
Definition: History.h:56
#define loc(X, Y)
Definition: Screen.cpp:56
const HistoryType & getType()
Definition: History.h:112
virtual int getLineLen(int lineno)
Definition: History.cpp:473
virtual bool isWrappedLine(int lineno)
Definition: History.cpp:481
bool isUnlimited() const
Returns true if the history size is unlimited.
Definition: History.h:264
HistoryScrollBuffer(unsigned int maxNbLines=1000)
Definition: History.cpp:279
virtual bool isEnabled() const
Returns true if the history is enabled ( can store lines of output ) or false otherwise.
Definition: History.cpp:568
virtual int getLines()
Definition: History.cpp:468
virtual void addLine(bool previousWrapped=false)
Definition: History.cpp:523
int readWriteBalance
Definition: History.h:66
size_t size(T const (&)[z])
Definition: help.cc:103
HistoryFile lineflags
Definition: History.h:144
HistoryTypeBuffer(unsigned int nbLines)
Definition: History.cpp:587
virtual int getLineLen(int lineno)
Definition: History.cpp:326
virtual HistoryScroll * scroll(HistoryScroll *) const
Definition: History.cpp:661
QHash< int, size_t > m_lineLengths
Definition: History.h:243
virtual void getCells(int lineno, int colno, int count, Character res[])
Definition: History.cpp:444
virtual int getLineLen(int lineno)
Definition: History.cpp:434
virtual bool isWrappedLine(int lineno)
Definition: History.cpp:230
QString m_fileName
Definition: History.h:312
int startOfLine(int lineno)
Definition: History.cpp:240
virtual ~HistoryScrollFile()
Definition: History.cpp:216
HistoryTypeBlockArray(size_t size)
Definition: History.cpp:563
virtual void addCellsVector(const QVector< Character > &cells)
Definition: History.h:100
virtual void addCells(const Character a[], int count)
Definition: History.cpp:261
QBitArray _wrappedLine
Definition: History.h:176
char * fileMap
Definition: History.h:60
void setMaxNbLines(unsigned int nbLines)
Definition: History.cpp:376
virtual void getCells(int lineno, int colno, int count, Character res[])
Definition: History.cpp:353
virtual bool isEnabled() const =0
Returns true if the history is enabled ( can store lines of output ) or false otherwise.
HistoryTypeFile(const QString &fileName=QString())
Definition: History.cpp:646
HistoryType * m_histType
Definition: History.h:115
virtual int getLineLen(int lineno)=0
static const int MAP_THRESHOLD
Definition: History.h:69
virtual bool isEnabled() const
Returns true if the history is enabled ( can store lines of output ) or false otherwise.
Definition: History.cpp:651
virtual void addLine(bool previousWrapped=false)=0
virtual bool isWrappedLine(int lineno)
Definition: History.cpp:340
int bufferIndex(int lineNumber)
Definition: History.cpp:396
HistoryScrollFile(const QString &logFileName)
Definition: History.cpp:210
virtual HistoryScroll * scroll(HistoryScroll *) const
Definition: History.cpp:602
void map()
Definition: History.cpp:105
virtual void addCells(const Character a[], int count)
Definition: History.cpp:448
int ion
Definition: History.h:55
virtual void add(const unsigned char *bytes, int len)
Definition: History.cpp:133
HistoryFile cells
Definition: History.h:143
void unmap()
Definition: History.cpp:120
virtual int getLineLen(int lineno)
Definition: History.cpp:225
virtual ~HistoryScrollBuffer()
Definition: History.cpp:289
virtual int maximumLineCount() const
Returns the maximum number of lines which this history type can store or 0 if the history can store a...
Definition: History.cpp:573
virtual HistoryScroll * scroll(HistoryScroll *) const
Definition: History.cpp:578
unsigned int m_nbLines
Definition: History.h:327
virtual void addLine(bool previousWrapped=false)
Definition: History.cpp:316
virtual int maximumLineCount() const
Returns the maximum number of lines which this history type can store or 0 if the history can store a...
Definition: History.cpp:597