xref: /glogg/src/data/abstractlogdata.h (revision 05467f52d72e282658c219cc889ed6bc08e5dc01)
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 {
97                 untabified_line.append( line[j] );
98             }
99         }
100 
101         return untabified_line;
102     }
103 
104     static inline QString untabify( const char* line ) {
105         QString untabified_line;
106         int total_spaces = 0;
107 
108         for ( const char* i = line; *i != '\0'; i++ ) {
109             if ( *i == '\t' ) {
110                 int spaces = tabStop - ( ( (i - line) + total_spaces ) % tabStop );
111                 // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)";
112                 QString blanks( spaces, QChar(' ') );
113                 untabified_line.append( blanks );
114                 total_spaces += spaces - 1;
115             }
116             else {
117                 untabified_line.append( *i );
118             }
119         }
120 
121         return untabified_line;
122     }
123 };
124 
125 #endif
126