File indexing completed on 2024-12-29 04:11:44

0001 /***************************************************************************
0002  *                                                                         *
0003  *   Copyright : (C) 2012 Peter Kümmel                                     *
0004  *   email     : syntheticpp@gmx.net                                       *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #ifndef ASCII_FILE_DATA_H
0014 #define ASCII_FILE_DATA_H
0015 
0016 #include <QVector>
0017 #include <QSharedPointer>
0018 
0019 class QFile;
0020 template<class T, int Prealloc>
0021 class QVarLengthArray;
0022 
0023 
0024 class AsciiFileData
0025 {
0026 public:
0027 
0028   enum SizeOnStack
0029   {
0030     Prealloc =
0031 #if defined(__ANDROID__) || defined(__QNX__) || defined(KST_SMALL_PRREALLOC)
0032     // Some mobile systems really do not like you allocating 1MB on the stack.
0033     1 * 1024
0034 #else
0035     1 * 1024 * 1024
0036 #endif
0037   };
0038 
0039   typedef QVarLengthArray<char, Prealloc> Array;
0040   
0041   AsciiFileData();
0042   ~AsciiFileData();
0043 
0044   inline bool reread() const { return _reread; }
0045   inline void setReread(bool value) { _reread = value; }
0046 
0047   inline qint64 begin() const { return _begin; }
0048   inline qint64 bytesRead() const { return _bytesRead; }
0049   inline void setBegin(qint64 begin) { _begin = begin; }
0050   inline void setBytesRead(qint64 read) { _bytesRead = read; }
0051 
0052   inline void setFile(QFile* file) { _file = file; }
0053   bool read();
0054   qint64 read(QFile&, qint64 start, qint64 numberOfBytes, qint64 maximalBytes = -1);
0055 
0056   char* data();
0057   const char* const constPointer() const;
0058   const Array& constArray() const;
0059 
0060   bool resize(qint64 size);
0061   void clear(bool forceDeletingArray = false);
0062 
0063   inline qint64 rowBegin() const { return _rowBegin; }
0064   inline qint64 rowsRead() const { return _rowsRead; }
0065   inline void setRowBegin(qint64 begin) { _rowBegin = begin; }
0066   inline void setRowsRead(qint64 read) { _rowsRead = read; }
0067 
0068   void setSharedArray(AsciiFileData&);
0069 
0070 
0071   void logData() const;
0072   static void logData(const QVector<AsciiFileData>& chunks);
0073 
0074 private:
0075   QSharedPointer<Array> _array;
0076   QFile* _file;
0077   bool _fileRead;
0078   bool _reread;
0079   qint64 _begin;
0080   qint64 _bytesRead;
0081   qint64 _rowBegin;
0082   qint64 _rowsRead;
0083 
0084 };
0085 
0086 Q_DECLARE_TYPEINFO(AsciiFileData, Q_MOVABLE_TYPE);
0087 
0088 #endif
0089 // vim: ts=2 sw=2 et