10
10
BufferTableModel::BufferTableModel (QObject *parent):
11
11
QAbstractTableModel(parent)
12
12
{
13
- state. bufFilePos = 0 ;
14
- state. fileSize = 0 ;
13
+ buf = nullptr ;
14
+ bufSize = 0 ;
15
15
}
16
16
17
17
int BufferTableModel::rowCount (const QModelIndex & /* parent*/ ) const
18
18
{
19
- qint64 tableBufferSize = state. fileSize ;
19
+ qint64 tableBufferSize = bufSize ;
20
20
21
21
// Limit buffer size to avoid memory overlows for large buffers
22
22
if (tableBufferSize > TABLE_BUFFER_LIMIT)
@@ -34,42 +34,32 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
34
34
{
35
35
QString hexString;
36
36
QChar decodedChar;
37
- qint64 start, end;
37
+ uint32_t start, end;
38
38
39
39
if (role == Qt::DisplayRole)
40
40
{
41
41
switch (index.column ())
42
42
{
43
43
case HEADER_ADDRESS_COL:
44
- return QString (" %1" ).arg (index.row () * ROW_DATA_SIZE, 10 , 16 ,
44
+ return QString (" %1" ).arg (index.row () * ROW_DATA_SIZE, 8 , 16 ,
45
45
QChar (' 0' ));
46
46
case HEADER_HEX_COL:
47
47
start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
48
48
end = start + ROW_DATA_SIZE;
49
49
50
- if (( sPtr -> bufFilePos > start) || (( sPtr -> bufFilePos + BUF_SIZE) < end) )
50
+ for ( uint32_t i = start; i < end && i < bufSize; i++ )
51
51
{
52
- sPtr ->bufFilePos = start - (BUF_SIZE / 2 );
53
- if (sPtr ->bufFilePos < 0 )
54
- sPtr ->bufFilePos = 0 ;
55
-
56
- sPtr ->file .seek (sPtr ->bufFilePos );
57
- sPtr ->file .read ((char *)sPtr ->buf , BUF_SIZE);
58
- }
59
-
60
- for (qint64 i = start; i < end && i < state.fileSize ; i++)
61
- {
62
- hexString.append (QString (" %1 " ).arg (sPtr ->buf [i - sPtr ->bufFilePos ], 2 , 16 ,
52
+ hexString.append (QString (" %1 " ).arg (buf[i], 2 , 16 ,
63
53
QChar (' 0' )));
64
54
}
65
55
return hexString;
66
56
case HEADER_ASCII_COL:
67
57
start = static_cast <uint32_t >(index.row ()) * ROW_DATA_SIZE;
68
58
end = start + ROW_DATA_SIZE;
69
59
70
- for (qint64 i = start; i < end && i < state. fileSize ; i++)
60
+ for (uint32_t i = start; i < end && i < bufSize ; i++)
71
61
{
72
- decodedChar = QChar (state. buf [i - state. bufFilePos ]);
62
+ decodedChar = QChar (buf[i]);
73
63
if (!decodedChar.isPrint ())
74
64
decodedChar = QChar (' .' );
75
65
hexString.append (decodedChar);
@@ -103,25 +93,16 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
103
93
return QVariant ();
104
94
}
105
95
106
- void BufferTableModel::setFile (QString filePath )
96
+ void BufferTableModel::setBuffer ( uint8_t *buffer, uint32_t size )
107
97
{
108
98
beginResetModel ();
109
- state.file .close ();
110
- if (!filePath.isEmpty ())
111
- {
112
- state.bufFilePos = 0 ;
113
- state.fileSize = 0 ;
114
- }
115
- state.file .setFileName (filePath);
116
- if (state.file .open (QIODevice::ReadOnly))
117
- {
118
- state.fileSize = state.file .size ();
119
- state.bufFilePos = INT64_MAX;
120
- }
121
- else
122
- {
123
- state.bufFilePos = 0 ;
124
- state.fileSize = 0 ;
125
- }
99
+ buf = buffer;
100
+ bufSize = size;
126
101
endResetModel ();
127
102
}
103
+
104
+ void BufferTableModel::getBuffer (uint8_t *&buffer, uint32_t &size)
105
+ {
106
+ buffer = buf;
107
+ size = bufSize;
108
+ }
0 commit comments