| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | #include "decodedtext.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 19:41:03 +00:00
										 |  |  | #include <QStringList>
 | 
					
						
							|  |  |  | #include <QRegularExpression>
 | 
					
						
							| 
									
										
										
										
											2017-09-01 21:29:02 +00:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2020-06-13 16:04:41 +01:00
										 |  |  | #include "qt_helpers.hpp"
 | 
					
						
							| 
									
										
										
										
											2016-11-05 19:41:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 19:27:23 +00:00
										 |  |  | extern "C" { | 
					
						
							| 
									
										
										
										
											2018-07-11 10:13:42 -04:00
										 |  |  |   bool stdmsg_(char const * msg, fortran_charlen_t); | 
					
						
							| 
									
										
										
										
											2017-07-24 19:27:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  | namespace | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-06 20:23:41 +01:00
										 |  |  |   QRegularExpression words_re {R"(^(?:(?<word1>(?:CQ|DE|QRZ)(?:\s?DX|\s(?:[A-Z]{1,4}|\d{3}))|[A-Z0-9/]+|\.{3})\s)(?:(?<word2>[A-Z0-9/]+)(?:\s(?<word3>[-+A-Z0-9]+)(?:\s(?<word4>(?:OOO|(?!RR73)[A-R]{2}[0-9]{2})))?)?)?)"}; | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 13:25:01 -04:00
										 |  |  | DecodedText::DecodedText (QString const& the_string) | 
					
						
							| 
									
										
										
										
											2017-09-22 22:09:04 +00:00
										 |  |  |   : string_ {the_string.left (the_string.indexOf (QChar::Nbsp))} // discard appended info
 | 
					
						
							| 
									
										
										
										
											2021-03-03 11:51:22 +00:00
										 |  |  |   , clean_string_ {string_} | 
					
						
							| 
									
										
										
										
											2017-09-22 22:09:04 +00:00
										 |  |  |   , padding_ {string_.indexOf (" ") > 4 ? 2 : 0} // allow for
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |                                                     // seconds
 | 
					
						
							|  |  |  |   , message_ {string_.mid (column_qsoText + padding_).trimmed ()} | 
					
						
							|  |  |  |   , is_standard_ {false} | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-03 11:51:22 +00:00
										 |  |  |   // discard appended AP info
 | 
					
						
							| 
									
										
										
										
											2021-03-12 23:28:33 +00:00
										 |  |  |   clean_string_.replace (QRegularExpression {R"(^(.*?)(?:\?\s)?a[0-9].*$)"}, "\\1"); | 
					
						
							| 
									
										
										
										
											2021-03-03 11:51:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:12:44 -04:00
										 |  |  | //  qDebug () << "DecodedText: the_string:" << the_string << "Nbsp pos:" << the_string.indexOf (QChar::Nbsp);
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   if (message_.length() >= 1) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-29 09:21:08 -04:00
										 |  |  |        message0_ = message_.left(36); | 
					
						
							|  |  |  |        message_ = message_.left(36).remove (QRegularExpression {"[<>]"}); | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |       int i1 = message_.indexOf ('\r'); | 
					
						
							|  |  |  |       if (i1 > 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           message_ = message_.left (i1 - 1); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  |       if (message_.contains (QRegularExpression {"^(CQ|QRZ)\\s"})) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // TODO this magic position 16 is guaranteed to be after the
 | 
					
						
							|  |  |  |           // last space in a decoded CQ or QRZ message but before any
 | 
					
						
							|  |  |  |           // appended DXCC entity name or worked before information
 | 
					
						
							|  |  |  |           auto eom_pos = message_.indexOf (' ', 16); | 
					
						
							|  |  |  |           // we always want at least the characters to position 16
 | 
					
						
							|  |  |  |           if (eom_pos < 16) eom_pos = message_.size () - 1; | 
					
						
							|  |  |  |           // remove DXCC entity and worked B4 status. TODO need a better way to do this
 | 
					
						
							|  |  |  |           message_ = message_.left (eom_pos + 1); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-11 10:13:42 -04:00
										 |  |  |       // stdmsg is a Fortran routine that packs the text, unpacks it
 | 
					
						
							| 
									
										
										
										
											2017-09-01 20:10:35 +00:00
										 |  |  |       // and compares the result
 | 
					
						
							| 
									
										
										
										
											2018-08-29 09:21:08 -04:00
										 |  |  |       auto message_c_string = message0_.toLocal8Bit (); | 
					
						
							| 
									
										
										
										
											2018-07-11 10:13:42 -04:00
										 |  |  |       message_c_string += QByteArray {37 - message_c_string.size (), ' '}; | 
					
						
							|  |  |  |       is_standard_ = stdmsg_(message_c_string.constData(),37); | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  | QStringList DecodedText::messageWords () const | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-12 11:09:49 -04:00
										 |  |  |   if(is_standard_) { | 
					
						
							|  |  |  |     // extract up to the first four message words
 | 
					
						
							|  |  |  |     QString t=message_; | 
					
						
							|  |  |  |     if(t.left(4)=="TU; ") t=message_.mid(4,-1); | 
					
						
							|  |  |  |     return words_re.match(t).capturedTexts(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-09-01 21:29:02 +00:00
										 |  |  |   // simple word split for free text messages
 | 
					
						
							| 
									
										
										
										
											2020-06-13 16:04:41 +01:00
										 |  |  |   auto words = message_.split (' ', SkipEmptyParts); | 
					
						
							| 
									
										
										
										
											2017-09-01 21:29:02 +00:00
										 |  |  |   // add whole message as item 0 to mimic RE capture list
 | 
					
						
							|  |  |  |   words.prepend (message_); | 
					
						
							|  |  |  |   return words; | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | QString DecodedText::CQersCall() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-28 13:24:03 -05:00
										 |  |  |   QRegularExpression callsign_re {R"(^(CQ|DE|QRZ)(\s?DX|\s([A-Z]{1,4}|\d{3}))?\s(?<callsign>[A-Z0-9/]{2,})(\s[A-R]{2}[0-9]{2})?)"}; | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   return callsign_re.match (message_).captured ("callsign"); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool DecodedText::isJT65() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |     return string_.indexOf("#") == column_mode + padding_; | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | bool DecodedText::isJT9() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |     return string_.indexOf("@") == column_mode + padding_; | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | bool DecodedText::isTX() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |     int i = string_.indexOf("Tx"); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |     return (i >= 0 && i < 15); // TODO guessing those numbers. Does Tx ever move?
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | bool DecodedText::isLowConfidence () const | 
					
						
							| 
									
										
										
										
											2017-07-26 21:18:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   return QChar {'?'} == string_.mid (padding_ + column_qsoText + 21, 1); | 
					
						
							| 
									
										
										
										
											2017-07-26 21:18:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | int DecodedText::frequencyOffset() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |     return string_.mid(column_freq + padding_,4).toInt(); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | int DecodedText::snr() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   int i1=string_.indexOf(" ")+1; | 
					
						
							|  |  |  |   return string_.mid(i1,3).toInt(); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | float DecodedText::dt() const | 
					
						
							| 
									
										
										
										
											2015-04-22 17:48:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   return string_.mid(column_dt + padding_,5).toFloat(); | 
					
						
							| 
									
										
										
										
											2015-04-22 17:48:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  | 2343 -11  0.8 1259 # YV6BFE F6GUU R-08 | 
					
						
							|  |  |  | 2343 -19  0.3  718 # VE6WQ SQ2NIJ -14 | 
					
						
							|  |  |  | 2343  -7  0.3  815 # KK4DSD W7VP -16 | 
					
						
							|  |  |  | 2343 -13  0.1 3627 @ CT1FBK IK5YZT R+02 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 0605  Tx      1259 # CQ VK3ACF QF22 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // find and extract any report. Returns true if this is a standard message
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | bool DecodedText::report(QString const& myBaseCall, QString const& dxBaseCall, /*mod*/QString& report) const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   if (message_.size () < 1) return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-14 12:35:48 +01:00
										 |  |  |   QStringList const& w = message_.split(" ", SkipEmptyParts); | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   if (w.size () | 
					
						
							|  |  |  |       && is_standard_ && (w[0] == myBaseCall | 
					
						
							|  |  |  |                           || w[0].endsWith ("/" + myBaseCall) | 
					
						
							|  |  |  |                           || w[0].startsWith (myBaseCall + "/") | 
					
						
							|  |  |  |                           || (w.size () > 1 && !dxBaseCall.isEmpty () | 
					
						
							|  |  |  |                               && (w[1] == dxBaseCall | 
					
						
							|  |  |  |                                   || w[1].endsWith ("/" + dxBaseCall) | 
					
						
							|  |  |  |                                   || w[1].startsWith (dxBaseCall + "/"))))) | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |       QString tt=""; | 
					
						
							|  |  |  |       if(w.size() > 2) tt=w[2]; | 
					
						
							|  |  |  |       bool ok; | 
					
						
							|  |  |  |       auto i1=tt.toInt(&ok); | 
					
						
							|  |  |  |       if (ok and i1>=-50 and i1<50) | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |           report = tt; | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |           if (tt.mid(0,1)=="R") | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |               i1=tt.mid(1).toInt(&ok); | 
					
						
							|  |  |  |               if(ok and i1>=-50 and i1<50) | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |                   report = tt.mid(1); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  |   return is_standard_; | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get the first text word, usually the call
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | QString DecodedText::call() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  |   return words_re.match (message_).captured ("word1"); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get the second word, most likely the de call and the third word, most likely grid
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | void DecodedText::deCallAndGrid(/*out*/QString& call, QString& grid) const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-18 14:47:20 +01:00
										 |  |  |   auto msg = message_; | 
					
						
							|  |  |  |   auto p = msg.indexOf ("; "); | 
					
						
							|  |  |  |   if (p >= 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       msg = msg.mid (p + 2); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   auto const& match = words_re.match (msg); | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  |   call = match.captured ("word2"); | 
					
						
							|  |  |  |   grid = match.captured ("word3"); | 
					
						
							| 
									
										
										
										
											2018-07-11 13:25:01 -04:00
										 |  |  |   if ("R" == grid) grid = match.captured ("word4"); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  | unsigned DecodedText::timeInSeconds() const | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-01 05:11:57 +00:00
										 |  |  |   return 3600 * string_.mid (column_time, 2).toUInt () | 
					
						
							|  |  |  |     + 60 * string_.mid (column_time + 2, 2).toUInt() | 
					
						
							|  |  |  |     + (padding_ ? string_.mid (column_time + 2 + padding_, 2).toUInt () : 0U); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-30 02:27:57 +00:00
										 |  |  | QString DecodedText::report() const // returns a string of the SNR field with a leading + or - followed by two digits
 | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     int sr = snr(); | 
					
						
							|  |  |  |     if (sr<-50) | 
					
						
							|  |  |  |         sr = -50; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         if (sr > 49) | 
					
						
							|  |  |  |             sr = 49; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString rpt; | 
					
						
							| 
									
										
										
										
											2020-05-07 02:56:57 +01:00
										 |  |  |     rpt = rpt.asprintf("%d",abs(sr)); | 
					
						
							| 
									
										
										
										
											2013-08-25 01:48:45 +00:00
										 |  |  |     if (sr > 9) | 
					
						
							|  |  |  |         rpt = "+" + rpt; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         if (sr >= 0) | 
					
						
							|  |  |  |             rpt = "+0" + rpt; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             if (sr >= -9) | 
					
						
							|  |  |  |                 rpt = "-0" + rpt; | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 rpt = "-" + rpt; | 
					
						
							|  |  |  |     return rpt; | 
					
						
							|  |  |  | } |