1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 11:52:08 +01:00

Improved error handling

This commit is contained in:
Martin Tůma 2025-01-03 19:17:30 +01:00
parent 7ee681313f
commit 7a161fa364
17 changed files with 76 additions and 62 deletions

View File

@ -11,7 +11,7 @@ class DEMFile : public SubFile
{ {
public: public:
DEMFile(const IMGData *img) : SubFile(img) {} DEMFile(const IMGData *img) : SubFile(img) {}
DEMFile(const QString *path) : SubFile(path) {} DEMFile(const QString &path) : SubFile(path) {}
DEMFile(const SubFile *gmp, quint32 offset) : SubFile(gmp, offset) {} DEMFile(const SubFile *gmp, quint32 offset) : SubFile(gmp, offset) {}
bool load(Handle &hdl); bool load(Handle &hdl);

View File

@ -87,12 +87,16 @@ bool GMAPData::loadTile(const QDir &dir)
const QFileInfo &fi = ml.at(i); const QFileInfo &fi = ml.at(i);
SubFile::Type tt = tileType(fi.suffix()); SubFile::Type tt = tileType(fi.suffix());
if (VectorTile::isTileFile(tt)) { if (VectorTile::isTileFile(tt)) {
_files.append(new QString(fi.absoluteFilePath())); if (!tile->addFile(fi.absoluteFilePath(), tt)) {
tile->addFile(_files.last(), tt); qWarning("%s: Invalid map tile structure",
qPrintable(dir.path()));
delete tile;
return false;
}
} }
} }
if (!tile->init(0)) { if (!tile->init()) {
qWarning("%s: Invalid map tile", qPrintable(dir.path())); qWarning("%s: Invalid map tile", qPrintable(dir.path()));
delete tile; delete tile;
return false; return false;
@ -131,10 +135,8 @@ GMAPData::GMAPData(const QString &fileName) : MapData(fileName)
loadTile(QDir(fi.absoluteFilePath())); loadTile(QDir(fi.absoluteFilePath()));
} }
if (baseDir.exists(typFilePath)) { if (baseDir.exists(typFilePath))
_files.append(new QString(baseDir.filePath(typFilePath))); _typ = new SubFile(baseDir.filePath(typFilePath));
_typ = new SubFile(_files.last());
}
if (!_tileTree.Count()) if (!_tileTree.Count())
_errorString = "No usable map tile found"; _errorString = "No usable map tile found";
@ -143,8 +145,3 @@ GMAPData::GMAPData(const QString &fileName) : MapData(fileName)
computeZooms(); computeZooms();
} }
GMAPData::~GMAPData()
{
qDeleteAll(_files);
}

View File

@ -12,15 +12,13 @@ class GMAPData : public MapData
{ {
public: public:
GMAPData(const QString &fileName); GMAPData(const QString &fileName);
~GMAPData();
private: private:
bool readXML(const QString &path, QString &dataDir, QString &typFile); bool readXML(const QString &path, QString &dataDir, QString &typFile);
void mapProduct(QXmlStreamReader &reader, QString &dataDir, QString &typFile); void mapProduct(QXmlStreamReader &reader, QString &dataDir,
QString &typFile);
void subProduct(QXmlStreamReader &reader, QString &dataDir); void subProduct(QXmlStreamReader &reader, QString &dataDir);
bool loadTile(const QDir &dir); bool loadTile(const QDir &dir);
QList<const QString*> _files;
}; };
} }

View File

@ -117,8 +117,7 @@ bool IMGData::readFAT(QFile *file, TileMap &tileMap)
} else } else
tile = *it; tile = *it;
SubFile *subFile = part ? tile->file(tt) SubFile *subFile = part ? tile->file(tt) : tile->addFile(this, tt);
: tile->addFile(this, tt);
if (!(subFile && readSubFileBlocks(file, offset, subFile))) if (!(subFile && readSubFileBlocks(file, offset, subFile)))
return false; return false;
} else if (tt == SubFile::TYP) { } else if (tt == SubFile::TYP) {

View File

@ -18,7 +18,7 @@ public:
LBLFile(const IMGData *img) LBLFile(const IMGData *img)
: SubFile(img), _huffmanText(0), _imgIdSize(0), _poiShift(0), _shift(0), : SubFile(img), _huffmanText(0), _imgIdSize(0), _poiShift(0), _shift(0),
_encoding(0) {} _encoding(0) {}
LBLFile(const QString *path) LBLFile(const QString &path)
: SubFile(path), _huffmanText(0), _imgIdSize(0), _poiShift(0), _shift(0), : SubFile(path), _huffmanText(0), _imgIdSize(0), _poiShift(0), _shift(0),
_encoding(0) {} _encoding(0) {}
LBLFile(const SubFile *gmp, quint32 offset) LBLFile(const SubFile *gmp, quint32 offset)

View File

@ -99,14 +99,14 @@ void MapData::load(qreal ratio)
Q_ASSERT(!_style); Q_ASSERT(!_style);
if (_typ) if (_typ)
_style = new Style(0, ratio, _typ); _style = new Style(ratio, _typ);
else { else {
QString typFile(ProgramPaths::typFile()); QString typFile(ProgramPaths::typFile());
if (QFileInfo::exists(typFile)) { if (QFileInfo::exists(typFile)) {
SubFile typ(&typFile); SubFile typ(typFile);
_style = new Style(0, ratio, &typ); _style = new Style(ratio, &typ);
} else } else
_style = new Style(0, ratio); _style = new Style(ratio);
} }
} }

View File

@ -18,7 +18,7 @@ class NETFile : public SubFile
public: public:
NETFile(const IMGData *img) NETFile(const IMGData *img)
: SubFile(img), _huffmanTable(0), _tp(0), _netShift(0), _linksShift(0) {} : SubFile(img), _huffmanTable(0), _tp(0), _netShift(0), _linksShift(0) {}
NETFile(const QString *path) NETFile(const QString &path)
: SubFile(path), _huffmanTable(0), _tp(0), _netShift(0), _linksShift(0) {} : SubFile(path), _huffmanTable(0), _tp(0), _netShift(0), _linksShift(0) {}
NETFile(const SubFile *gmp, quint32 offset) NETFile(const SubFile *gmp, quint32 offset)
: SubFile(gmp, offset), _huffmanTable(0), _tp(0), _netShift(0), : SubFile(gmp, offset), _huffmanTable(0), _tp(0), _netShift(0),

View File

@ -62,7 +62,7 @@ public:
NODFile(const IMGData *img) NODFile(const IMGData *img)
: SubFile(img), _indexFlags(0), _indexRecordSize(0), _blockRecordSize(0), : SubFile(img), _indexFlags(0), _indexRecordSize(0), _blockRecordSize(0),
_blockShift(0), _nodeShift(0), _indexIdSize(0) {} _blockShift(0), _nodeShift(0), _indexIdSize(0) {}
NODFile(const QString *path) NODFile(const QString &path)
: SubFile(path), _indexFlags(0), _indexRecordSize(0), _blockRecordSize(0), : SubFile(path), _indexFlags(0), _indexRecordSize(0), _blockRecordSize(0),
_blockShift(0), _nodeShift(0), _indexIdSize(0) {} _blockShift(0), _nodeShift(0), _indexIdSize(0) {}
NODFile(const SubFile *gmp, quint32 offset) NODFile(const SubFile *gmp, quint32 offset)

View File

@ -152,7 +152,7 @@ void RasterTile::drawPolygons(QPainter *painter,
bool insert = false; bool insert = false;
SubFile::Handle *hdl = hc.object(poly.raster.lbl()); SubFile::Handle *hdl = hc.object(poly.raster.lbl());
if (!hdl) { if (!hdl) {
hdl = new SubFile::Handle(_file, poly.raster.lbl()); hdl = new SubFile::Handle(poly.raster.lbl(), _file);
insert = true; insert = true;
} }
QPixmap pm(poly.raster.lbl()->image(*hdl, poly.raster.id())); QPixmap pm(poly.raster.lbl()->image(*hdl, poly.raster.id()));

View File

@ -24,7 +24,7 @@ public:
}; };
RGNFile(const IMGData *img) : SubFile(img), _huffmanTable(0) {} RGNFile(const IMGData *img) : SubFile(img), _huffmanTable(0) {}
RGNFile(const QString *path) : SubFile(path), _huffmanTable(0) {} RGNFile(const QString &path) : SubFile(path), _huffmanTable(0) {}
RGNFile(const SubFile *gmp, quint32 offset) RGNFile(const SubFile *gmp, quint32 offset)
: SubFile(gmp, offset), _huffmanTable(0) {} : SubFile(gmp, offset), _huffmanTable(0) {}
~RGNFile(); ~RGNFile();

View File

@ -1268,9 +1268,9 @@ bool Style::parseDrawOrder(SubFile *file, SubFile::Handle &hdl,
return true; return true;
} }
bool Style::parseTYPFile(QFile *file, SubFile *typ) bool Style::parseTYPFile(SubFile *typ)
{ {
SubFile::Handle hdl(file, typ); SubFile::Handle hdl(typ);
Section points, lines, polygons, order; Section points, lines, polygons, order;
quint16 tmp16, codepage; quint16 tmp16, codepage;
@ -1311,7 +1311,7 @@ bool Style::parseTYPFile(QFile *file, SubFile *typ)
return true; return true;
} }
Style::Style(QFile *file, qreal ratio, SubFile *typ) Style::Style(qreal ratio, SubFile *typ)
{ {
_large = pixelSizeFont(16); _large = pixelSizeFont(16);
_normal = pixelSizeFont(14); _normal = pixelSizeFont(14);
@ -1326,7 +1326,7 @@ Style::Style(QFile *file, qreal ratio, SubFile *typ)
defaultPointStyle(ratio); defaultPointStyle(ratio);
if (typ) if (typ)
parseTYPFile(file, typ); parseTYPFile(typ);
} }
const Style::Line &Style::line(quint32 type) const const Style::Line &Style::line(quint32 type) const

View File

@ -104,7 +104,7 @@ public:
}; };
Style(QFile *file, qreal ratio, SubFile *typ = 0); Style(qreal ratio, SubFile *typ = 0);
const Line &line(quint32 type) const; const Line &line(quint32 type) const;
const Polygon &polygon(quint32 type) const; const Polygon &polygon(quint32 type) const;
@ -171,7 +171,7 @@ private:
bool extended; bool extended;
}; };
bool parseTYPFile(QFile *file, SubFile *typ); bool parseTYPFile(SubFile *typ);
bool parsePoints(SubFile *file, SubFile::Handle &hdl, bool parsePoints(SubFile *file, SubFile::Handle &hdl,
const Section &section); const Section &section);
bool parsePoint(SubFile *file, SubFile::Handle &hdl, bool parsePoint(SubFile *file, SubFile::Handle &hdl,

View File

@ -18,7 +18,7 @@ public:
class Handle class Handle
{ {
public: public:
Handle(QFile *file, const SubFile *subFile) Handle(const SubFile *subFile, QFile *file = 0)
: _file(file), _blockNum(-1), _blockPos(-1), _pos(-1), _delete(false) : _file(file), _blockNum(-1), _blockPos(-1), _pos(-1), _delete(false)
{ {
if (!subFile) if (!subFile)
@ -53,13 +53,18 @@ public:
SubFile(const IMGData *img) SubFile(const IMGData *img)
: _gmpOffset(0), _img(img), _blocks(new QVector<quint16>()), _path(0) {} : _gmpOffset(0), _img(img), _blocks(new QVector<quint16>()), _path(0) {}
SubFile(const SubFile *gmp, quint32 offset) : _gmpOffset(offset), SubFile(const SubFile *gmp, quint32 offset) : _gmpOffset(offset),
_img(gmp->_img), _blocks(gmp->_blocks), _path(gmp->_path) {} _img(gmp->_img), _blocks(gmp->_blocks), _path(gmp->_path)
SubFile(const QString *path) {
: _gmpOffset(0), _img(0), _blocks(0), _path(path) {} Q_ASSERT(offset);
}
SubFile(const QString &path)
: _gmpOffset(0), _img(0), _blocks(0), _path(new QString(path)) {}
~SubFile() ~SubFile()
{ {
if (!_gmpOffset) if (!_gmpOffset) {
delete _blocks; delete _blocks;
delete _path;
}
} }
void addBlock(quint16 block) {_blocks->append(block);} void addBlock(quint16 block) {_blocks->append(block);}

View File

@ -51,7 +51,7 @@ TREFile::~TREFile()
bool TREFile::init(QFile *file) bool TREFile::init(QFile *file)
{ {
Handle hdl(file, this); Handle hdl(this, file);
quint8 locked, levels[64]; quint8 locked, levels[64];
quint16 hdrLen; quint16 hdrLen;
qint32 north, east, south, west; qint32 north, east, south, west;
@ -156,7 +156,7 @@ int TREFile::readExtEntry(Handle &hdl, quint32 &polygons, quint32 &lines,
bool TREFile::load(QFile *file, int idx) bool TREFile::load(QFile *file, int idx)
{ {
Handle hdl(file, this); Handle hdl(this, file);
QList<SubDiv*> sl; QList<SubDiv*> sl;
SubDiv *s = 0; SubDiv *s = 0;
SubDivTree *tree = new SubDivTree(); SubDivTree *tree = new SubDivTree();

View File

@ -18,7 +18,7 @@ class TREFile : public SubFile
public: public:
TREFile(const IMGData *img) TREFile(const IMGData *img)
: SubFile(img), _flags(0), _extItemSize(0) {} : SubFile(img), _flags(0), _extItemSize(0) {}
TREFile(const QString *path) TREFile(const QString &path)
: SubFile(path), _flags(0), _extItemSize(0) {} : SubFile(path), _flags(0), _extItemSize(0) {}
TREFile(const SubFile *gmp, quint32 offset) TREFile(const SubFile *gmp, quint32 offset)
: SubFile(gmp, offset), _flags(0), _extItemSize(0) {} : SubFile(gmp, offset), _flags(0), _extItemSize(0) {}

View File

@ -53,9 +53,12 @@ bool VectorTile::init(QFile *file)
bool VectorTile::initGMP(QFile *file) bool VectorTile::initGMP(QFile *file)
{ {
SubFile::Handle hdl(file, _gmp); SubFile::Handle hdl(_gmp, file);
quint32 tre, rgn, lbl, net, nod, dem; quint32 tre, rgn, lbl, net, nod, dem;
if (_tre || _rgn || _lbl || _net || _nod || _dem)
return false;
if (!(_gmp->seek(hdl, 0x19) && _gmp->readUInt32(hdl, tre) if (!(_gmp->seek(hdl, 0x19) && _gmp->readUInt32(hdl, tre)
&& _gmp->readUInt32(hdl, rgn) && _gmp->readUInt32(hdl, lbl) && _gmp->readUInt32(hdl, rgn) && _gmp->readUInt32(hdl, lbl)
&& _gmp->readUInt32(hdl, net) && _gmp->readUInt32(hdl, nod) && _gmp->readUInt32(hdl, net) && _gmp->readUInt32(hdl, nod)
@ -133,10 +136,10 @@ void VectorTile::polys(QFile *file, const RectC &rect, const Zoom &zoom,
} }
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(file, _rgn); rgnHdl = new SubFile::Handle(_rgn, file);
lblHdl = new SubFile::Handle(file, _lbl); lblHdl = new SubFile::Handle(_lbl, file);
netHdl = new SubFile::Handle(file, _net); netHdl = new SubFile::Handle(_net, file);
nodHdl = new SubFile::Handle(file, _nod); nodHdl = new SubFile::Handle(_nod, file);
if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) { if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) {
_lock.unlock(); _lock.unlock();
@ -159,9 +162,9 @@ void VectorTile::polys(QFile *file, const RectC &rect, const Zoom &zoom,
quint32 shift = _tre->shift(subdiv->bits()); quint32 shift = _tre->shift(subdiv->bits());
if (!rgnHdl) { if (!rgnHdl) {
rgnHdl = new SubFile::Handle(file, _rgn); rgnHdl = new SubFile::Handle(_rgn, file);
lblHdl = new SubFile::Handle(file, _lbl); lblHdl = new SubFile::Handle(_lbl, file);
netHdl = new SubFile::Handle(file, _net); netHdl = new SubFile::Handle(_net, file);
} }
if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) { if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) {
@ -182,9 +185,9 @@ void VectorTile::polys(QFile *file, const RectC &rect, const Zoom &zoom,
if (_net && _net->hasLinks()) { if (_net && _net->hasLinks()) {
if (!nodHdl) if (!nodHdl)
nodHdl = new SubFile::Handle(file, _nod); nodHdl = new SubFile::Handle(_nod, file);
if (!nodHdl2) if (!nodHdl2)
nodHdl2 = new SubFile::Handle(file, _nod); nodHdl2 = new SubFile::Handle(_nod, file);
_rgn->links(*rgnHdl, subdiv, shift, _net, *netHdl, _nod, *nodHdl, _rgn->links(*rgnHdl, subdiv, shift, _net, *netHdl, _nod, *nodHdl,
*nodHdl2, _lbl, *lblHdl, &polys->lines); *nodHdl2, _lbl, *lblHdl, &polys->lines);
} }
@ -221,10 +224,10 @@ void VectorTile::points(QFile *file, const RectC &rect, const Zoom &zoom,
} }
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(file, _rgn); rgnHdl = new SubFile::Handle(_rgn, file);
lblHdl = new SubFile::Handle(file, _lbl); lblHdl = new SubFile::Handle(_lbl, file);
SubFile::Handle nodHdl(file, _nod); SubFile::Handle nodHdl(_nod, file);
SubFile::Handle netHdl(file, _net); SubFile::Handle netHdl(_net, file);
if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) { if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) {
_lock.unlock(); _lock.unlock();
@ -245,8 +248,8 @@ void VectorTile::points(QFile *file, const RectC &rect, const Zoom &zoom,
cacheLock->unlock(); cacheLock->unlock();
if (!rgnHdl) { if (!rgnHdl) {
rgnHdl = new SubFile::Handle(file, _rgn); rgnHdl = new SubFile::Handle(_rgn, file);
lblHdl = new SubFile::Handle(file, _lbl); lblHdl = new SubFile::Handle(_lbl, file);
} }
if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) { if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) {
@ -290,7 +293,7 @@ void VectorTile::elevations(QFile *file, const RectC &rect, const Zoom &zoom,
} }
if (!_demLoaded) { if (!_demLoaded) {
hdl = new SubFile::Handle(file, _dem); hdl = new SubFile::Handle(_dem, file);
if (!loadDem(*hdl)) { if (!loadDem(*hdl)) {
_demLock.unlock(); _demLock.unlock();
@ -315,7 +318,7 @@ void VectorTile::elevations(QFile *file, const RectC &rect, const Zoom &zoom,
cacheLock->unlock(); cacheLock->unlock();
if (!hdl) if (!hdl)
hdl = new SubFile::Handle(file, _dem); hdl = new SubFile::Handle(_dem, file);
el = _dem->elevations(*hdl, level, tile); el = _dem->elevations(*hdl, level, tile);
if (!el->m.isNull()) if (!el->m.isNull())

View File

@ -21,7 +21,7 @@ public:
delete _dem; delete _gmp; delete _dem; delete _gmp;
} }
bool init(QFile *file); bool init(QFile *file = 0);
void clear(); void clear();
const RectC &bounds() const {return _tre->bounds();} const RectC &bounds() const {return _tre->bounds();}
@ -49,25 +49,37 @@ public:
} }
template<typename T> template<typename T>
SubFile *addFile(T *container, SubFile::Type type) SubFile *addFile(T container, SubFile::Type type)
{ {
switch (type) { switch (type) {
case SubFile::TRE: case SubFile::TRE:
if (_tre)
return 0;
_tre = new TREFile(container); _tre = new TREFile(container);
return _tre; return _tre;
case SubFile::RGN: case SubFile::RGN:
if (_rgn)
return 0;
_rgn = new RGNFile(container); _rgn = new RGNFile(container);
return _rgn; return _rgn;
case SubFile::LBL: case SubFile::LBL:
if (_lbl)
return 0;
_lbl = new LBLFile(container); _lbl = new LBLFile(container);
return _lbl; return _lbl;
case SubFile::NET: case SubFile::NET:
if (_net)
return 0;
_net = new NETFile(container); _net = new NETFile(container);
return _net; return _net;
case SubFile::NOD: case SubFile::NOD:
if (_nod)
return 0;
_nod = new NODFile(container); _nod = new NODFile(container);
return _nod; return _nod;
case SubFile::DEM: case SubFile::DEM:
if (_dem)
return 0;
_dem = new DEMFile(container); _dem = new DEMFile(container);
return _dem; return _dem;
case SubFile::GMP: case SubFile::GMP: