Skip to content

Commit bcd4fe7

Browse files
committed
Revert "A file instead of a read/write buffer."
This reverts commit a9f956a.
1 parent 14cd7c3 commit bcd4fe7

12 files changed

+243
-271
lines changed

qt/buffer_table_model.cpp

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

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

2121
// Limit buffer size to avoid memory overlows for large buffers
2222
if (tableBufferSize > TABLE_BUFFER_LIMIT)
@@ -34,42 +34,32 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
3434
{
3535
QString hexString;
3636
QChar decodedChar;
37-
qint64 start, end;
37+
uint32_t 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, 10, 16,
44+
return QString("%1").arg(index.row() * ROW_DATA_SIZE, 8, 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-
if ((sPtr->bufFilePos > start) || ((sPtr->bufFilePos + BUF_SIZE) < end))
50+
for (uint32_t i = start; i < end && i < bufSize; i++)
5151
{
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,
6353
QChar('0')));
6454
}
6555
return hexString;
6656
case HEADER_ASCII_COL:
6757
start = static_cast<uint32_t>(index.row()) * ROW_DATA_SIZE;
6858
end = start + ROW_DATA_SIZE;
6959

70-
for (qint64 i = start; i < end && i < state.fileSize; i++)
60+
for (uint32_t i = start; i < end && i < bufSize; i++)
7161
{
72-
decodedChar = QChar(state.buf[i - state.bufFilePos]);
62+
decodedChar = QChar(buf[i]);
7363
if (!decodedChar.isPrint())
7464
decodedChar = QChar('.');
7565
hexString.append(decodedChar);
@@ -103,25 +93,16 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
10393
return QVariant();
10494
}
10595

106-
void BufferTableModel::setFile(QString filePath)
96+
void BufferTableModel::setBuffer(uint8_t *buffer, uint32_t size)
10797
{
10898
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;
126101
endResetModel();
127102
}
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: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,19 @@
77
#define BUFFER_TABLE_MODEL_H
88

99
#include <QAbstractTableModel>
10-
#include <QFile>
1110

1211
#define HEADER_ADDRESS_COL 0
1312
#define HEADER_HEX_COL 1
1413
#define HEADER_ASCII_COL 2
1514

1615
#define ROW_DATA_SIZE 16
17-
#define BUF_SIZE 4000
1816

1917
class BufferTableModel: public QAbstractTableModel
2018
{
2119
Q_OBJECT
2220

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;
21+
uint8_t *buf;
22+
uint32_t bufSize;
3323

3424
public:
3525
BufferTableModel(QObject *parent = nullptr);
@@ -40,8 +30,8 @@ class BufferTableModel: public QAbstractTableModel
4030
const override;
4131
QVariant headerData(int section, Qt::Orientation orientation, int role)
4232
const override;
43-
void setFile(QString filePath);
44-
33+
void setBuffer(uint8_t *buffer, uint32_t size);
34+
void getBuffer(uint8_t *&buffer, uint32_t &size);
4535
};
4636

4737
#endif // BUFFER_TABLE_MODEL_H

0 commit comments

Comments
 (0)