-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataprocessor.cpp
More file actions
185 lines (159 loc) · 4.92 KB
/
dataprocessor.cpp
File metadata and controls
185 lines (159 loc) · 4.92 KB
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
#include "dataprocessor.h"
#include <QStringDecoder>
/**
* @brief DataProcessor 实现
*
* Requirements: 3.1, 3.2, 3.3
*/
DataProcessor::DataProcessor(QObject *parent)
: QObject(parent)
, m_format(ASCII)
, m_timestampEnabled(false)
, m_encoding(AppSettings::ANSI)
, m_hexNewlineEnabled(true)
{
}
void DataProcessor::setFormat(Format format)
{
m_format = format;
}
DataProcessor::Format DataProcessor::format() const
{
return m_format;
}
void DataProcessor::setTimestampEnabled(bool enabled)
{
m_timestampEnabled = enabled;
}
bool DataProcessor::isTimestampEnabled() const
{
return m_timestampEnabled;
}
void DataProcessor::setEncoding(AppSettings::Encoding encoding)
{
m_encoding = encoding;
}
AppSettings::Encoding DataProcessor::encoding() const
{
return m_encoding;
}
void DataProcessor::setHexNewlineEnabled(bool enabled)
{
m_hexNewlineEnabled = enabled;
}
bool DataProcessor::isHexNewlineEnabled() const
{
return m_hexNewlineEnabled;
}
void DataProcessor::process(const QByteArray &data)
{
if (data.isEmpty()) {
return;
}
QString result;
// 添加时间戳(如果启用)
// Requirements: 3.2
if (m_timestampEnabled) {
result = formatTimestamp() + " >>";
}
// 根据格式转换数据
// Requirements: 3.1
switch (m_format) {
case ASCII:
result += toAsciiString(data);
break;
case Hexadecimal:
result += toHexString(data);
break;
}
// 发出处理完成信号
// Requirements: 3.3
emit dataProcessed(result);
}
QString DataProcessor::toHexString(const QByteArray &data) const
{
// 转换为大写十六进制,每字节用空格分隔
// 当 m_hexNewlineEnabled 为 true 时,换行符(0x0A)和回车符(0x0D)单独显示并在前后添加换行
// 当 m_hexNewlineEnabled 为 false 时,0x0A 和 0x0D 作为普通十六进制值显示
// Requirements: 2.2, 2.3
QString result;
result.reserve(data.size() * 4); // 预分配空间
for (int i = 0; i < data.size(); ++i) {
quint8 byte = static_cast<quint8>(data.at(i));
// 处理换行符序列 (0x0D 0x0A 或单独的 0x0A 或 0x0D)
// Requirements: 2.2 - 当启用时插入换行
// Requirements: 2.3 - 当禁用时显示为普通十六进制值
if (m_hexNewlineEnabled && (byte == 0x0A || byte == 0x0D)) {
if (!result.isEmpty() && !result.endsWith('\n')) {
result += '\n';
}
// 收集连续的换行符
QString controlSequence = "[" + QString("%1").arg(byte, 2, 16, QChar('0')).toUpper();
// 查看后续字节是否也是换行符
while (i + 1 < data.size()) {
quint8 nextByte = static_cast<quint8>(data.at(i + 1));
if (nextByte == 0x0A || nextByte == 0x0D) {
controlSequence += " " + QString("%1").arg(nextByte, 2, 16, QChar('0')).toUpper();
i++; // 移动到下一个字节
} else {
break;
}
}
controlSequence += "]\n";
result += controlSequence;
} else {
// 普通字节处理(或禁用换行时的 0x0A/0x0D)
if (!result.isEmpty() && !result.endsWith(' ') && !result.endsWith('\n')) {
result += ' ';
}
result += QString("%1").arg(byte, 2, 16, QChar('0')).toUpper();
}
}
// 确保结果末尾没有多余的空格
if (result.endsWith(' ')) {
result.chop(1);
}
return result;
}
QString DataProcessor::toAsciiString(const QByteArray &data) const
{
// 根据当前编码设置转换字节数据
// Requirements: 1.2
switch (m_encoding) {
case AppSettings::UTF8: {
// 使用 UTF-8 编码转换 (Qt 6 QStringDecoder)
auto decoder = QStringDecoder(QStringDecoder::Utf8);
if (decoder.isValid()) {
QString result = decoder(data);
return result;
}
break;
}
case AppSettings::GBK: {
// 使用 GBK 编码转换 (Qt 6 QStringDecoder)
auto decoder = QStringDecoder("GBK");
if (decoder.isValid()) {
QString result = decoder(data);
return result;
}
// GBK 可能不可用,回退到 System 编码
auto sysDecoder = QStringDecoder(QStringDecoder::System);
if (sysDecoder.isValid()) {
return sysDecoder(data);
}
break;
}
case AppSettings::ANSI:
default:
// 使用 Latin1 编码转换(ANSI/Windows 本地编码)
return QString::fromLatin1(data);
}
// 回退到 Latin1
return QString::fromLatin1(data);
}
QString DataProcessor::formatTimestamp() const
{
// 格式:HH:mm:ss.zzz
// Requirements: 3.2
return QDateTime::currentDateTime().toString("HH:mm:ss.zzz");
}