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