1 /* 2 * Copyright (C) 2015 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 #include <QtGlobal> 21 #include <QVector> 22 23 // This class is a list of end of lines position, 24 // in addition to a list of qint64 (positions within the files) 25 // it can keep track of whether the final LF was added (for non-LF terminated 26 // files) and remove it when more data are added. 27 class LinePositionArray 28 { 29 public: 30 // Default constructor 31 LinePositionArray() : array() 32 { fakeFinalLF_ = false; } 33 // Copy constructor 34 inline LinePositionArray( const LinePositionArray& orig ) 35 : array(orig.array) 36 { fakeFinalLF_ = orig.fakeFinalLF_; } 37 38 // Add a new line position at the given position 39 // Invariant: pos must be greater than the previous one 40 // (this is NOT checked!) 41 inline void append( qint64 pos ) 42 { 43 if ( fakeFinalLF_ ) 44 array.pop_back(); 45 46 array.append( pos ); 47 } 48 // Size of the array 49 inline int size() 50 { return array.size(); } 51 // Extract an element 52 inline qint64 at( int i ) const 53 { return array.at( i ); } 54 inline qint64 operator[]( int i ) const 55 { return array.at( i ); } 56 // Set the presence of a fake final LF 57 // Must be used after 'append'-ing a fake LF at the end. 58 void setFakeFinalLF( bool finalLF=true ) 59 { fakeFinalLF_ = finalLF; } 60 61 // Add another list to this one, removing any fake LF on this list. 62 // Invariant: all pos in other must be greater than any pos in this 63 // (this is NOT checked!) 64 LinePositionArray& operator+= ( const LinePositionArray& other ) 65 { 66 // If our final LF is fake, we remove it 67 if ( fakeFinalLF_ ) 68 this->array.pop_back(); 69 70 // Append the arrays 71 this->array += other.array; 72 73 // In case the 'other' object has a fake LF 74 this->fakeFinalLF_ = other.fakeFinalLF_; 75 76 return *this; 77 } 78 79 private: 80 QVector<qint64> array; 81 bool fakeFinalLF_; 82 }; 83 84 85