osrm-backend/ThirdParty/osmium/io/gzip_compression.hpp

133 lines
4.3 KiB
C++
Raw Normal View History

2014-08-15 12:47:26 -04:00
#ifndef OSMIUM_IO_GZIP_COMPRESSION_HPP
#define OSMIUM_IO_GZIP_COMPRESSION_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013,2014 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#define OSMIUM_LINK_WITH_LIBS_ZLIB -lz
#include <stdexcept>
#include <string>
#include <zlib.h>
#include <osmium/io/compression.hpp>
#include <osmium/io/file_compression.hpp>
namespace osmium {
namespace io {
class GzipCompressor : public Compressor {
gzFile m_gzfile;
public:
explicit GzipCompressor(int fd) :
Compressor(),
m_gzfile(::gzdopen(fd, "w")) {
if (!m_gzfile) {
throw std::runtime_error("initialization of gzip compression failed");
}
}
~GzipCompressor() override final {
this->close();
}
void write(const std::string& data) override final {
::gzwrite(m_gzfile, data.data(), data.size());
}
void close() override final {
if (m_gzfile) {
::gzclose(m_gzfile);
m_gzfile = nullptr;
}
}
}; // class GzipCompressor
class GzipDecompressor : public Decompressor {
gzFile m_gzfile;
public:
explicit GzipDecompressor(int fd) :
Decompressor(),
m_gzfile(::gzdopen(fd, "r")) {
if (!m_gzfile) {
throw std::runtime_error("initialization of gzip compression failed");
}
}
~GzipDecompressor() override final {
this->close();
}
std::string read() override final {
std::string buffer(osmium::io::Decompressor::input_buffer_size, '\0');
int nread = ::gzread(m_gzfile, const_cast<char*>(buffer.data()), buffer.size());
if (nread < 0) {
throw std::runtime_error("gzip read failed"); // XXX better error detection and reporting
// throw std::system_error(errno, std::system_category(), "Read failed");
}
buffer.resize(static_cast<std::string::size_type>(nread));
return buffer;
}
void close() override final {
if (m_gzfile) {
::gzclose(m_gzfile);
m_gzfile = nullptr;
}
}
}; // class GzipDecompressor
namespace {
const bool registered_gzip_compression = osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::gzip,
[](int fd) { return new osmium::io::GzipCompressor(fd); },
[](int fd) { return new osmium::io::GzipDecompressor(fd); }
);
} // anonymous namespace
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_GZIP_COMPRESSION_HPP