source: subversion/applications/utils/import/srtm2wayinfo/zip.cpp

Last change on this file was 16775, checked in by herm, 8 years ago

Copyright messages added to all files.
Small documentation changes.

File size: 3.0 KB
Line 
1/* Copyright (c) 2009 Hermann Kraus
2 * This software is available under a "MIT Style" license
3 * (see COPYING).
4 */
5/** \file
6    Read SRTM zip files. */
7#include "zip.h"
8#include "settings.h"
9
10#include <QFileInfo>
11#include <QDebug>
12#include <zzip/lib.h>
13#include <math.h>
14
15
16
17/** Creates a buffer of all the data in the SRTM file. The uncompressed version is used if available.
18  *
19  * \returns The length of the side of the data (e.g. 1201 or 3601)
20  * \note The buffer returned is owned by the caller of this function and _must_ be freed after usage.
21  */
22int SrtmZipFile::getData(QString filename, qint16 **buffer)
23{
24    *buffer = 0;
25   
26    QFileInfo fi(filename);
27    QString uncompressedFile = fi.path()+'/'+fi.completeBaseName();
28
29    int size = 0;
30    if (QFileInfo(uncompressedFile).exists()) {
31        QFile file(uncompressedFile);
32        if (!file.open(QIODevice::ReadOnly)) {
33            qCritical() << "ZIP(Uncompressed): Could not open file" << uncompressedFile << file.errorString();
34            return 0;
35        }
36        size = sqrt(file.size()/2);
37        if (size*size*2 != file.size()) {
38            qCritical() << "ZIP(Uncompressed): Invalid data: Not a square!";
39        }
40        *buffer = new qint16[file.size()/2];
41        if (!*buffer) {
42            qCritical() << "ZIP(Uncompressed): Could not allocate buffer.";
43            return 0;
44        }
45        if (file.read((char *)*buffer, file.size()) != file.size()) {
46            qCritical() << "ZIP(Uncompressed): Could not read all bytes.";
47        }
48        file.close();
49    } else {
50        ZZIP_DIR* dir = zzip_dir_open(filename.toAscii(), 0);
51        if (!dir) {
52            qCritical() << "ZIP: Could not open zip file" << filename;
53            return 0;
54        }
55        ZZIP_FILE* fp = zzip_file_open(dir, fi.completeBaseName().toAscii(), 0);
56        if (!fp) {
57            qCritical() << "ZIP: Could not find" <<  fi.completeBaseName() << "in" << filename;
58            return 0;
59        }
60        ZZIP_STAT stat;
61        if (zzip_file_stat(fp, &stat) == -1) {
62            qCritical() << "ZIP: Could not get info about" << uncompressedFile;
63            return 0;
64        }
65       
66        size = sqrt(stat.st_size/2);
67        if (size*size*2 != stat.st_size) {
68            qCritical() << "ZIP: Invalid data: Not a square!";
69        }
70        *buffer = new qint16[stat.st_size/2];
71
72        if (zzip_file_read(fp, *buffer, stat.st_size) != stat.st_size) {
73            qCritical() << "ZIP: Could not read all bytes.";
74            delete *buffer;
75            *buffer = 0;
76            return 0;
77        }
78
79        if (global_settings.getStoreUncompressed()) {
80            QFile file(uncompressedFile);
81            if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
82                qCritical() << "ZIP(Writing): Could not open file" << uncompressedFile << file.errorString();
83            } else {
84                file.write((char *)*buffer, stat.st_size);
85                file.close();
86            }
87        }
88        zzip_file_close(fp);
89        zzip_dir_close(dir);
90    }
91    return size;
92}
Note: See TracBrowser for help on using the repository browser.