1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

ISO8211 API cleanup

This commit is contained in:
Martin Tůma 2023-01-19 10:08:17 +01:00
parent ee73908231
commit ab76521990
3 changed files with 36 additions and 42 deletions

View File

@ -188,12 +188,16 @@ bool ISO8211::readDDA(QFile &file, const FieldDefinition &def, SubFields &fields
return true; return true;
} }
bool ISO8211::readDDR(QFile &file) bool ISO8211::readDDR()
{ {
QVector<FieldDefinition> fields; QVector<FieldDefinition> fields;
qint64 pos = file.pos();
int len = readDR(file, fields);
if (!_file.open(QIODevice::ReadOnly)) {
_errorString = _file.errorString();
return false;
}
int len = readDR(_file, fields);
if (len < 0) { if (len < 0) {
_errorString = "Not a ISO8211 file"; _errorString = "Not a ISO8211 file";
return false; return false;
@ -201,7 +205,7 @@ bool ISO8211::readDDR(QFile &file)
for (int i = 0; i < fields.size(); i++) { for (int i = 0; i < fields.size(); i++) {
SubFields def; SubFields def;
if (!readDDA(file, fields.at(i), def)) { if (!readDDA(_file, fields.at(i), def)) {
_errorString = QString("Error reading %1 DDA field") _errorString = QString("Error reading %1 DDA field")
.arg(QString(fields.at(i).tag)); .arg(QString(fields.at(i).tag));
return false; return false;
@ -209,7 +213,7 @@ bool ISO8211::readDDR(QFile &file)
_map.insert(fields.at(i).tag, def); _map.insert(fields.at(i).tag, def);
} }
if (file.pos() != pos + len || fields.size() < 2) { if (_file.pos() != len || fields.size() < 2) {
_errorString = "DDR format error"; _errorString = "DDR format error";
return false; return false;
} }
@ -231,6 +235,7 @@ bool ISO8211::readUDA(QFile &file, quint64 pos, const FieldDefinition &def,
const char *dp = ba.constData(); const char *dp = ba.constData();
const char *ep = ba.constData() + ba.size() - 1; const char *ep = ba.constData() + ba.size() - 1;
data.clear();
data.setFields(&fields); data.setFields(&fields);
do { do {
@ -287,11 +292,14 @@ bool ISO8211::readUDA(QFile &file, quint64 pos, const FieldDefinition &def,
return true; return true;
} }
bool ISO8211::readRecord(QFile &file, Record &record) bool ISO8211::readRecord(Record &record)
{ {
if (_file.atEnd())
return false;
QVector<FieldDefinition> fields; QVector<FieldDefinition> fields;
qint64 pos = file.pos(); qint64 pos = _file.pos();
int len = readDR(file, fields); int len = readDR(_file, fields);
if (len < 0) { if (len < 0) {
_errorString = "Error reading DR"; _errorString = "Error reading DR";
@ -313,7 +321,7 @@ bool ISO8211::readRecord(QFile &file, Record &record)
f.setTag(def.tag); f.setTag(def.tag);
if (!readUDA(file, pos, def, it.value(), f.rdata())) { if (!readUDA(_file, pos, def, it.value(), f.rdata())) {
_errorString = QString("Error reading %1 record") _errorString = QString("Error reading %1 record")
.arg(QString(def.tag)); .arg(QString(def.tag));
return false; return false;

View File

@ -1,12 +1,11 @@
#ifndef ENC_ISO8211_H #ifndef ENC_ISO8211_H
#define ENC_ISO8211_H #define ENC_ISO8211_H
#include <QFile>
#include <QByteArray> #include <QByteArray>
#include <QVariant> #include <QVariant>
#include <QDebug> #include <QDebug>
class QFile;
#define UINT32(x) \ #define UINT32(x) \
(((quint32)*(const uchar*)(x)) \ (((quint32)*(const uchar*)(x)) \
| ((quint32)(*((const uchar*)(x) + 1)) << 8) \ | ((quint32)(*((const uchar*)(x) + 1)) << 8) \
@ -102,8 +101,9 @@ public:
} }
}; };
bool readDDR(QFile &file); ISO8211(const QString &path) : _file(path) {}
bool readRecord(QFile &file, Record &record); bool readDDR();
bool readRecord(Record &record);
const QString &errorString() const {return _errorString;} const QString &errorString() const {return _errorString;}
@ -118,6 +118,7 @@ private:
bool readUDA(QFile &file, quint64 pos, const FieldDefinition &def, bool readUDA(QFile &file, quint64 pos, const FieldDefinition &def,
const SubFields &fields, Data &data) const; const SubFields &fields, Data &data) const;
QFile _file;
FieldsMap _map; FieldsMap _map;
QString _errorString; QString _errorString;
}; };

View File

@ -1,4 +1,3 @@
#include <QFile>
#include "common/util.h" #include "common/util.h"
#include "objects.h" #include "objects.h"
#include "attributes.h" #include "attributes.h"
@ -751,31 +750,25 @@ bool MapData::bounds(const QVector<ISO8211::Record> &gv, Rect &b)
MapData::MapData(const QString &path): _fileName(path) MapData::MapData(const QString &path): _fileName(path)
{ {
QFile file(_fileName);
QVector<ISO8211::Record> gv; QVector<ISO8211::Record> gv;
ISO8211 ddf; ISO8211 ddf(_fileName);
ISO8211::Record record;
uint COMF = 1; uint COMF = 1;
if (!file.open(QIODevice::ReadOnly)) { if (!ddf.readDDR()) {
_errorString = file.errorString();
return;
}
if (!ddf.readDDR(file)) {
_errorString = ddf.errorString();
return;
}
while (!file.atEnd()) {
ISO8211::Record record;
if (!ddf.readRecord(file, record)) {
_errorString = ddf.errorString(); _errorString = ddf.errorString();
return; return;
} }
while (ddf.readRecord(record)) {
if (!processRecord(record, gv, COMF, _name)) { if (!processRecord(record, gv, COMF, _name)) {
_errorString = "Invalid S-57 record"; _errorString = "Invalid S-57 record";
return; return;
} }
} }
if (!ddf.errorString().isNull()) {
_errorString = ddf.errorString();
return;
}
Rect b; Rect b;
if (!bounds(gv, b)) { if (!bounds(gv, b)) {
@ -807,30 +800,22 @@ MapData::~MapData()
void MapData::load() void MapData::load()
{ {
QFile file(_fileName);
RecordMap vi, vc, ve, vf; RecordMap vi, vc, ve, vf;
QVector<ISO8211::Record> fe; QVector<ISO8211::Record> fe;
uint COMF = 1, SOMF = 1; ISO8211 ddf(_fileName);
ISO8211 ddf; ISO8211::Record record;
uint PRIM, OBJL; uint PRIM, OBJL, COMF = 1, SOMF = 1;
Poly *poly; Poly *poly;
Line *line; Line *line;
Point *point; Point *point;
double min[2], max[2]; double min[2], max[2];
if (!file.open(QIODevice::ReadOnly)) if (!ddf.readDDR())
return;
if (!ddf.readDDR(file))
return;
while (!file.atEnd()) {
ISO8211::Record record;
if (!ddf.readRecord(file, record))
return; return;
while (ddf.readRecord(record))
if (!processRecord(record, fe, vi, vc, ve, vf, COMF, SOMF)) if (!processRecord(record, fe, vi, vc, ve, vf, COMF, SOMF))
return; qWarning("Invalid S-57 record");
}
for (int i = 0; i < fe.size(); i++) { for (int i = 0; i < fe.size(); i++) {
const ISO8211::Record &r = fe.at(i); const ISO8211::Record &r = fe.at(i);