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