forked from Dax89/QHexView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqhexeditdata.h
300 lines (236 loc) · 11.4 KB
/
qhexeditdata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef QHEXEDITDATA_H
#define QHEXEDITDATA_H
/*
* Piece Chain Data Structure Documentation From:
* 1) http://www.catch22.net/tuts/piece-chains
* 2) http://www.catch22.net/tuts/memory-techniques-part-1
* 3) http://www.catch22.net/tuts/memory-techniques-part-2
*/
#include <QtCore>
#include <QUndoStack>
#include <QUndoCommand>
class QHexEditData : public QObject
{
Q_OBJECT
public:
enum ActionType { None = 0, Insert = 1, Remove = 2, Replace = 3 };
private:
class ModifiedItem
{
public:
ModifiedItem(qint64 pos, qint64 len, bool mod = true): _pos(pos), _len(len), _mod(mod) {}
qint64 pos() const { return this->_pos; }
qint64 length() const { return this->_len; }
bool modified() const { return this->_mod; }
void updatePos(qint64 amt) { this->_pos += amt; }
void updateLen(qint64 amt) { this->_len += amt; }
private:
qint64 _pos;
qint64 _len;
bool _mod;
};
typedef QList<ModifiedItem*> ModifyList;
public:
class AbstractCommand: public QUndoCommand
{
public:
AbstractCommand(qint64 pos, QHexEditData* owner, QUndoCommand* parent = 0): QUndoCommand(parent), _owner(owner), _notify(true), _pos(pos) { }
public:
bool canNotify() const { return this->_notify; }
void setNotify(bool b) { this->_notify = b; }
qint64 pos() const { return this->_pos; }
QHexEditData* owner() const { return this->_owner; }
protected:
void notifyDataChanged(qint64 offset, qint64 size, QHexEditData::ActionType reason)
{
emit this->owner()->dataChanged(offset, size, reason);
}
private:
QHexEditData* _owner;
bool _notify;
qint64 _pos;
};
class ModifyRangeCommand: public AbstractCommand
{
public:
ModifyRangeCommand(int index, qint64 pos, const ModifyList& oldml, const ModifyList& newml, QHexEditData* owner, QUndoCommand* parent = 0): AbstractCommand(pos, owner, parent), _index(index), _oldml(oldml), _newml(newml)
{
this->_oldlength = this->_newlength = 0;
for(int i = 0; i < oldml.length(); i++)
this->_oldlength += oldml[i]->length();
for(int i = 0; i < newml.length(); i++)
this->_newlength += newml[i]->length();
}
protected:
qint64 index() const { return this->_index; }
qint64 oldLength() const { return this->_oldlength; }
qint64 newLength() const { return this->_newlength; }
private:
int _index;
protected:
QHexEditData::ModifyList _oldml;
QHexEditData::ModifyList _newml;
qint64 _oldlength;
qint64 _newlength;
};
class InsertCommand: public ModifyRangeCommand
{
public:
InsertCommand(int index, qint64 pos, const ModifyList& oldml, const ModifyList& newml, QHexEditData* owner, bool opt, QUndoCommand* parent = 0): ModifyRangeCommand(index, pos, oldml, newml, owner, parent), _optimized(opt) { }
virtual int id() const { return QHexEditData::Insert; }
virtual bool mergeWith(const QUndoCommand* command)
{
const InsertCommand* ic = dynamic_cast<const InsertCommand*>(command);
if(ic->optimized())
{
ModifiedItem* oldmi = nullptr;
if(this->_newml.length() == 1) /* Single Item */
oldmi = this->_newml[0];
else /* Splitted Span in Three Parts, the second is the new one, update length */
oldmi = this->_newml[1];
ModifiedItem* newmi = ic->_newml.last();
this->_newlength += ic->_newlength;
oldmi->updateLen(newmi->length());
this->notifyDataChanged(ic->pos(), ic->newLength(), QHexEditData::Insert);
return true;
}
return false;
}
virtual void undo()
{
for(int i = 0; i < this->_newml.length(); i++)
this->owner()->_modlist.removeAt(this->index());
for(int i = 0; i < this->_oldml.length(); i++)
this->owner()->_modlist.insert(this->index() + i, this->_oldml[i]);
/* Update HexEditData's length */
this->owner()->_length -= this->newLength();
this->owner()->_length += this->oldLength();
if(this->canNotify())
this->notifyDataChanged(this->pos(), this->oldLength(), QHexEditData::Insert);
}
virtual void redo()
{
if(this->_optimized)
return; /* Don't modify the list if this is an optimized insertion */
for(int i = 0; i < this->_oldml.length(); i++)
this->owner()->_modlist.removeAt(this->index());
for(int i = 0; i < this->_newml.length(); i++)
this->owner()->_modlist.insert(this->index() + i, this->_newml[i]);
if(this->canNotify())
this->notifyDataChanged(this->pos(), this->newLength(), QHexEditData::Insert);
}
private:
bool optimized() const { return this->_optimized; }
private:
bool _optimized;
};
class RemoveCommand: public ModifyRangeCommand
{
public:
RemoveCommand(int index, qint64 pos, const ModifyList& oldml, const ModifyList& newml, QHexEditData* owner, QUndoCommand* parent = 0): ModifyRangeCommand(index, pos, oldml, newml, owner, parent) { }
virtual int id() const { return QHexEditData::Remove; }
virtual void undo()
{
this->owner()->_modlist.erase(this->owner()->_modlist.begin() + this->index(), this->owner()->_modlist.begin() + (this->index() + this->_newml.length()));
for(int i = 0; i < this->_oldml.length(); i++)
this->owner()->_modlist.insert(this->index() + i, this->_oldml.at(i));
/* Update HexEditData's length */
this->owner()->_length -= this->newLength();
this->owner()->_length += this->oldLength();
if(this->canNotify())
this->notifyDataChanged(this->pos(), this->oldLength(), QHexEditData::Remove);
}
virtual void redo()
{
this->owner()->_modlist.erase(this->owner()->_modlist.begin() + this->index(), this->owner()->_modlist.begin() + (this->index() + this->_oldml.length()));
for(int i = 0; i < this->_newml.length(); i++)
this->owner()->_modlist.insert(this->index() + i, this->_newml.at(i));
if(this->canNotify())
this->notifyDataChanged(this->pos(), this->newLength(), QHexEditData::Remove);
}
};
class ReplaceCommand: public AbstractCommand
{
public:
ReplaceCommand(qint64 pos, qint64 len, const QByteArray& ba, QHexEditData* owner, QUndoCommand* parent = 0): AbstractCommand(pos, owner, parent), _len(len), _data(ba), _remcmd(nullptr), _inscmd(nullptr) { }
virtual int id() const { return QHexEditData::Replace; }
virtual void undo()
{
this->_inscmd->undo();
if(this->_remcmd) /* 'remcmd' is NULL if the replace command is done at EOF */
this->_remcmd->undo();
if(this->canNotify())
emit this->notifyDataChanged(this->pos(), this->_data.length(), QHexEditData::Replace);
}
virtual void redo()
{
if(this->_remcmd && this->_inscmd)
{
this->_remcmd->redo();
this->_inscmd->redo();
}
else
{
qint64 l = qMin(this->_len, this->owner()->length());
this->_remcmd = this->owner()->internalRemove(this->pos(), l, QHexEditData::Replace);
if(this->_remcmd)
{
this->_remcmd->setNotify(false); /* Do not emit signal */
this->_remcmd->redo();
}
this->_inscmd = this->owner()->internalInsert(this->pos(), this->_data, QHexEditData::Replace);
if(this->_inscmd)
{
this->_inscmd->setNotify(false); /* Do not emit signal */
this->_inscmd->redo();
}
}
if(this->canNotify())
this->notifyDataChanged(this->pos(), this->_data.length(), QHexEditData::Replace);
}
private:
qint64 _len;
QByteArray _data;
RemoveCommand* _remcmd;
InsertCommand* _inscmd;
};
private:
explicit QHexEditData(QIODevice* iodevice, QObject *parent = 0);
~QHexEditData();
public:
QUndoStack* undoStack();
qint64 length() const;
bool isReadOnly() const;
public slots:
bool save();
bool saveTo(QIODevice* iodevice);
public:
static QHexEditData* fromDevice(QIODevice* iodevice);
static QHexEditData* fromFile(QString filename);
static QHexEditData* fromMemory(const QByteArray& ba);
private:
InsertCommand* internalInsert(qint64 pos, const QByteArray& ba, QHexEditData::ActionType act);
RemoveCommand* internalRemove(qint64 pos, qint64 len, QHexEditData::ActionType act); /* TODO: QHexEditData::internalRemove(): Optimization Needed */
QHexEditData::ModifiedItem *modifiedItem(qint64 pos, qint64 *datapos = nullptr, int* index = nullptr);
qint64 updateBuffer(const QByteArray& ba);
bool canOptimize(QHexEditData::ActionType at, qint64 pos);
void recordAction(QHexEditData::ActionType at, qint64 pos);
signals:
void dataChanged(qint64 offset, qint64 size, QHexEditData::ActionType reason);
private:
static const qint64 BUFFER_SIZE;
ModifyList _modlist;
QIODevice* _iodevice;
QByteArray _modbuffer;
qint64 _length;
qint64 _devicelength;
qint64 _lastpos;
QHexEditData::ActionType _lastaction;
QUndoStack _undostack;
QMutex _mutex;
friend class QHexEditData::AbstractCommand;
friend class QHexEditDataReader;
friend class QHexEditDataWriter;
};
Q_DECLARE_METATYPE(QHexEditData::ActionType)
#endif // QHEXEDITDATA_H