Skip to content

Commit 88550dc

Browse files
7134956bbogush
authored andcommitted
A file instead of a read/write buffer.
This reverts commit bcd4fe7.
1 parent bcd4fe7 commit 88550dc

12 files changed

+271
-243
lines changed

qt/buffer_table_model.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
BufferTableModel::BufferTableModel(QObject *parent):
1111
QAbstractTableModel(parent)
1212
{
13-
buf = nullptr;
14-
bufSize = 0;
13+
state.bufFilePos = 0;
14+
state.fileSize = 0;
1515
}
1616

1717
int BufferTableModel::rowCount(const QModelIndex & /*parent*/) const
1818
{
19-
qint64 tableBufferSize = bufSize;
19+
qint64 tableBufferSize = state.fileSize;
2020

2121
// Limit buffer size to avoid memory overlows for large buffers
2222
if (tableBufferSize > TABLE_BUFFER_LIMIT)
@@ -34,32 +34,42 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
3434
{
3535
QString hexString;
3636
QChar decodedChar;
37-
uint32_t start, end;
37+
qint64 start, end;
3838

3939
if (role == Qt::DisplayRole)
4040
{
4141
switch (index.column())
4242
{
4343
case HEADER_ADDRESS_COL:
44-
return QString("%1").arg(index.row() * ROW_DATA_SIZE, 8, 16,
44+
return QString("%1").arg(index.row() * ROW_DATA_SIZE, 10, 16,
4545
QChar('0'));
4646
case HEADER_HEX_COL:
4747
start = static_cast<uint32_t>(index.row()) * ROW_DATA_SIZE;
4848
end = start + ROW_DATA_SIZE;
4949

50-
for (uint32_t i = start; i < end && i < bufSize; i++)
50+
if ((sPtr->bufFilePos > start) || ((sPtr->bufFilePos + BUF_SIZE) < end))
5151
{
52-
hexString.append(QString("%1 ").arg(buf[i], 2, 16,
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,
5363
QChar('0')));
5464
}
5565
return hexString;
5666
case HEADER_ASCII_COL:
5767
start = static_cast<uint32_t>(index.row()) * ROW_DATA_SIZE;
5868
end = start + ROW_DATA_SIZE;
5969

60-
for (uint32_t i = start; i < end && i < bufSize; i++)
70+
for (qint64 i = start; i < end && i < state.fileSize; i++)
6171
{
62-
decodedChar = QChar(buf[i]);
72+
decodedChar = QChar(state.buf[i - state.bufFilePos]);
6373
if (!decodedChar.isPrint())
6474
decodedChar = QChar('.');
6575
hexString.append(decodedChar);
@@ -93,16 +103,25 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
93103
return QVariant();
94104
}
95105

96-
void BufferTableModel::setBuffer(uint8_t *buffer, uint32_t size)
106+
void BufferTableModel::setFile(QString filePath)
97107
{
98108
beginResetModel();
99-
buf = buffer;
100-
bufSize = size;
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+
}
101126
endResetModel();
102127
}
103-
104-
void BufferTableModel::getBuffer(uint8_t *&buffer, uint32_t &size)
105-
{
106-
buffer = buf;
107-
size = bufSize;
108-
}

qt/buffer_table_model.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,29 @@
77
#define BUFFER_TABLE_MODEL_H
88

99
#include <QAbstractTableModel>
10+
#include <QFile>
1011

1112
#define HEADER_ADDRESS_COL 0
1213
#define HEADER_HEX_COL 1
1314
#define HEADER_ASCII_COL 2
1415

1516
#define ROW_DATA_SIZE 16
17+
#define BUF_SIZE 4000
1618

1719
class BufferTableModel: public QAbstractTableModel
1820
{
1921
Q_OBJECT
2022

21-
uint8_t *buf;
22-
uint32_t bufSize;
23+
typedef struct
24+
{
25+
uint8_t buf[BUF_SIZE];
26+
qint64 fileSize;
27+
qint64 bufFilePos;
28+
QFile file;
29+
} BufferModelState;
30+
31+
BufferModelState state;
32+
BufferModelState *sPtr = &state;
2333

2434
public:
2535
BufferTableModel(QObject *parent = nullptr);
@@ -30,8 +40,8 @@ class BufferTableModel: public QAbstractTableModel
3040
const override;
3141
QVariant headerData(int section, Qt::Orientation orientation, int role)
3242
const override;
33-
void setBuffer(uint8_t *buffer, uint32_t size);
34-
void getBuffer(uint8_t *&buffer, uint32_t &size);
43+
void setFile(QString filePath);
44+
3545
};
3646

3747
#endif // BUFFER_TABLE_MODEL_H

0 commit comments

Comments
 (0)