129 lines
3.3 KiB
C++
Executable File
129 lines
3.3 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Philipp Last, SLSE, 2017-12-01
|
|
*/
|
|
|
|
#include "participant_dis_helper.h"
|
|
|
|
|
|
namespace ParticipantManagement
|
|
{
|
|
namespace DIS
|
|
{
|
|
const QRegularExpression gRegExValidDIS(QStringLiteral("\\b([0-9])\\." // 8 Bit: Kind Field, exact values between 0 and 9
|
|
"(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 8 bit unspecified: Domain Field
|
|
"(26[0-6]|2[0-5][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 16 bit but highest value is 266 which is 'Uzbekistan'
|
|
"(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}" // 4x 8 bit unspecified: Category, Subcategory, Specific, Extra
|
|
"(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\b")); // Disable leading zeros
|
|
|
|
/*
|
|
* Converts a given integer to a MSDL QString which has 21 characters.
|
|
* Adds leading zeros if missing.
|
|
*
|
|
* E.g: 3001222001246001000 (integer) becomes 003001222001246001000 (QString)
|
|
*/
|
|
QString convertDISTypeInteger2MSDLString(int64_t distype)
|
|
{
|
|
QString strDisType = QString::number(distype);
|
|
|
|
// Two leading zeros might be missing if the dis entity kind is small than 10 (-> [0, 255] i spossible]
|
|
while (strDisType.size() < 21)
|
|
strDisType.insert(0, QLatin1Char('0'));
|
|
|
|
return strDisType;
|
|
}
|
|
|
|
|
|
/*
|
|
* Converts a DIS type QString to an integer.
|
|
* Adds leading zeros (if missing) to each dis attribute.
|
|
*
|
|
* Example: 3.1.222.1.246.1.0 -> 3001222001246001000
|
|
*/
|
|
int64_t convertDISTypeString2Integer(const QString &strDisType)
|
|
{
|
|
const QStringList list = strDisType.split(QLatin1Char('.'));
|
|
|
|
if (list.size() != 7)
|
|
return -1;
|
|
|
|
QString strNumber(list[0]); // Prevent leading zeros
|
|
|
|
for (int i = 1; i < list.size(); ++i)
|
|
{
|
|
QString attribute = list[i];
|
|
while (attribute.size() < 3)
|
|
attribute.insert(0, QLatin1Char('0'));
|
|
|
|
strNumber += attribute;
|
|
}
|
|
|
|
bool ok;
|
|
int64_t number = strNumber.toLongLong(&ok, 10);
|
|
|
|
if (!ok)
|
|
return -1;
|
|
|
|
return number;
|
|
}
|
|
|
|
|
|
/*
|
|
* Converts a DIS type integer to a QString which can be correct (according to the DIS standard) visualized to the user.
|
|
* E.g. 3001222001246001000 (int) becomes 3.1.222.1.246.1.0 (QString)
|
|
*/
|
|
QString convertDISTypeInteger2DISString(int64_t distype)
|
|
{
|
|
QString strMSDL = convertDISTypeInteger2MSDLString(distype);
|
|
QString strDis;
|
|
|
|
if (strMSDL.size() != 21)
|
|
return strDis;
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < 7; ++i)
|
|
{
|
|
QString part = strMSDL.mid(index, 3);
|
|
part.remove(QRegularExpression(QStringLiteral("^[0]*")));
|
|
|
|
// It may be possible that a part is "000" which would be completely erased
|
|
if (!part.size())
|
|
part = QStringLiteral("0");
|
|
|
|
strDis += part;
|
|
|
|
if (i != 6)
|
|
strDis += QLatin1Char('.');
|
|
|
|
index += 3;
|
|
}
|
|
|
|
return strDis;
|
|
}
|
|
|
|
|
|
int64_t convertMSDLString2Integer(const QString &strMSDL, bool *ok)
|
|
{
|
|
int64_t number = strMSDL.toLongLong(ok, 10);
|
|
return number;
|
|
}
|
|
|
|
|
|
QString convertMSDLString2DISString(const QString &strMSDL)
|
|
{
|
|
bool ok;
|
|
int64_t number = convertMSDLString2Integer(strMSDL, &ok);
|
|
|
|
if (!ok)
|
|
return QString();
|
|
|
|
return convertDISTypeInteger2DISString(number);
|
|
}
|
|
|
|
}
|
|
}
|