diff --git a/src/map/IMG/img.cpp b/src/map/IMG/img.cpp index 7ffc4f38..3dcc20a2 100644 --- a/src/map/IMG/img.cpp +++ b/src/map/IMG/img.cpp @@ -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 *polygons, @@ -27,9 +21,37 @@ struct CTX QList *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 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 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::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 diff --git a/src/map/IMG/img.h b/src/map/IMG/img.h index 925963e9..55cf3151 100644 --- a/src/map/IMG/img.h +++ b/src/map/IMG/img.h @@ -1,14 +1,12 @@ #ifndef IMG_H #define IMG_H -#include #include #include #include #include #include "common/rtree.h" #include "common/rectc.h" -#include "common/range.h" #include "style.h" #include "label.h" diff --git a/src/map/IMG/subfile.cpp b/src/map/IMG/subfile.cpp index d653d28b..589697bf 100644 --- a/src/map/IMG/subfile.cpp +++ b/src/map/IMG/subfile.cpp @@ -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 diff --git a/src/map/IMG/subfile.h b/src/map/IMG/subfile.h index 55b2535a..5245f339 100644 --- a/src/map/IMG/subfile.h +++ b/src/map/IMG/subfile.h @@ -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()), _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 *_blocks; - QVector _blockData; + QFile *_file; }; -#ifndef QT_NO_DEBUG -QDebug operator<<(QDebug dbg, const SubFile &file); -#endif // QT_NO_DEBUG - #endif // SUBFILE_H diff --git a/src/map/IMG/vectortile.cpp b/src/map/IMG/vectortile.cpp index ee721d2d..070a9348 100644 --- a/src/map/IMG/vectortile.cpp +++ b/src/map/IMG/vectortile.cpp @@ -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(); } diff --git a/src/map/IMG/vectortile.h b/src/map/IMG/vectortile.h index 101d0cc2..423550db 100644 --- a/src/map/IMG/vectortile.h +++ b/src/map/IMG/vectortile.h @@ -25,6 +25,13 @@ public: void objects(const RectC &rect, int bits, QList *polygons, QList *lines, QList *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: diff --git a/src/map/imgmap.cpp b/src/map/imgmap.cpp index 70940c9c..dc8f660f 100644 --- a/src/map/imgmap.cpp +++ b/src/map/imgmap.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) #include diff --git a/src/map/imgmap.h b/src/map/imgmap.h index b83d07df..2dfbd7dd 100644 --- a/src/map/imgmap.h +++ b/src/map/imgmap.h @@ -1,7 +1,6 @@ #ifndef IMGMAP_H #define IMGMAP_H -#include #include "map.h" #include "projection.h" #include "transform.h"