1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Fixed error handling + code cleanup

This commit is contained in:
Martin Tůma 2020-01-19 13:23:20 +01:00
parent a486abb159
commit 040de56a54
8 changed files with 51 additions and 90 deletions

View File

@ -7,12 +7,6 @@
#define CACHE_SIZE 8388608 /* 8MB */
#define CHECK(condition) \
if (!(condition)) { \
_errorString = "Unsupported or invalid IMG file"; \
return; \
}
struct CTX
{
CTX(const RectC &rect, int bits, QList<IMG::Poly> *polygons,
@ -27,9 +21,37 @@ struct CTX
QList<IMG::Point> *points;
};
static SubFile::Type tileType(const char str[3])
{
if (!memcmp(str, "TRE", 3))
return SubFile::TRE;
else if (!memcmp(str, "RGN", 3))
return SubFile::RGN;
else if (!memcmp(str, "LBL", 3))
return SubFile::LBL;
else if (!memcmp(str, "TYP", 3))
return SubFile::TYP;
else if (!memcmp(str, "GMP", 3))
return SubFile::GMP;
else if (!memcmp(str, "NET", 3))
return SubFile::NET;
else
return SubFile::Unknown;
}
IMG::IMG(const QString &fileName)
: _file(fileName), _typ(0), _style(0), _valid(false)
{
#define CHECK(condition) \
if (!(condition)) { \
_errorString = "Unsupported or invalid IMG file"; \
qDeleteAll(tileMap); \
return; \
}
QMap<QString, VectorTile*> tileMap;
QString typFile;
if (!_file.open(QFile::ReadOnly)) {
_errorString = _file.errorString();
return;
@ -75,19 +97,15 @@ IMG::IMG(const QString &fileName)
offset += 512;
int cnt = (size - offset) / 512;
QMap<QString, VectorTile*> tileMap;
QString typFile;
// Read FAT blocks describing the IMG sub-files
for (int i = 0; i < cnt; i++) {
quint16 block;
CHECK(_file.seek(offset) && readValue(flag) && read(name, sizeof(name))
&& read(type, sizeof(type)) && readValue(size) && readValue(part));
SubFile::Type tt = SubFile::type(type);
SubFile::Type tt = tileType(type);
QString fn(QByteArray(name, sizeof(name)));
if (SubFile::isTileFile(tt)) {
if (VectorTile::isTileFile(tt)) {
VectorTile *tile;
QMap<QString, VectorTile*>::iterator it = tileMap.find(fn);
if (it == tileMap.end()) {
@ -166,7 +184,7 @@ void IMG::load()
_style = new Style(_typ);
else {
QFile typFile(ProgramPaths::typFile());
if (typFile.exists()) {
if (typFile.open(QIODevice::ReadOnly)) {
SubFile typ(&typFile);
_style = new Style(&typ);
} else

View File

@ -1,14 +1,12 @@
#ifndef IMG_H
#define IMG_H
#include <QRect>
#include <QFile>
#include <QByteArray>
#include <QCache>
#include <QDebug>
#include "common/rtree.h"
#include "common/rectc.h"
#include "common/range.h"
#include "style.h"
#include "label.h"

View File

@ -2,35 +2,9 @@
#include "img.h"
#include "subfile.h"
SubFile::Type SubFile::type(const char str[3])
{
if (!memcmp(str, "TRE", 3))
return TRE;
else if (!memcmp(str, "RGN", 3))
return RGN;
else if (!memcmp(str, "LBL", 3))
return LBL;
else if (!memcmp(str, "TYP", 3))
return TYP;
else if (!memcmp(str, "GMP", 3))
return GMP;
else if (!memcmp(str, "NET", 3))
return NET;
else
return Unknown;
}
SubFile::SubFile(QFile *file) :_gmpOffset(0), _img(0), _file(file), _blocks(0)
{
if (!_file->open(QIODevice::ReadOnly))
qWarning("Error opening %s: %s", qPrintable(_file->fileName()),
qPrintable(_file->errorString()));
}
bool SubFile::seek(Handle &handle, quint32 pos) const
{
Q_ASSERT(_img || _file);
if (_file)
return _file->seek(pos);
else {
@ -54,8 +28,6 @@ bool SubFile::seek(Handle &handle, quint32 pos) const
bool SubFile::readByte(Handle &handle, quint8 &val) const
{
Q_ASSERT(_img || _file);
if (_file)
return _file->getChar((char*)&val);
else {
@ -101,20 +73,3 @@ QString SubFile::fileName() const
{
return _file ? _file->fileName() : _img->fileName();
}
#ifndef QT_NO_DEBUG
QDebug operator<<(QDebug dbg, const SubFile &file)
{
bool continuous = true;
for (int i = 1; i < file._blocks->size(); i++) {
if (file._blocks->at(i) != file._blocks->at(i-1) + 1) {
continuous = false;
break;
}
}
dbg.nospace() << "SubFile(" << file._blocks->size() << ", "
<< continuous << ")";
return dbg.space();
}
#endif // QT_NO_DEBUG

View File

@ -22,11 +22,17 @@ public:
int pos;
};
SubFile(IMG *img) : _gmpOffset(0), _img(img), _file(0),
_blocks(&_blockData) {}
SubFile(IMG *img)
: _gmpOffset(0), _img(img), _blocks(new QVector<quint16>()), _file(0) {}
SubFile(SubFile *gmp, quint32 offset) : _gmpOffset(offset), _img(gmp->_img),
_file(0), _blocks(&(gmp->_blockData)) {}
SubFile(QFile *file);
_blocks(gmp->_blocks), _file(gmp->_file) {}
SubFile(QFile *file)
: _gmpOffset(0), _img(0), _blocks(0), _file(file) {}
~SubFile()
{
if (!_gmpOffset)
delete _blocks;
}
void addBlock(quint16 block) {_blocks->append(block);}
@ -86,27 +92,13 @@ public:
quint16 offset() const {return _blocks->first();}
QString fileName() const;
static Type type(const char str[3]);
static bool isTileFile(Type type)
{
return (type == TRE || type == LBL || type == RGN || type == NET
|| type == GMP);
}
friend QDebug operator<<(QDebug dbg, const SubFile &file);
protected:
quint32 _gmpOffset;
private:
IMG *_img;
QFile *_file;
QVector<quint16> *_blocks;
QVector<quint16> _blockData;
QFile *_file;
};
#ifndef QT_NO_DEBUG
QDebug operator<<(QDebug dbg, const SubFile &file);
#endif // QT_NO_DEBUG
#endif // SUBFILE_H

View File

@ -85,16 +85,7 @@ void VectorTile::objects(const RectC &rect, int bits,
#ifndef QT_NO_DEBUG
QDebug operator<<(QDebug dbg, const VectorTile &tile)
{
dbg.nospace() << "VectorTile(";
if (tile._tre)
dbg << "TRE: " << *(tile._tre);
if (tile._rgn)
dbg << ", RGN: " << *(tile._rgn);
if (tile._lbl)
dbg << ", LBL: " << *(tile._lbl);
if (tile._net)
dbg << ", NET: " << *(tile._net);
dbg << ")";
dbg.nospace() << "VectorTile(" << tile.bounds() <<")";
return dbg.space();
}

View File

@ -25,6 +25,13 @@ public:
void objects(const RectC &rect, int bits, QList<IMG::Poly> *polygons,
QList<IMG::Poly> *lines, QList<IMG::Point> *points) const;
static bool isTileFile(SubFile::Type type)
{
return (type == SubFile::TRE || type == SubFile::LBL
|| type == SubFile::RGN || type == SubFile::NET
|| type == SubFile::GMP);
}
friend QDebug operator<<(QDebug dbg, const VectorTile &tile);
private:

View File

@ -1,5 +1,6 @@
#include <QFile>
#include <QPainter>
#include <QFont>
#include <QPixmapCache>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtCore>

View File

@ -1,7 +1,6 @@
#ifndef IMGMAP_H
#define IMGMAP_H
#include <QFont>
#include "map.h"
#include "projection.h"
#include "transform.h"