-
Notifications
You must be signed in to change notification settings - Fork 5
/
asterixReportGenerator.cpp
260 lines (204 loc) · 6.47 KB
/
asterixReportGenerator.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* This application is free software and is released under the terms of
* the BSD license. See LICENSE file for details.
*
* Copyright (c) 2016 Volker Poplawski ([email protected])
*/
#include "asterixReportGenerator.h"
#include <QProgressDialog>
#include "global.h"
#include "asterixModel.h"
#include "uap.h"
AsterixReportGenerator::AsterixReportGenerator( QObject *parent) :
QObject(parent)
{
}
void AsterixReportGenerator::createReport(QTextStream& out, Type type)
{
if (g_asterixModel == 0)
return;
QScopedPointer<AbstractGenerator> generator;
switch (type)
{
case XML:
generator.reset(new XmlGenerator(out));
break;
}
QProgressDialog progressDialog("Generating Report...", "Abort", 0, g_asterixModel->numBlocks(), g_mainWindow);
progressDialog.setWindowModality(Qt::WindowModal);
for (int i = 0; i < g_asterixModel->numBlocks(); ++i)
{
progressDialog.setValue(i);
if (progressDialog.wasCanceled())
{
break;
}
const AsterixBlockInfo& blockInfo = g_asterixModel->getBlockInfos()[i];
const AsterixBlock block(g_file + blockInfo.offset, *g_uap);
handleBlock(generator.data(), block);
}
progressDialog.setValue(g_asterixModel->numBlocks()); // this will close the dialog
}
void AsterixReportGenerator::handleBlock(AbstractGenerator* gen, const AsterixBlock& block)
{
gen->startBlock(block);
for (int j = 0; j < block.numRecords(); ++j)
{
const AsterixRecord& record = block.record(j);
gen->startRecord(record);
gen->startDataItemsSection(record);
for (int k = 0; k < record.numFields(); ++k)
{
const AsterixDataItem &dataItem = record.field(k);
const UapDataItem &uap = *dataItem.uapDataItem();
gen->startDataItem(dataItem, uap);
if (dataItem.numSubfields() == 0)
{
foreach (const UapField& uapField, uap.m_fields)
{
if (not uap.fieldPresent(dataItem.data(), uapField))
continue;
// suppress output of spare fields
if (uapField.name() == "Spare")
continue;
gen->startFinishField(dataItem, uapField);
}
}
else
{
for (int si = 0; si < dataItem.numSubfields(); si++)
{
const AsterixDataItem& subField = dataItem.subField(si);
const UapDataItem& subUap = *subField.uapDataItem();
gen->startDataItem(subField, subUap, 1);
foreach (const UapField& uapField, subUap.m_fields)
{
if (not subUap.fieldPresent(subField.data(), uapField))
continue;
// suppress output of spare fields
if (uapField.name() == "Spare")
continue;
gen->startFinishField(subField, uapField, 1);
}
gen->finishDataItem(subField, *subField.uapDataItem(), 1);
}
}
gen->finishDataItem(dataItem, uap);
}
gen->finishDataItemsSection(record);
gen->finishRecord(record);
}
gen->finishBlock(block);
}
AbstractGenerator::Field AbstractGenerator::decodeField(const AsterixDataItem& dataItem, const UapField& uapField)
{
const int itemLength = (dataItem.uapDataItem()->format() == UapDataItem::VARIABLE) ? 1 : dataItem.length();
const QByteArray field = dataItem.bitfield(uapField.octed(), itemLength, uapField.msb()-1, uapField.lsb()-1);
int value = 0;
Field ret;
ret.hasraw = false;
switch (uapField.format())
{
case UapField::ICAO6STR:
ret.value = AsterixDataItem::decodeIcaoStr((const uchar*)field.constData());
break;
case UapField::UINTEGER:
ret = unitAndScale(uapField, bitfieldToUInt(field));
break;
case UapField::INTEGER:
ret = unitAndScale(uapField, bitfieldToInt(field, uapField.msb()-1 - (((uapField.lsb()-1)/8)*8)));
break;
case UapField::OCTAL:
ret.rawval = bitfieldToUInt(field);
ret.hasraw = true;
ret.value = QString("0%1").arg(ret.rawval, 0, 8);
ret.unit = uapField.unitStr(uapField.unit());
break;
case UapField::ASCII:
ret.value = QString::fromLatin1(field);
break;
case UapField::HEX:
default:
ret.value = "0x";
for (int i=0; i < field.size(); i++)
{
ret.value += QString("%1").arg((uint)((uchar)field[i]), 2, 16, QChar('0')).toUpper();
}
break;
}
// place enum description (if ex.) in 'unit' field
QMap<int, UapEnum>::const_iterator iterator = uapField.enums().constFind(value);
if (iterator != uapField.enums().end())
{
ret.unit = iterator.value().m_desc;
}
return ret;
}
AbstractGenerator::Field AbstractGenerator::unitAndScale(const UapField& uapField, qint64 rawvalue)
{
Field f;
f.rawval = rawvalue;
f.hasraw = true;
if (uapField.unit() != UapField::UNDEFINED)
{
f.value = QString::number((double)rawvalue * uapField.scale());
f.unit = uapField.unitStr(uapField.unit());
}
else
{
f.value = QString::number(rawvalue);
}
return f;
}
void XmlGenerator::startBlock(const AsterixBlock& block)
{
ind(0) << R"(<block cat=")" << block.category() << R"(" len=")" << block.length() << R"(">)" << endl;
}
void XmlGenerator::finishBlock(const AsterixBlock&)
{
ind(0) << R"(</block>)" << endl;
}
void XmlGenerator::startRecord(const AsterixRecord& record)
{
ind(1) << R"(<record len=")" << record.length() << R"(">)" << endl;
}
void XmlGenerator::finishRecord(const AsterixRecord&)
{
ind(1) << R"(</record>)" << endl;
}
void XmlGenerator::startDataItem(const AsterixDataItem& dataItem, const UapDataItem& uap, int i)
{
ind(2+i) << R"(<dataitem type=")" << uap.id() << R"(" frn=")" << uap.frn() << R"(" len=")" << dataItem.length() << R"(">)" << endl;
}
void XmlGenerator::finishDataItem(const AsterixDataItem&, const UapDataItem&, int i)
{
ind(2+i) << R"(</dataitem>)" << endl;
}
void XmlGenerator::startFinishField(const AsterixDataItem& dataItem, const UapField& uap, int i)
{
ind(3+i) << R"(<field name=")" << uap.name() << R"(">)" << endl;
ind(4+i) << R"(<desc>)" << wrapInCData(uap.desc()) << R"(</desc>)" << endl; // desc might contain non xml characters
Field f = decodeField(dataItem, uap);
ind(4+i) << R"(<value)";
if (f.hasraw)
{
ind(0) << R"( raw=")" << f.rawval << R"(")";
}
if (not f.unit.isEmpty())
{
ind(0) << R"( unit=")" << f.unit << R"(")";
}
ind(0) << R"(>)" << f.value << R"(</value>)" << endl;
ind(3+i) << R"(</field>)" << endl;
}
QString XmlGenerator::wrapInCData(const QString& s)
{
if (s.isEmpty())
{
return QString();
}
else
{
return "<![CDATA[" + s + "]]>";
}
}