1 #ifndef CHUNKS_H
2 #define CHUNKS_H
3 
4 /** \cond docNever */
5 
6 /*! The Chunks class is the storage backend for QHexEdit.
7  *
8  * When QHexEdit loads data, Chunks access them using a QIODevice interface. When the app uses
9  * a QByteArray interface, QBuffer is used to provide again a QIODevice like interface. No data
10  * will be changed, therefore Chunks opens the QIODevice in QIODevice::ReadOnly mode. After every
11  * access Chunks closes the QIODevice, that's why external applications can overwrite files while
12  * QHexEdit shows them.
13  *
14  * When the the user starts to edit the data, Chunks creates a local copy of a chunk of data (4
15  * kilobytes) and notes all changes there. Parallel to that chunk, there is a second chunk,
16  * which keep track of which bytes are changed and which not.
17  *
18  */
19 
20 #include <QtCore>
21 
22 struct Chunk
23 {
24     QByteArray data;
25     QByteArray dataChanged;
26     qint64 absPos;
27 };
28 
29 class Chunks: public QObject
30 {
31 Q_OBJECT
32 public:
33     // Constructors and file settings
34     Chunks(QObject *parent);
35     Chunks(QIODevice &ioDevice, QObject *parent);
36     bool setIODevice(QIODevice &ioDevice);
37 
38     // Getting data out of Chunks
39     QByteArray data(qint64 pos=0, qint64 count=-1, QByteArray *highlighted=0);
40     bool write(QIODevice &iODevice, qint64 pos=0, qint64 count=-1);
41 
42     // Set and get highlighting infos
43     void setDataChanged(qint64 pos, bool dataChanged);
44     bool dataChanged(qint64 pos);
45 
46     // Search API
47     qint64 indexOf(const QByteArray &ba, qint64 from);
48     qint64 lastIndexOf(const QByteArray &ba, qint64 from);
49 
50     // Char manipulations
51     bool insert(qint64 pos, char b);
52     bool overwrite(qint64 pos, char b);
53     bool removeAt(qint64 pos);
54 
55     // Utility functions
56     char operator[](qint64 pos);
57     qint64 pos();
58     qint64 size();
59 
60 
61 private:
62     int getChunkIndex(qint64 absPos);
63 
64     QIODevice * _ioDevice;
65     qint64 _pos;
66     qint64 _size;
67     QList<Chunk> _chunks;
68 
69 #ifdef MODUL_TEST
70 public:
71     int chunkSize();
72 #endif
73 };
74 
75 /** \endcond docNever */
76 
77 #endif // CHUNKS_H
78