Skip to content

Commit

Permalink
Merge pull request #49 from JGI-Bioinformatics/master
Browse files Browse the repository at this point in the history
Flush close on destroy, CMake and buff_size
Added CMakeLists.txt and included buff_size in the constructor of the i/ofstream classes
Including a close on the underlying I/ofstream on destruction
  • Loading branch information
ferdymercury authored Dec 10, 2021
2 parents 80e311f + 364c40f commit 93cfe0a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/local
/build*
/nbproject/
/.vscode/
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")

3 changes: 3 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 10 additions & 11 deletions src/zstr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ class istreambuf
bool is_text;
int window_bits;

static const std::size_t default_buff_size = static_cast<std::size_t>(1 << 20);
}; // class istreambuf

class ostreambuf
Expand Down Expand Up @@ -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<std::size_t>(1 << 20);
}; // class ostreambuf

class istream
Expand Down Expand Up @@ -420,17 +417,17 @@ 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);
}
explicit ifstream(): detail::strict_fstream_holder< strict_fstream::ifstream >(), std::istream(new istreambuf(_fs.rdbuf())){}
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())));
}
Expand All @@ -439,6 +436,7 @@ class ifstream
}
virtual ~ifstream()
{
if (_fs.is_open()) close();
if (rdbuf()) delete rdbuf();
}

Expand All @@ -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);
}
Expand All @@ -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();
Expand All @@ -481,6 +479,7 @@ class ofstream
}
virtual ~ofstream()
{
if (_fs.is_open()) close();
if (rdbuf()) delete rdbuf();
}

Expand Down

0 comments on commit 93cfe0a

Please sign in to comment.