Skip to content

Commit

Permalink
Merge pull request #30 from CMurtagh-LGTM/cmurtagh/default_constructor
Browse files Browse the repository at this point in the history
Thanks so much for this contribution :)
  • Loading branch information
ferdymercury authored Jun 21, 2021
2 parents 7c7f4a3 + f5c8c50 commit becd7a5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/zstr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ struct strict_fstream_holder
strict_fstream_holder(const std::string& filename, std::ios_base::openmode mode = std::ios_base::in)
: _fs(filename, mode)
{}
strict_fstream_holder() = default;
bool is_open() const {
return _fs.is_open();
}
FStream_Type _fs;
}; // class strict_fstream_holder

Expand All @@ -425,6 +429,14 @@ class ifstream
{
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){
_fs.open(filename, mode);
std::istream::operator=(std::istream(new istreambuf(_fs.rdbuf())));
}
virtual ~ifstream()
{
if (rdbuf()) delete rdbuf();
Expand All @@ -443,6 +455,16 @@ class ofstream
{
exceptions(std::ios_base::badbit);
}
explicit ofstream(): detail::strict_fstream_holder< strict_fstream::ofstream >(), std::ostream(new ostreambuf(_fs.rdbuf())){}
void close() {
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){
flush();
_fs.open(filename, mode | std::ios_base::binary);
std::ostream::operator=(std::ostream(new ostreambuf(_fs.rdbuf(), ostreambuf::default_buff_size, level)));
}
ofstream& flush() {
std::ostream::flush();
_fs.flush();
Expand Down

0 comments on commit becd7a5

Please sign in to comment.