Skip to content

Commit 93cfe0a

Browse files
authored
Merge pull request #49 from JGI-Bioinformatics/master
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
2 parents 80e311f + 364c40f commit 93cfe0a

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/local
2+
/build*
3+
/nbproject/
4+
/.vscode/

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
2+
project(zstr)
3+
4+
if(${CMAKE_VERSION} VERSION_LESS 3.13)
5+
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)")
6+
endif()
7+
8+
# zlib is required
9+
find_package(ZLIB 1.2.3 REQUIRED)
10+
11+
add_library(zstr INTERFACE)
12+
target_include_directories(zstr INTERFACE src ${ZLIB_INCLUDE_DIRS})
13+
target_link_libraries(zstr INTERFACE ${ZLIB_LIBRARIES})
14+
15+
message(STATUS "Adding zstr and ZLIB for ${PROJECT_NAME}: includes: ${CMAKE_CURRENT_SOURCE_DIR}/src ${ZLIB_INCLUDE_DIRS} and libraries: ${ZLIB_LIBRARIES}")
16+

README.org

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ For input access (decompression), the compression format is auto-detected, and m
1010

1111
For output access (compression), the only parameter exposed by this API is the compression level.
1212

13+
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.
14+
For cmake versions < 3.13, you will also need to 'include_directories(zstr/src)'
15+
1316
Alternatives to this library include:
1417

1518
- 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.

src/zstr.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ class istreambuf
255255
bool is_text;
256256
int window_bits;
257257

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

261260
class ostreambuf
@@ -354,8 +353,6 @@ class ostreambuf
354353
std::size_t buff_size;
355354
bool failed = false;
356355

357-
public:
358-
static const std::size_t default_buff_size = static_cast<std::size_t>(1 << 20);
359356
}; // class ostreambuf
360357

361358
class istream
@@ -420,17 +417,17 @@ class ifstream
420417
public std::istream
421418
{
422419
public:
423-
explicit ifstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
420+
explicit ifstream(const std::string filename, std::ios_base::openmode mode = std::ios_base::in, size_t buff_size = default_buff_size)
424421
: detail::strict_fstream_holder< strict_fstream::ifstream >(filename, mode),
425-
std::istream(new istreambuf(_fs.rdbuf()))
422+
std::istream(new istreambuf(_fs.rdbuf(), buff_size))
426423
{
427424
exceptions(std::ios_base::badbit);
428425
}
429426
explicit ifstream(): detail::strict_fstream_holder< strict_fstream::ifstream >(), std::istream(new istreambuf(_fs.rdbuf())){}
430427
void close() {
431428
_fs.close();
432429
}
433-
void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in) {
430+
void open(const std::string filename, std::ios_base::openmode mode = std::ios_base::in) {
434431
_fs.open(filename, mode);
435432
std::istream::operator=(std::istream(new istreambuf(_fs.rdbuf())));
436433
}
@@ -439,6 +436,7 @@ class ifstream
439436
}
440437
virtual ~ifstream()
441438
{
439+
if (_fs.is_open()) close();
442440
if (rdbuf()) delete rdbuf();
443441
}
444442

@@ -454,10 +452,10 @@ class ofstream
454452
public std::ostream
455453
{
456454
public:
457-
explicit ofstream(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out,
458-
int level = Z_DEFAULT_COMPRESSION)
455+
explicit ofstream(const std::string filename, std::ios_base::openmode mode = std::ios_base::out,
456+
int level = Z_DEFAULT_COMPRESSION, size_t buff_size = default_buff_size)
459457
: detail::strict_fstream_holder< strict_fstream::ofstream >(filename, mode | std::ios_base::binary),
460-
std::ostream(new ostreambuf(_fs.rdbuf(), ostreambuf::default_buff_size, level))
458+
std::ostream(new ostreambuf(_fs.rdbuf(), buff_size, level))
461459
{
462460
exceptions(std::ios_base::badbit);
463461
}
@@ -466,10 +464,10 @@ class ofstream
466464
std::ostream::flush();
467465
_fs.close();
468466
}
469-
void open(const std::string& filename, std::ios_base::openmode mode = std::ios_base::out, int level = Z_DEFAULT_COMPRESSION) {
467+
void open(const std::string filename, std::ios_base::openmode mode = std::ios_base::out, int level = Z_DEFAULT_COMPRESSION) {
470468
flush();
471469
_fs.open(filename, mode | std::ios_base::binary);
472-
std::ostream::operator=(std::ostream(new ostreambuf(_fs.rdbuf(), ostreambuf::default_buff_size, level)));
470+
std::ostream::operator=(std::ostream(new ostreambuf(_fs.rdbuf(), default_buff_size, level)));
473471
}
474472
bool is_open() const {
475473
return _fs.is_open();
@@ -481,6 +479,7 @@ class ofstream
481479
}
482480
virtual ~ofstream()
483481
{
482+
if (_fs.is_open()) close();
484483
if (rdbuf()) delete rdbuf();
485484
}
486485

0 commit comments

Comments
 (0)