diff --git a/.gitignore b/.gitignore index 2c553cb..59060d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /local +/build* +/nbproject/ +/.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f8e13a4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(zstr) + +if(${CMAKE_VERSION} VERSION_LESS 3.13) + message(WARNING "Interface libraries are not well supported before cmake 3.13, you will need to include within the parent CMakeLists.txt: INCLUDE_DIRECTORIES(zstr/src)") +endif() + +# zlib is required +find_package(ZLIB 1.2.3 REQUIRED) + +add_library(zstr INTERFACE) +target_include_directories(zstr INTERFACE src ${ZLIB_INCLUDE_DIRS}) +target_link_libraries(zstr INTERFACE ${ZLIB_LIBRARIES}) + +message(STATUS "Adding zstr and ZLIB for ${PROJECT_NAME}: includes: ${CMAKE_CURRENT_SOURCE_DIR}/src ${ZLIB_INCLUDE_DIRS} and libraries: ${ZLIB_LIBRARIES}") + diff --git a/README.org b/README.org index dc4e41b..2136271 100644 --- a/README.org +++ b/README.org @@ -10,6 +10,9 @@ For input access (decompression), the compression format is auto-detected, and m For output access (compression), the only parameter exposed by this API is the compression level. +To add zstr within a CMake project, include this directory within your CMakeLists.txxt: 'add_subdirectory(zstr)' and include zstr for each required target when specifiying target_link_libraries. + For cmake versions < 3.13, you will also need to 'include_directories(zstr/src)' + Alternatives to this library include: - The original [[http://www.zlib.net/][ZLib]], through its [[http://www.zlib.net/manual.html][C API]]. This does not interact nicely with C++ iostreams. diff --git a/src/zstr.hpp b/src/zstr.hpp index 0edb9d7..d8b4fbe 100644 --- a/src/zstr.hpp +++ b/src/zstr.hpp @@ -255,7 +255,6 @@ class istreambuf bool is_text; int window_bits; - static const std::size_t default_buff_size = static_cast(1 << 20); }; // class istreambuf class ostreambuf @@ -354,8 +353,6 @@ class ostreambuf std::size_t buff_size; bool failed = false; -public: - static const std::size_t default_buff_size = static_cast(1 << 20); }; // class ostreambuf class istream @@ -420,9 +417,9 @@ class ifstream public std::istream { public: - explicit ifstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in) + explicit ifstream(const std::string filename, std::ios_base::openmode mode = std::ios_base::in, size_t buff_size = default_buff_size) : detail::strict_fstream_holder< strict_fstream::ifstream >(filename, mode), - std::istream(new istreambuf(_fs.rdbuf())) + std::istream(new istreambuf(_fs.rdbuf(), buff_size)) { exceptions(std::ios_base::badbit); } @@ -430,7 +427,7 @@ class ifstream void close() { _fs.close(); } - void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in) { + void open(const std::string filename, std::ios_base::openmode mode = std::ios_base::in) { _fs.open(filename, mode); std::istream::operator=(std::istream(new istreambuf(_fs.rdbuf()))); } @@ -439,6 +436,7 @@ class ifstream } virtual ~ifstream() { + if (_fs.is_open()) close(); if (rdbuf()) delete rdbuf(); } @@ -454,10 +452,10 @@ class ofstream public std::ostream { public: - explicit ofstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out, - int level = Z_DEFAULT_COMPRESSION) + explicit ofstream(const std::string filename, std::ios_base::openmode mode = std::ios_base::out, + int level = Z_DEFAULT_COMPRESSION, size_t buff_size = default_buff_size) : detail::strict_fstream_holder< strict_fstream::ofstream >(filename, mode | std::ios_base::binary), - std::ostream(new ostreambuf(_fs.rdbuf(), ostreambuf::default_buff_size, level)) + std::ostream(new ostreambuf(_fs.rdbuf(), buff_size, level)) { exceptions(std::ios_base::badbit); } @@ -466,10 +464,10 @@ class ofstream std::ostream::flush(); _fs.close(); } - void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out, int level = Z_DEFAULT_COMPRESSION) { + void open(const std::string filename, std::ios_base::openmode mode = std::ios_base::out, int level = Z_DEFAULT_COMPRESSION) { flush(); _fs.open(filename, mode | std::ios_base::binary); - std::ostream::operator=(std::ostream(new ostreambuf(_fs.rdbuf(), ostreambuf::default_buff_size, level))); + std::ostream::operator=(std::ostream(new ostreambuf(_fs.rdbuf(), default_buff_size, level))); } bool is_open() const { return _fs.is_open(); @@ -481,6 +479,7 @@ class ofstream } virtual ~ofstream() { + if (_fs.is_open()) close(); if (rdbuf()) delete rdbuf(); }