Skip to content

Commit a9f956a

Browse files
7134956bbogush
authored andcommitted
A file instead of a read/write buffer.
1 parent 7f0379c commit a9f956a

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
@@ -8,13 +8,13 @@
88
BufferTableModel::BufferTableModel(QObject *parent):
99
QAbstractTableModel(parent)
1010
{
11-
buf = nullptr;
12-
bufSize = 0;
11+
state.bufFilePos = 0;
12+
state.fileSize = 0;
1313
}
1414

1515
int BufferTableModel::rowCount(const QModelIndex & /*parent*/) const
1616
{
17-
return (bufSize + ROW_DATA_SIZE - 1) / ROW_DATA_SIZE;
17+
return (state.fileSize + ROW_DATA_SIZE - 1) / ROW_DATA_SIZE;
1818
}
1919

2020
int BufferTableModel::columnCount(const QModelIndex & /*parent*/) const
@@ -26,32 +26,42 @@ QVariant BufferTableModel::data(const QModelIndex &index, int role) const
2626
{
2727
QString hexString;
2828
QChar decodedChar;
29-
uint32_t start, end;
29+
qint64 start, end;
3030

3131
if (role == Qt::DisplayRole)
3232
{
3333
switch (index.column())
3434
{
3535
case HEADER_ADDRESS_COL:
36-
return QString("%1").arg(index.row() * ROW_DATA_SIZE, 8, 16,
36+
return QString("%1").arg(index.row() * ROW_DATA_SIZE, 10, 16,
3737
QChar('0'));
3838
case HEADER_HEX_COL:
3939
start = static_cast<uint32_t>(index.row()) * ROW_DATA_SIZE;
4040
end = start + ROW_DATA_SIZE;
4141

42-
for (uint32_t i = start; i < end && i < bufSize; i++)
42+
if ((sPtr->bufFilePos > start) || ((sPtr->bufFilePos + BUF_SIZE) < end))
4343
{
44-
hexString.append(QString("%1 ").arg(buf[i], 2, 16,
44+
sPtr->bufFilePos = start - (BUF_SIZE / 2);
45+
if (sPtr->bufFilePos < 0)
46+
sPtr->bufFilePos = 0;
47+
48+
sPtr->file.seek(sPtr->bufFilePos);
49+
sPtr->file.read((char *)sPtr->buf, BUF_SIZE);
50+
}
51+
52+
for (qint64 i = start; i < end && i < state.fileSize; i++)
53+
{
54+
hexString.append(QString("%1 ").arg(sPtr->buf[i - sPtr->bufFilePos], 2, 16,
4555
QChar('0')));
4656
}
4757
return hexString;
4858
case HEADER_ASCII_COL:
4959
start = static_cast<uint32_t>(index.row()) * ROW_DATA_SIZE;
5060
end = start + ROW_DATA_SIZE;
5161

52-
for (uint32_t i = start; i < end && i < bufSize; i++)
62+
for (qint64 i = start; i < end && i < state.fileSize; i++)
5363
{
54-
decodedChar = QChar(buf[i]);
64+
decodedChar = QChar(state.buf[i - state.bufFilePos]);
5565
if (!decodedChar.isPrint())
5666
decodedChar = QChar('.');
5767
hexString.append(decodedChar);
@@ -85,16 +95,25 @@ QVariant BufferTableModel::headerData(int section, Qt::Orientation orientation,
8595
return QVariant();
8696
}
8797

88-
void BufferTableModel::setBuffer(uint8_t *buffer, uint32_t size)
98+
void BufferTableModel::setFile(QString filePath)
8999
{
90100
beginResetModel();
91-
buf = buffer;
92-
bufSize = size;
101+
state.file.close();
102+
if (!filePath.isEmpty())
103+
{
104+
state.bufFilePos = 0;
105+
state.fileSize = 0;
106+
}
107+
state.file.setFileName(filePath);
108+
if (state.file.open(QIODevice::ReadOnly))
109+
{
110+
state.fileSize = state.file.size();
111+
state.bufFilePos = INT64_MAX;
112+
}
113+
else
114+
{
115+
state.bufFilePos = 0;
116+
state.fileSize = 0;
117+
}
93118
endResetModel();
94119
}
95-
96-
void BufferTableModel::getBuffer(uint8_t *&buffer, uint32_t &size)
97-
{
98-
buffer = buf;
99-
size = bufSize;
100-
}

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)