Skip to content

Commit

Permalink
isValidCall and Callsign::operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ea4k committed Dec 30, 2024
1 parent 668c895 commit 8864964
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 90 deletions.
66 changes: 48 additions & 18 deletions src/callsign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,33 @@
*****************************************************************************/
#include "callsign.h"

Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
fullCall(callsign.toUpper()), homeAreaNumber(0), valid(false), prefValid(false)

Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent}
{
initialize(callsign);
}


//Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
// fullCall(callsign.toUpper()), homeAreaNumber(0), valid(false), prefValid(false)
//{
// //qDebug() << Q_FUNC_INFO << ": " << callsign;
// initialize(callsign);
//}

Callsign::~Callsign(){}

void Callsign::operator()(const QString &newCallsign)
{
initialize(newCallsign);
}

void Callsign::initialize(const QString &callsign)
{
//qDebug() << Q_FUNC_INFO << ": " << callsign;

clear(); // Clear existing data
fullCall = callsign.toUpper();


bool hostAreaNumberExist1 = false; // Helper to check if the value exists or not
bool hostSpecialNumberExist1 = false; // Helper to check if the value exists or not
Expand All @@ -55,7 +78,7 @@ Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
QString suffix; // String containing Suffixes like QRP in EA4K/QRP but also 3D2 in EA4K/3D2

if (fullCall.contains('\\')) // Lets normalize complx calls
fullCall.replace('\\', '/');
fullCall.replace('\\', '/');

QString string2test = fullCall;

Expand Down Expand Up @@ -86,10 +109,10 @@ Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
hostSpecialPrefix1 = match.captured("hostspecialprefix1");
hostSpecialNumber1 = match.captured("hostspecialareanumber1").toInt(&hostSpecialNumberExist1);

//qDebug() << Q_FUNC_INFO << " - 40";
//qDebug() << Q_FUNC_INFO << " - 40";
homeCallsign = match.captured("homecall");
homeFullPrefix = match.captured("homefullprefix");
//qDebug() << Q_FUNC_INFO << " homeFullPrefix: " << homeFullPrefix;
//qDebug() << Q_FUNC_INFO << " homeFullPrefix: " << homeFullPrefix;
homeNormalPrefix = match.captured("homenormalprefix");
homeSpecialPrefix = match.captured("homespecialprefix");

Expand Down Expand Up @@ -216,16 +239,16 @@ Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
//qDebug() << Q_FUNC_INFO << " - 399";
}

//qDebug() << Q_FUNC_INFO << " - @ hostFullPrefix : " << hostFullPrefix;
//qDebug() << Q_FUNC_INFO << " - @ hostPrefix : " << hostPrefix;
//qDebug() << Q_FUNC_INFO << " - @ hostAreaNumber : " << QString::number(hostAreaNumber);
//qDebug() << Q_FUNC_INFO << " - @ homeCallsign : " << homeCallsign;
//qDebug() << Q_FUNC_INFO << " - @ homeFullPrefix : " << homeFullPrefix;
//qDebug() << Q_FUNC_INFO << " - @ homePrefix : " << homePrefix;
//qDebug() << Q_FUNC_INFO << " - @ homeNormalPrefix : " << homeNormalPrefix;
//qDebug() << Q_FUNC_INFO << " - @ homeAreaNumber : " << QString::number(homeAreaNumber);
//qDebug() << Q_FUNC_INFO << " - @ homeSuffix : " << homeSuffix;
//qDebug() << Q_FUNC_INFO << " - @ suffix : " << generalSuffix;
//qDebug() << Q_FUNC_INFO << " - @ hostFullPrefix : " << hostFullPrefix;
//qDebug() << Q_FUNC_INFO << " - @ hostPrefix : " << hostPrefix;
//qDebug() << Q_FUNC_INFO << " - @ hostAreaNumber : " << QString::number(hostAreaNumber);
//qDebug() << Q_FUNC_INFO << " - @ homeCallsign : " << homeCallsign;
//qDebug() << Q_FUNC_INFO << " - @ homeFullPrefix : " << homeFullPrefix;
//qDebug() << Q_FUNC_INFO << " - @ homePrefix : " << homePrefix;
//qDebug() << Q_FUNC_INFO << " - @ homeNormalPrefix : " << homeNormalPrefix;
//qDebug() << Q_FUNC_INFO << " - @ homeAreaNumber : " << QString::number(homeAreaNumber);
//qDebug() << Q_FUNC_INFO << " - @ homeSuffix : " << homeSuffix;
//qDebug() << Q_FUNC_INFO << " - @ suffix : " << generalSuffix;

}
else if ( matchPrefix.hasMatch() )
Expand Down Expand Up @@ -265,8 +288,6 @@ Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
//qDebug() << Q_FUNC_INFO << " - 100";
}

Callsign::~Callsign(){}

QRegularExpression Callsign::callsignRegEx()
{
// Returns a REGEX that matches a hamradio callsign
Expand Down Expand Up @@ -318,6 +339,15 @@ bool Callsign::isValidPrefix()
return prefValid;
}

bool Callsign::isSimple()
{
if (fullCall.contains('/'))
return false;
if (fullCall.contains('\\'))
return false;
return true;
}

void Callsign::clear()
{
fullCall.clear();
Expand Down
9 changes: 7 additions & 2 deletions src/callsign.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class Callsign : public QObject
{
Q_OBJECT
public:
Callsign(const QString &callsign,
QObject *parent = nullptr);
Callsign(const QString &callsign, QObject *parent = nullptr);
~Callsign();
void operator()(const QString &newCallsign);

//static QStringList secondarySpecialSuffixes;

// K1/EA4K/QRP
Expand Down Expand Up @@ -74,14 +75,18 @@ class Callsign : public QObject

bool isValid(); // True if it is a full callsign
bool isValidPrefix(); // True if it is a prefix, but not a call
bool isSimple(); // True if it has no / nor \ characters, no prefix nor suffix
void clear();

private:

static QString callsignRegExString();
static QRegularExpression callsignRegEx();
static QString prefixRegExString();
static QRegularExpression prefixRegEx();

void initialize(const QString &callsign); // A helper to perform the initialization and prevent code duplication

QString fullCall; // K1/EA4K/QRP
QString hostFullPrefix; // K1
QString hostPrefix; // K
Expand Down
70 changes: 7 additions & 63 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* *
*****************************************************************************/
#include "utilities.h"
#include "callsign.h"
#include <QRegularExpression>
//bool c;
Utilities::Utilities(const QString &_parentName)
Expand Down Expand Up @@ -1028,79 +1029,22 @@ bool Utilities::isValidCall(const QString &_c, bool _force)

//qDebug() << Q_FUNC_INFO << " - Long prefixes: " << QString::number(longPrefixes.count());
if (longPrefixes.count()<100)
{
{ // Reporting a problem with the DB
//TODO: This should be managed differently, maybe issuing a signal and show a message stating a
// potential error with the DB
//qDebug() << Q_FUNC_INFO << "Long prefixes < 100 " << _c;
return false;
}
QString call = _c;
//qDebug() << Q_FUNC_INFO << "000 " << _c;
if (isAKnownCall(call))
if (isAKnownCall(_c))
{
//qDebug() << Q_FUNC_INFO << "001 - Known call: " << _c;
return true;
}
//qDebug() << Q_FUNC_INFO << "- 002 " << call;
if (call.length()<3)
{
//qDebug() << Q_FUNC_INFO << "- 003 " << call;
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("END - 010 - False"), Debug);
return false;
}
//call = getMainCallFromComplexCall(call);
//qDebug() << Q_FUNC_INFO << "- 004 :" << call;

//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("END - 010"), Devel);
if (call.count('\\')>0)
{
call.replace('\\', '/');
}
//qDebug() << Q_FUNC_INFO << " -005";

if (call.count('/')>2)
{
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("END - 015 - false"), Debug);
//qDebug() << Q_FUNC_INFO << " -005.5";
return false;
}
//qDebug() << Q_FUNC_INFO << " -006";
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("END - 020"), Devel);
//qDebug() << QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName) << " - 020";
if (call.count('/') == 2)
{ //Things like F/EA4K/P will become F/EA4K
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("Two /; Ignoring the last part: %1").arg(call), Devel);
QStringList parts;
parts.clear();
parts << call.split('/');
call = parts.at(0) + "/" + parts.at(1);
}
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString(" - 025: %1").arg(call), Devel);
//qDebug() << Q_FUNC_INFO << " -025";

if (call.count('/') == 1)
{ // Complex calls (like F/EA4K or EA4K/F OR /p OR /qrp
// We are just checking the call format not if it belongs to a country or whatever.
// It may return true for wrong calls like "ABC/EA4K"
// TODO: Add a check just for prefixes to fix the previous
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString(" - Call with one /: %1").arg(call), Devel);
QStringList parts;
parts.clear();
parts << call.split ('/');
//EA4K/P

bool result1 = ((isAPrefix (parts.at (0))) || (isValidSimpleCall (parts.at(0))));
//qDebug() << Q_FUNC_INFO << " -027";
bool result2 = ((isAPrefix (parts.at (1))) || (isValidSimpleCall (parts.at(1))) || isAValidOperatingSuffix(parts.at(1)) );
//qDebug() << Q_FUNC_INFO << parts.at(0) << "/" << parts.at(1);
//qDebug() << Q_FUNC_INFO << QString("Result1=%1").arg(boolToQString(result1));
//qDebug() << Q_FUNC_INFO << QString("Result2=%1").arg(boolToQString(result2));
//qDebug() << Q_FUNC_INFO << QString("Detailed=%1/%2/%3").arg(boolToQString((isAPrefix (parts.at (1))) )).arg(boolToQString((isValidSimpleCall (parts.at(1))))).arg(boolToQString(isAValidOperatingSuffix(parts.at(1))));
//qDebug() << Q_FUNC_INFO << "END1";
return (result1 && result2);
}
//logEvent (QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName), QString("END - %1").arg(isValidSimpleCall(call)), Debug);
//qDebug() << QString("%1-%2").arg(Q_FUNC_INFO).arg(parentName) << " - END";

return isValidSimpleCall(call);
Callsign callsign(_c);
return callsign.isValid();
}

int Utilities::getAreaNumberFromCall(const QString &_c)
Expand Down
23 changes: 16 additions & 7 deletions tests/tst_callsign/tst_callsign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private slots:
void test_Constructors();
void test_callsigns();
void test_callsigns_data(); // Data function
void test_callsign_operator();

private:
//Callsign *callsign;
Expand Down Expand Up @@ -98,7 +99,7 @@ void tst_Callsign::test_callsigns_data()

//KB1/EA4K/QRP
QTest::addColumn<QString>("testString"); // KB1/EA4K/QRP
QTest::addColumn<QString>("fullcallsign"); // KB1/EA4K/QRP
QTest::addColumn<QString>("fullcallsign"); // KB1/EA4K/QRP
QTest::addColumn<QString>("hostfullprefix"); // KB1
QTest::addColumn<QString>("hostprefix"); // KB
QTest::addColumn<int>("hostareanumber"); // 1
Expand Down Expand Up @@ -142,13 +143,10 @@ void tst_Callsign::test_callsigns_data()
QTest::newRow("A2") << "A2" << "" << "" << "" << -1 << "" << "A2" << "A2" << -1 << "" << "" << true << false;
QTest::newRow("3D2") << "3D2" << "" << "" << "" << -1 << "" << "3D2" << "3D2" << -1 << "" << "" << true << false;
QTest::newRow("3D20") << "3D20" << "" << "" << "" << -1 << "" << "3D20" << "3D2" << 0 << "" << "" << true << false;

// Now wrong callsigns
// FAIL: E/EA0K, , KKK1J
//QTest::newRow("E0J") << "E0J" << "E0J" << "" << "" << -1 << "E0J" << "E0" << "J" << 0 << "J" << "" << true << false;



// TO BE ADDED
// 3D20CR, 3D2C, 3D2NV/P, 3D3HY/R, 3V8ST/J, 4J75T/FF, UF/UA6GG/FF
// ,
Expand All @@ -162,10 +160,7 @@ void tst_Callsign::test_callsigns_data()
// FR/F6KDF/T
// M2001Y/71B, 2IONGM/NHS
// W0S, N6J

// mount Athos SV2A


}

void tst_Callsign::test_callsigns()
Expand Down Expand Up @@ -224,7 +219,21 @@ void tst_Callsign::test_callsigns()
}
}

void tst_Callsign::test_callsign_operator()
{

Callsign testCall("EA0K");
//qDebug() << Q_FUNC_INFO << " - getCallsign-1 : " << testCall.getCallsign();
QVERIFY2("EA0K" == testCall.getCallsign(), "Constructor is failing - EA0K");
testCall("EA0L");
//qDebug() << Q_FUNC_INFO << " - getCallsign-2 : " << testCall.getCallsign();
QVERIFY2("EA0L" == testCall.getCallsign(), "Operator is failing - EA0L");
testCall("KB1/EA0K/QRP");
QCOMPARE(testCall.getHomeSuffix(), "K");
QCOMPARE(testCall.getHomeCallsign(), "EA0K");
QCOMPARE(testCall.getSuffix(), "QRP");
QCOMPARE(testCall.getHostFullPrefix(), "KB1");
}

QTEST_APPLESS_MAIN(tst_Callsign)

Expand Down

0 comments on commit 8864964

Please sign in to comment.