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