forked from KoynovStas/QCRC_Calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_calc_for_text.cpp
174 lines (102 loc) · 3.07 KB
/
crc_calc_for_text.cpp
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
#include <QMetaType>
#include <QTextCodec>
#include <QSet>
#include "crc_calc_for_text.h"
const QList<QByteArray> CRC_Calc_for_Text::Encodings = CRC_Calc_for_Text::get_Encodings();
CRC_Calc_for_Text::CRC_Calc_for_Text() :
QObject(NULL),
// public
with_BOM(false),
end_line_format(EndLine_LF),
// private
encoding_index(0),
num_lines(0)
{
qRegisterMetaType<uint64_t>("uint64_t");
this->moveToThread(&thread);
thread.start();
QObject::connect(this, SIGNAL(run_calculate(const QString &)),
this, SLOT(_calculate(const QString &)) );
}
CRC_Calc_for_Text::~CRC_Calc_for_Text()
{
thread.quit();
thread.wait();
}
int CRC_Calc_for_Text::set_encoding_index(size_t new_index)
{
if( new_index >= (size_t)Encodings.size() )
return -1; //error
encoding_index = new_index;
return 0; //good job
}
void CRC_Calc_for_Text::calculate(const QString &data)
{
emit run_calculate(data);
}
void CRC_Calc_for_Text::_calculate(const QString &data)
{
if( data.isEmpty() )
{
emit error("String is empty");
return;
}
num_lines = data.count('\n') + 1; // +1 last string have no EndLine char
replace_end_line(data);
encoding_str();
if( ucrc )
emit calculated( ucrc->get_crc(raw_str.data(), raw_str.size() ) );
}
void CRC_Calc_for_Text::replace_end_line(const QString &data)
{
tmp_str = data;
switch( end_line_format )
{
case EndLine_LF:
tmp_str.replace('\n', QLatin1String("\n"));
break;
case EndLine_CR:
tmp_str.replace('\n', QLatin1String("\r"));
break;
case EndLine_RS:
tmp_str.replace('\n', QLatin1String("\x1E"));
break;
case EndLine_9B:
tmp_str.replace('\n', QLatin1String("\x9B"));
break;
case EndLine_CRLF:
tmp_str.replace('\n', QLatin1String("\r\n"));
break;
case EndLine_LFCR:
tmp_str.replace('\n', QLatin1String("\n\r"));
break;
default:
break;
}
}
void CRC_Calc_for_Text::encoding_str()
{
if( encoding_index == 0 ) //ASCII
{
raw_str = tmp_str.toLatin1();
}
else
{
QTextCodec *text_codec = QTextCodec::codecForName(Encodings[encoding_index]);
QTextCodec::ConverterState state(with_BOM ? QTextCodec::DefaultConversion : QTextCodec::IgnoreHeader);
raw_str = text_codec->fromUnicode(tmp_str.constData(), tmp_str.size(), &state);
}
}
const QList<QByteArray> CRC_Calc_for_Text::get_Encodings()
{
// list from Set - list have no a duplicate
QList<QByteArray> list = QList<QByteArray>::fromSet( QTextCodec::availableCodecs().toSet() );
std::sort(list.begin(), list.end());
QList<QByteArray>::iterator it = std::find(list.begin(), list.end(), "ASCII");
if( it != list.end() )
{
list.erase(it);
}
list.push_front("ASCII"); // ASCII always have the index 0
return list;
}