xref: /glogg/src/data/abstractlogdata.h (revision 4fb0346e73d7caa82d42531c8c8681b5eb607728)
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 // Base class representing a set of data.
28 // It can be either a full set or a filtered set.
29 class AbstractLogData : public QObject {
30   Q_OBJECT
31 
32   public:
33     AbstractLogData();
34     // Permit each child to have its destructor
35     virtual ~AbstractLogData() {};
36 
37     // Returns the line passed as a QString
38     QString getLineString( qint64 line ) const;
39     // Returns the line passed as a QString, with tabs expanded
40     QString getExpandedLineString( qint64 line ) const;
41     // Returns a set of lines as a QStringList
42     QStringList getLines( qint64 first_line, int number ) const;
43     // Returns a set of lines with tabs expanded
44     QStringList getExpandedLines( qint64 first_line, int number ) const;
45     // Returns the total number of lines
46     qint64 getNbLine() const;
47     // Returns the visible length of the longest line
48     // Tabs are expanded
49     int getMaxLength() const;
50     // Returns the visible length of the passed line
51     // Tabs are expanded
52     int getLineLength( qint64 line ) const;
53 
54     // Set the view to use the passed encoding for display
55     void setDisplayEncoding( const char* encoding_name );
56     // Configure how the view shall interpret newline characters
57     // this should be non zero for encodings where \n is encoded
58     // in multiple bytes (e.g. UTF-16)
59     void setMultibyteEncodingOffsets( int before_cr, int after_cr );
60 
61     // Length of a tab stop
62     static const int tabStop = 8;
63 
64   protected:
65     // Internal function called to get a given line
66     virtual QString doGetLineString( qint64 line ) const = 0;
67     // Internal function called to get a given line
68     virtual QString doGetExpandedLineString( qint64 line ) const = 0;
69     // Internal function called to get a set of lines
70     virtual QStringList doGetLines( qint64 first_line, int number ) const = 0;
71     // Internal function called to get a set of expanded lines
72     virtual QStringList doGetExpandedLines( qint64 first_line, int number ) const = 0;
73     // Internal function called to get the number of lines
74     virtual qint64 doGetNbLine() const = 0;
75     // Internal function called to get the maximum length
76     virtual int doGetMaxLength() const = 0;
77     // Internal function called to get the line length
78     virtual int doGetLineLength( qint64 line ) const = 0;
79     // Internal function called to set the encoding
80     virtual void doSetDisplayEncoding( const char* encoding ) = 0;
81     // Internal function called to set the newline offsets
82     virtual void doSetMultibyteEncodingOffsets( int before_cr, int after_cr ) = 0;
83 
84     static inline QString untabify( const QString& line ) {
85         QString untabified_line;
86         int total_spaces = 0;
87 
88         for ( int j = 0; j < line.length(); j++ ) {
89             if ( line[j] == '\t' ) {
90                 int spaces = tabStop - ( ( j + total_spaces ) % tabStop );
91                 // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)";
92                 QString blanks( spaces, QChar(' ') );
93                 untabified_line.append( blanks );
94                 total_spaces += spaces - 1;
95             }
96             else if ( line[j] == '\0' ) {
97                 untabified_line.append( QChar(' ') );
98             }
99             else {
100                 untabified_line.append( line[j] );
101             }
102         }
103 
104         return untabified_line;
105     }
106 
107     static inline QString untabify( const char* line ) {
108         QString untabified_line;
109         int total_spaces = 0;
110 
111         for ( const char* i = line; *i != '\0'; i++ ) {
112             if ( *i == '\t' ) {
113                 int spaces = tabStop - ( ( (i - line) + total_spaces ) % tabStop );
114                 // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)";
115                 QString blanks( spaces, QChar(' ') );
116                 untabified_line.append( blanks );
117                 total_spaces += spaces - 1;
118             }
119             else if ( *i == '\0' ) {
120                 untabified_line.append( QChar(' ') );
121             }
122             else {
123                 untabified_line.append( *i );
124             }
125         }
126 
127         return untabified_line;
128     }
129 };
130 
131 #endif
132