xref: /glogg/src/data/abstractlogdata.h (revision fcf40f1761134a69881dfa6f1d28d01064374a37)
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 
57     // Length of a tab stop
58     static const int tabStop = 8;
59 
60   protected:
61     // Internal function called to get a given line
62     virtual QString doGetLineString( qint64 line ) const = 0;
63     // Internal function called to get a given line
64     virtual QString doGetExpandedLineString( qint64 line ) const = 0;
65     // Internal function called to get a set of lines
66     virtual QStringList doGetLines( qint64 first_line, int number ) const = 0;
67     // Internal function called to get a set of expanded lines
68     virtual QStringList doGetExpandedLines( qint64 first_line, int number ) const = 0;
69     // Internal function called to get the number of lines
70     virtual qint64 doGetNbLine() const = 0;
71     // Internal function called to get the maximum length
72     virtual int doGetMaxLength() const = 0;
73     // Internal function called to get the line length
74     virtual int doGetLineLength( qint64 line ) const = 0;
75     // Internal function called to set the encoding
76     virtual void doSetDisplayEncoding( const char* encoding ) = 0;
77 
78     static inline QString untabify( const QString& line ) {
79         QString untabified_line;
80         int total_spaces = 0;
81 
82         for ( int j = 0; j < line.length(); j++ ) {
83             if ( line[j] == '\t' ) {
84                 int spaces = tabStop - ( ( j + total_spaces ) % tabStop );
85                 // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)";
86                 QString blanks( spaces, QChar(' ') );
87                 untabified_line.append( blanks );
88                 total_spaces += spaces - 1;
89             }
90             else {
91                 untabified_line.append( line[j] );
92             }
93         }
94 
95         return untabified_line;
96     }
97 
98     static inline QString untabify( const char* line ) {
99         QString untabified_line;
100         int total_spaces = 0;
101 
102         for ( const char* i = line; *i != '\0'; i++ ) {
103             if ( *i == '\t' ) {
104                 int spaces = tabStop - ( ( (i - line) + total_spaces ) % tabStop );
105                 // LOG(logDEBUG4) << "Replacing tab at char " << j << " (" << spaces << " spaces)";
106                 QString blanks( spaces, QChar(' ') );
107                 untabified_line.append( blanks );
108                 total_spaces += spaces - 1;
109             }
110             else {
111                 untabified_line.append( *i );
112             }
113         }
114 
115         return untabified_line;
116     }
117 };
118 
119 #endif
120