2024-01-01 14:10:54 +01:00
|
|
|
#
|
|
|
|
/*
|
2025-07-17 20:49:55 +02:00
|
|
|
* Copyright (C) 2016 .. 2023
|
2024-01-01 14:10:54 +01:00
|
|
|
* Jan van Katwijk (J.vanKatwijk@gmail.com)
|
|
|
|
* Lazy Chair Computing
|
|
|
|
*
|
2024-03-20 15:05:10 +01:00
|
|
|
* This file is part of Qt-DAB
|
2024-01-01 14:10:54 +01:00
|
|
|
*
|
|
|
|
* Qt-DAB is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Qt-DAB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Qt-DAB; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <math.h>
|
|
|
|
#include "dab-constants.h"
|
2024-02-25 14:18:27 +01:00
|
|
|
#include "tii-reader.h"
|
2024-01-01 14:10:54 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
2025-07-17 20:49:55 +02:00
|
|
|
//#include "scan-handler.h"
|
2025-07-14 17:25:42 +02:00
|
|
|
|
2024-01-01 14:10:54 +01:00
|
|
|
#define SEPARATOR ';'
|
|
|
|
#define COUNTRY 1
|
|
|
|
#define CHANNEL 2
|
2024-11-29 20:31:44 +01:00
|
|
|
#define ENSEMBLE 3
|
2024-01-01 14:10:54 +01:00
|
|
|
#define EID 4
|
|
|
|
#define TII 5
|
|
|
|
#define LOCATION 6
|
|
|
|
#define LATITUDE 7
|
|
|
|
#define LONGITUDE 8
|
2024-11-29 20:31:44 +01:00
|
|
|
#define ALTITUDE 9
|
|
|
|
#define HEIGHT 10
|
|
|
|
#define POLARIZATION 11
|
|
|
|
#define FREQUENCY 12
|
2024-01-01 14:10:54 +01:00
|
|
|
#define POWER 13
|
2024-11-29 20:31:44 +01:00
|
|
|
#define DIRECTION 14
|
|
|
|
#define NR_COLUMNS 15
|
2024-01-01 14:10:54 +01:00
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
tiiReader::tiiReader () {
|
2024-01-01 14:10:54 +01:00
|
|
|
}
|
2024-02-25 14:18:27 +01:00
|
|
|
tiiReader::~tiiReader () {
|
2024-01-01 14:10:54 +01:00
|
|
|
}
|
|
|
|
//
|
2024-02-25 14:18:27 +01:00
|
|
|
std::vector<cacheElement> tiiReader::readFile (const QString &s) {
|
2024-01-01 14:10:54 +01:00
|
|
|
std::vector<cacheElement> res;
|
|
|
|
if (s == "") {
|
|
|
|
return res;
|
|
|
|
}
|
2024-03-20 15:05:10 +01:00
|
|
|
|
2024-01-01 14:10:54 +01:00
|
|
|
res. resize (0);
|
2024-06-16 11:23:09 +02:00
|
|
|
Rflag = s. endsWith ("/.txdata.tii");
|
2024-01-01 14:10:54 +01:00
|
|
|
FILE *f = fopen (s. toUtf8 (). data (), "r+b");
|
2024-04-19 18:34:50 +02:00
|
|
|
#ifdef __TRACE__
|
|
|
|
if (f == nullptr)
|
|
|
|
fprintf (stderr, "Cannot open %s\n", s. toUtf8 (). data ());
|
|
|
|
#endif
|
2024-01-01 14:10:54 +01:00
|
|
|
if (f == nullptr)
|
|
|
|
return res;
|
2024-06-16 11:23:09 +02:00
|
|
|
if (!Rflag)
|
|
|
|
fprintf (stderr, "YOU OPENED A NON STANDARD TII DATABASE\n");
|
2024-04-19 18:34:50 +02:00
|
|
|
#ifdef __TRACE__
|
|
|
|
fprintf (stderr, "File %s is opened\n", s. toUtf8 (). data ());
|
|
|
|
#endif
|
2024-09-23 10:19:31 +02:00
|
|
|
cacheElement ed;
|
|
|
|
res. push_back (ed); // the dummy one
|
|
|
|
int count = 1;
|
2024-01-01 14:10:54 +01:00
|
|
|
char buffer [1024];
|
|
|
|
std::vector<QString> columnVector;
|
2024-06-16 11:23:09 +02:00
|
|
|
int shift = 0;
|
|
|
|
if (Rflag)
|
|
|
|
shift = fgetc (f);
|
2024-01-01 14:10:54 +01:00
|
|
|
while (eread (buffer, 1024, f, shift) != nullptr) {
|
|
|
|
cacheElement ed;
|
|
|
|
if (feof (f))
|
|
|
|
break;
|
|
|
|
columnVector. resize (0);
|
|
|
|
int columns = readColumns (columnVector, buffer, NR_COLUMNS);
|
|
|
|
if (columns < NR_COLUMNS)
|
|
|
|
continue;
|
2024-09-23 10:19:31 +02:00
|
|
|
ed. valid = true;
|
2024-02-25 14:18:27 +01:00
|
|
|
ed. country = columnVector [COUNTRY]. trimmed ();
|
2024-01-01 14:10:54 +01:00
|
|
|
ed. Eid = get_Eid (columnVector [EID]);
|
|
|
|
ed. mainId = get_mainId (columnVector [TII]);
|
|
|
|
ed. subId = get_subId (columnVector [TII]);
|
|
|
|
ed. channel = columnVector [CHANNEL]. trimmed ();
|
2024-11-29 20:31:44 +01:00
|
|
|
ed. ensemble = columnVector [ENSEMBLE]. trimmed ();
|
2024-01-01 14:10:54 +01:00
|
|
|
ed. transmitterName = columnVector [LOCATION];
|
|
|
|
ed. latitude = convert (columnVector [LATITUDE]);
|
|
|
|
ed. longitude = convert (columnVector [LONGITUDE]);
|
|
|
|
ed. power = convert (columnVector [POWER]);
|
2024-11-29 20:31:44 +01:00
|
|
|
ed. altitude = convert (columnVector [ALTITUDE]);
|
2024-02-25 14:18:27 +01:00
|
|
|
ed. height = convert (columnVector [HEIGHT]);
|
2024-11-29 20:31:44 +01:00
|
|
|
ed. polarization = columnVector [POLARIZATION].trimmed();
|
|
|
|
ed. frequency = convert (columnVector[FREQUENCY]);
|
|
|
|
ed. direction = columnVector [DIRECTION]. trimmed();
|
|
|
|
|
|
|
|
if ((ed. mainId == 255) ||(ed. subId == 255))
|
|
|
|
ed. valid = false;
|
|
|
|
if ((ed. mainId == 0) || (ed. subId == 0))
|
|
|
|
ed. valid = false;
|
2024-12-02 14:47:07 +01:00
|
|
|
if (ed. ensemble == "")
|
|
|
|
ed. valid = false;
|
2024-01-01 14:10:54 +01:00
|
|
|
if (count >= (int) res. size ())
|
|
|
|
res. resize (res. size () + 100);
|
2025-07-14 17:25:42 +02:00
|
|
|
ed. key_1 = (ed. Eid << 16) | (ed. mainId << 8) + ed. subId;
|
|
|
|
bool ok;
|
|
|
|
uint16_t cc = ed. channel. toInt (&ok, 16);
|
|
|
|
ed. key_2 = ok ? cc : 0;
|
2024-01-01 14:10:54 +01:00
|
|
|
res. at (count) = ed;
|
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
fclose (f);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
int tiiReader::readColumns (std::vector<QString> &v, char *b, int N) {
|
2024-01-01 14:10:54 +01:00
|
|
|
int charp = 0;
|
|
|
|
char tb [256];
|
|
|
|
int elementCount = 0;
|
|
|
|
QString element;
|
|
|
|
|
|
|
|
v. resize (0);
|
|
|
|
while ((*b != 0) && (*b != '\n')) {
|
|
|
|
if (*b == SEPARATOR) {
|
|
|
|
tb [charp] = 0;
|
2024-02-25 14:18:27 +01:00
|
|
|
QString ss = QString::fromUtf8 (tb);
|
2024-01-01 14:10:54 +01:00
|
|
|
v. push_back (ss);
|
|
|
|
charp = 0;
|
|
|
|
elementCount ++;
|
|
|
|
if (elementCount >= N)
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tb [charp ++] = *b;
|
|
|
|
b ++;
|
|
|
|
}
|
|
|
|
return elementCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2024-02-25 14:18:27 +01:00
|
|
|
char *tiiReader::eread (char *buffer, int amount, FILE *f, uint8_t shift) {
|
2024-01-01 14:10:54 +01:00
|
|
|
char *bufferP;
|
|
|
|
|
|
|
|
if (fgets (buffer, amount, f) == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
bufferP = buffer;
|
2024-06-16 11:23:09 +02:00
|
|
|
if (Rflag) {
|
|
|
|
while (*bufferP != 0) {
|
|
|
|
if (shift != 0xAA)
|
|
|
|
*bufferP -= shift;
|
|
|
|
else
|
|
|
|
*bufferP ^= 0xAA;
|
|
|
|
bufferP ++;
|
|
|
|
}
|
|
|
|
*bufferP = 0;
|
2024-01-01 14:10:54 +01:00
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
float tiiReader::convert (const QString &s) {
|
2024-01-01 14:10:54 +01:00
|
|
|
bool flag;
|
|
|
|
float v;
|
|
|
|
v = s. trimmed (). toFloat (&flag);
|
|
|
|
if (!flag)
|
|
|
|
v = 0;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
uint16_t tiiReader::get_Eid (const QString &s) {
|
2024-01-01 14:10:54 +01:00
|
|
|
bool flag;
|
|
|
|
uint16_t res;
|
|
|
|
res = s. trimmed (). toInt (&flag, 16);
|
|
|
|
if (!flag)
|
|
|
|
res = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
uint8_t tiiReader::get_mainId (const QString &s) {
|
2024-01-01 14:10:54 +01:00
|
|
|
bool flag;
|
|
|
|
uint16_t res;
|
|
|
|
res = s. trimmed (). toInt (&flag);
|
|
|
|
if (!flag)
|
|
|
|
res = 0;
|
|
|
|
return res / 100;
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:18:27 +01:00
|
|
|
uint8_t tiiReader::get_subId (const QString &s) {
|
2024-01-01 14:10:54 +01:00
|
|
|
bool flag;
|
|
|
|
uint16_t res;
|
|
|
|
res = s. trimmed (). toInt (&flag);
|
|
|
|
if (!flag)
|
|
|
|
res = 0;
|
|
|
|
return res % 100;
|
|
|
|
}
|
|
|
|
|