Skip to content

Commit

Permalink
Fix windows build.
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Nov 26, 2018
1 parent e1912a8 commit 0716247
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
24 changes: 21 additions & 3 deletions include/osmium/io/bzip2_compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,18 @@ namespace osmium {

detail::file_wrapper m_file;
int m_bzerror;
BZFILE* m_bzfile;
BZFILE* m_bzfile = nullptr;

public:

explicit Bzip2Compressor(const int fd, const fsync sync) :
Compressor(sync),
m_file(fd, "wb"),
m_bzerror(BZ_OK),
m_bzfile(::BZ2_bzWriteOpen(&m_bzerror, m_file.file(), 6, 0, 0)) {
m_bzerror(BZ_OK) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
m_bzfile = ::BZ2_bzWriteOpen(&m_bzerror, m_file.file(), 6, 0, 0);
if (!m_bzfile) {
detail::throw_bzip2_error(m_bzfile, "write open failed", m_bzerror);
}
Expand All @@ -189,6 +192,9 @@ namespace osmium {
void write(const std::string& data) final {
assert(data.size() < std::numeric_limits<int>::max());
assert(m_bzfile);
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
int error = BZ_OK;
::BZ2_bzWrite(&error, m_bzfile, const_cast<char*>(data.data()), static_cast<int>(data.size()));
if (error != BZ_OK && error != BZ_STREAM_END) {
Expand All @@ -198,6 +204,9 @@ namespace osmium {

void close() final {
if (m_bzfile) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
int error = BZ_OK;
::BZ2_bzWriteClose(&error, m_bzfile, 0, nullptr, nullptr);
m_bzfile = nullptr;
Expand All @@ -223,6 +232,9 @@ namespace osmium {

explicit Bzip2Decompressor(const int fd) :
m_file(fd, "rb") {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
int bzerror = BZ_OK;
m_bzfile = ::BZ2_bzReadOpen(&bzerror, m_file.file(), 0, 0, nullptr, 0);
if (!m_bzfile) {
Expand All @@ -245,6 +257,9 @@ namespace osmium {
}

std::string read() final {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
assert(m_bzfile);
std::string buffer;

Expand Down Expand Up @@ -288,6 +303,9 @@ namespace osmium {

void close() final {
if (m_bzfile) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
int error = BZ_OK;
::BZ2_bzReadClose(&error, m_bzfile);
m_bzfile = nullptr;
Expand Down
3 changes: 3 additions & 0 deletions include/osmium/io/detail/read_write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ namespace osmium {
}

inline void reliable_close(const int fd) {
if (fd < 0) {
return;
}
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
Expand Down
27 changes: 24 additions & 3 deletions include/osmium/io/gzip_compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ namespace osmium {

explicit GzipCompressor(const int fd, const fsync sync) :
Compressor(sync),
m_fd(osmium::io::detail::reliable_dup(fd)),
m_gzfile(::gzdopen(fd, "wb")) {
m_fd(osmium::io::detail::reliable_dup(fd)) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
m_gzfile = ::gzdopen(fd, "wb");
if (!m_gzfile) {
detail::throw_gzip_error(m_gzfile, "write initialization failed");
}
Expand All @@ -130,6 +133,9 @@ namespace osmium {
}

void write(const std::string& data) final {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
assert(m_gzfile);
assert(data.size() < std::numeric_limits<unsigned int>::max());
if (!data.empty()) {
Expand All @@ -142,6 +148,9 @@ namespace osmium {

void close() final {
if (m_gzfile) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
const int result = ::gzclose(m_gzfile);
m_gzfile = nullptr;
if (result != Z_OK) {
Expand All @@ -163,9 +172,15 @@ namespace osmium {
public:

explicit GzipDecompressor(const int fd) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
m_gzfile = ::gzdopen(fd, "rb");
if (!m_gzfile) {
::close(fd);
try {
osmium::io::detail::reliable_close(fd);
} catch (...) {
}
detail::throw_gzip_error(m_gzfile, "read initialization failed");
}
}
Expand All @@ -185,6 +200,9 @@ namespace osmium {
}

std::string read() final {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
assert(m_gzfile);
std::string buffer(osmium::io::Decompressor::input_buffer_size, '\0');
assert(buffer.size() < std::numeric_limits<unsigned int>::max());
Expand All @@ -201,6 +219,9 @@ namespace osmium {

void close() final {
if (m_gzfile) {
#ifdef _MSC_VER
osmium::detail::disable_invalid_parameter_handler diph;
#endif
const int result = ::gzclose(m_gzfile);
m_gzfile = nullptr;
if (result != Z_OK) {
Expand Down
2 changes: 1 addition & 1 deletion test/t/io/test_compression_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

TEST_CASE("Create compressor using factory") {
const auto& factory = osmium::io::CompressionFactory::instance();
REQUIRE(factory.create_compressor(osmium::io::file_compression::none, 1, osmium::io::fsync::no));
REQUIRE(factory.create_compressor(osmium::io::file_compression::none, -1, osmium::io::fsync::no));
}

TEST_CASE("Create decompressor using factory") {
Expand Down

0 comments on commit 0716247

Please sign in to comment.