1bb02e0acSNicolas Bonnefon /* 2bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon #ifndef ABSTRACTLOGDATA_H 21bb02e0acSNicolas Bonnefon #define ABSTRACTLOGDATA_H 22bb02e0acSNicolas Bonnefon 23bb02e0acSNicolas Bonnefon #include <QObject> 24bb02e0acSNicolas Bonnefon #include <QString> 25bb02e0acSNicolas Bonnefon #include <QStringList> 26bb02e0acSNicolas Bonnefon 27*209000a6SNicolas Bonnefon #include "utils.h" 28*209000a6SNicolas Bonnefon 29bb02e0acSNicolas Bonnefon // Base class representing a set of data. 30bb02e0acSNicolas Bonnefon // It can be either a full set or a filtered set. 31bb02e0acSNicolas Bonnefon class AbstractLogData : public QObject { 32bb02e0acSNicolas Bonnefon Q_OBJECT 33bb02e0acSNicolas Bonnefon 34bb02e0acSNicolas Bonnefon public: 35bb02e0acSNicolas Bonnefon AbstractLogData(); 36bb02e0acSNicolas Bonnefon // Permit each child to have its destructor ~AbstractLogData()37bb02e0acSNicolas Bonnefon virtual ~AbstractLogData() {}; 38bb02e0acSNicolas Bonnefon 39bb02e0acSNicolas Bonnefon // Returns the line passed as a QString 40bb02e0acSNicolas Bonnefon QString getLineString( qint64 line ) const; 41bb02e0acSNicolas Bonnefon // Returns the line passed as a QString, with tabs expanded 42bb02e0acSNicolas Bonnefon QString getExpandedLineString( qint64 line ) const; 43bb02e0acSNicolas Bonnefon // Returns a set of lines as a QStringList 44bb02e0acSNicolas Bonnefon QStringList getLines( qint64 first_line, int number ) const; 45bb02e0acSNicolas Bonnefon // Returns a set of lines with tabs expanded 46bb02e0acSNicolas Bonnefon QStringList getExpandedLines( qint64 first_line, int number ) const; 47bb02e0acSNicolas Bonnefon // Returns the total number of lines 48bb02e0acSNicolas Bonnefon qint64 getNbLine() const; 49bb02e0acSNicolas Bonnefon // Returns the visible length of the longest line 50bb02e0acSNicolas Bonnefon // Tabs are expanded 51bb02e0acSNicolas Bonnefon int getMaxLength() const; 52bb02e0acSNicolas Bonnefon // Returns the visible length of the passed line 53bb02e0acSNicolas Bonnefon // Tabs are expanded 54bb02e0acSNicolas Bonnefon int getLineLength( qint64 line ) const; 55bb02e0acSNicolas Bonnefon 565fa25391SNicolas Bonnefon // Set the view to use the passed encoding for display 57*209000a6SNicolas Bonnefon void setDisplayEncoding( Encoding encoding ); 585fa25391SNicolas Bonnefon 59bb02e0acSNicolas Bonnefon // Length of a tab stop 60bb02e0acSNicolas Bonnefon static const int tabStop = 8; 61bb02e0acSNicolas Bonnefon 62bb02e0acSNicolas Bonnefon protected: 63bb02e0acSNicolas Bonnefon // Internal function called to get a given line 64bb02e0acSNicolas Bonnefon virtual QString doGetLineString( qint64 line ) const = 0; 65bb02e0acSNicolas Bonnefon // Internal function called to get a given line 66bb02e0acSNicolas Bonnefon virtual QString doGetExpandedLineString( qint64 line ) const = 0; 67bb02e0acSNicolas Bonnefon // Internal function called to get a set of lines 68bb02e0acSNicolas Bonnefon virtual QStringList doGetLines( qint64 first_line, int number ) const = 0; 69bb02e0acSNicolas Bonnefon // Internal function called to get a set of expanded lines 70bb02e0acSNicolas Bonnefon virtual QStringList doGetExpandedLines( qint64 first_line, int number ) const = 0; 71bb02e0acSNicolas Bonnefon // Internal function called to get the number of lines 72bb02e0acSNicolas Bonnefon virtual qint64 doGetNbLine() const = 0; 73bb02e0acSNicolas Bonnefon // Internal function called to get the maximum length 74bb02e0acSNicolas Bonnefon virtual int doGetMaxLength() const = 0; 75bb02e0acSNicolas Bonnefon // Internal function called to get the line length 76bb02e0acSNicolas Bonnefon virtual int doGetLineLength( qint64 line ) const = 0; 775fa25391SNicolas Bonnefon // Internal function called to set the encoding 78*209000a6SNicolas Bonnefon virtual void doSetDisplayEncoding( Encoding encoding ) = 0; 794a4a124eSNicolas Bonnefon // Internal function called to set the newline offsets 804a4a124eSNicolas Bonnefon virtual void doSetMultibyteEncodingOffsets( int before_cr, int after_cr ) = 0; 81bb02e0acSNicolas Bonnefon untabify(const QString & line)82bb02e0acSNicolas Bonnefon static inline QString untabify( const QString& line ) { 83bb02e0acSNicolas Bonnefon QString untabified_line; 84bb02e0acSNicolas Bonnefon int total_spaces = 0; 85bb02e0acSNicolas Bonnefon 86bb02e0acSNicolas Bonnefon for ( int j = 0; j < line.length(); j++ ) { 87bb02e0acSNicolas Bonnefon if ( line[j] == '\t' ) { 88bb02e0acSNicolas Bonnefon int spaces = tabStop - ( ( j + total_spaces ) % tabStop ); 89bb02e0acSNicolas Bonnefon // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)"; 90bb02e0acSNicolas Bonnefon QString blanks( spaces, QChar(' ') ); 91bb02e0acSNicolas Bonnefon untabified_line.append( blanks ); 92bb02e0acSNicolas Bonnefon total_spaces += spaces - 1; 93bb02e0acSNicolas Bonnefon } 944343ea42SNicolas Bonnefon else if ( line[j] == '\0' ) { 954343ea42SNicolas Bonnefon untabified_line.append( QChar(' ') ); 964343ea42SNicolas Bonnefon } 97bb02e0acSNicolas Bonnefon else { 98bb02e0acSNicolas Bonnefon untabified_line.append( line[j] ); 99bb02e0acSNicolas Bonnefon } 100bb02e0acSNicolas Bonnefon } 101bb02e0acSNicolas Bonnefon 102bb02e0acSNicolas Bonnefon return untabified_line; 103bb02e0acSNicolas Bonnefon } 104bb02e0acSNicolas Bonnefon untabify(const char * line)105bb02e0acSNicolas Bonnefon static inline QString untabify( const char* line ) { 106bb02e0acSNicolas Bonnefon QString untabified_line; 107bb02e0acSNicolas Bonnefon int total_spaces = 0; 108bb02e0acSNicolas Bonnefon 109bb02e0acSNicolas Bonnefon for ( const char* i = line; *i != '\0'; i++ ) { 110bb02e0acSNicolas Bonnefon if ( *i == '\t' ) { 111bb02e0acSNicolas Bonnefon int spaces = tabStop - ( ( (i - line) + total_spaces ) % tabStop ); 112bb02e0acSNicolas Bonnefon // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)"; 113bb02e0acSNicolas Bonnefon QString blanks( spaces, QChar(' ') ); 114bb02e0acSNicolas Bonnefon untabified_line.append( blanks ); 115bb02e0acSNicolas Bonnefon total_spaces += spaces - 1; 116bb02e0acSNicolas Bonnefon } 1174343ea42SNicolas Bonnefon else if ( *i == '\0' ) { 1184343ea42SNicolas Bonnefon untabified_line.append( QChar(' ') ); 1194343ea42SNicolas Bonnefon } 120bb02e0acSNicolas Bonnefon else { 121bb02e0acSNicolas Bonnefon untabified_line.append( *i ); 122bb02e0acSNicolas Bonnefon } 123bb02e0acSNicolas Bonnefon } 124bb02e0acSNicolas Bonnefon 125bb02e0acSNicolas Bonnefon return untabified_line; 126bb02e0acSNicolas Bonnefon } 127bb02e0acSNicolas Bonnefon }; 128bb02e0acSNicolas Bonnefon 129bb02e0acSNicolas Bonnefon #endif 130