![]() |
Rosetta Utilities
2015.09
|
ozstream: Output file stream wrapper for uncompressed and compressed files More...
#include <ozstream.hh>
Public Member Functions | |
ozstream () | |
Default constructor. More... | |
ozstream (std::string const &filename_a, std::ios_base::openmode open_mode=std::ios_base::out, std::streamsize buf_size=OZSTREAM_DEFAULT_BUFFER_SIZE) | |
Filename constructor. More... | |
virtual | ~ozstream () |
Destructor. More... | |
operator bool () const | |
bool conversion More... | |
operator std::ostream const & () const | |
Stream conversion. More... | |
operator std::ostream & () | |
Stream conversion. More... | |
template<typename T > | |
ozstream & | operator<< (T const &t) |
Stream output: override to preserve type of return value. More... | |
ozstream & | operator<< (manipulator m) |
Stream output overload to replace std::endl with to avoid flushing. More... | |
void | open (std::string const &filename_a, std::ios_base::openmode open_mode=std::ios_base::out) |
Open a file. More... | |
void | open_append (std::string const &filename_a) |
Open a text file for appending. More... | |
void | open_append_if_existed (std::string const &filename_a, std::stringstream &preprinted_header) |
open file as append if it exists, return true if file existed before, false if it is new. More... | |
ozstream & | put (char const c) |
Write a char. More... | |
ozstream & | write (char const *str, std::streamsize const count) |
Write a string. More... | |
ozstream & | write (std::string const &str, std::streamsize const count) |
Write a string. More... | |
ozstream & | flush () |
Flush the stream – currently alias to flush_finalize() More... | |
ozstream & | flush_finalize () |
Flush the streams and finalize the zip stream. More... | |
void | zflush () |
Flush the zip_ostream. More... | |
void | zflush_finalize () |
Flush and finalize the zip_stream. More... | |
void | clear () |
Clear the stream. More... | |
void | close () |
Close the ofstream and reset the state. More... | |
std::ostream const & | operator() () const |
Stream access. More... | |
std::ostream & | operator() () |
Stream access. More... | |
std::ostream const & | stream () const |
Stream access. More... | |
std::ostream & | stream () |
Stream access. More... | |
std::streambuf * | rdbuf () const |
Pointer to the stream buffer. More... | |
std::string const & | filename () const |
File name. More... | |
bool | good () const |
Good? More... | |
bool | eof () const |
End of file? More... | |
bool | fail () const |
Fail? More... | |
bool | bad () const |
Bad? More... | |
bool | compressed () const |
Compressed? More... | |
bool | uncompressed () const |
Uncompressed? More... | |
bool | gzipped () const |
gzipped? More... | |
std::streamsize | buffer_size () const |
get buffer size (bytes) More... | |
void | buffer_size (std::streamsize const &buf_size) |
set buffer size (bytes) More... | |
![]() | |
virtual | ~orstream () |
Destructor. More... | |
template<typename T > | |
orstream & | operator<< (T const &t) |
Stream output. More... | |
Static Public Member Functions | |
static void | enable_MPI_reroute (int min_client_rank, int master_rank) |
static int | MPI_reroute_rank () |
Private Types | |
enum | Compression { NONE, UNCOMPRESSED, GZIP } |
Private Member Functions | |
bool | is_gzip () const |
Is stream attached to a gzip file? More... | |
long | get_crc () const |
CRC of the uncompressed data (see zipstream documentation) More... | |
long | get_in_size () const |
Uncompressed data size. More... | |
long | get_out_size () const |
Compressed data size. More... | |
bool | allocate_assign_char_buffer () |
if character buffer does not exist, create it and assign it to internal ofstream More... | |
bool | destroy_char_buffer () |
if character buffer exists, destroy it More... | |
Private Attributes | |
Compression | compression_ |
Compression state. More... | |
std::ofstream | of_stream_ |
File stream. More... | |
std::string | filename_ |
File name. More... | |
std::streamsize | buffer_size_ |
size of buffer (in bytes) More... | |
char * | char_buffer_p_ |
character buffer pointer (owning) More... | |
zlib_stream::zip_ostream * | zip_stream_p_ |
Zip file stream pointer (owning) More... | |
mpi_stream::mpi_ostream * | mpi_stream_p_ |
Static Private Attributes | |
static bool | bMPI_reroute_stream_ |
static int | mpi_FileBuf_master_rank_ |
Friends | |
long | utility::file::gzip (std::string const &uncompressedfile, bool overwrite) |
long | utility::file::gunzip (std::string const &compressedfile, bool overwrite) |
Additional Inherited Members | |
![]() | |
typedef std::ostream &(* | manipulator )(std::ostream &) |
![]() | |
orstream () | |
Default constructor. More... | |
ozstream: Output file stream wrapper for uncompressed and compressed files
|
private |
|
inline |
Default constructor.
|
inlineexplicit |
Filename constructor.
[in] | filename_a | filename |
[in] | open_mode | opening mode bitmask, use std::ios_base::out for gzip files |
[in] | buf_size | buffer size (bytes), enforced lower bound of 4KB |
References buffer_size(), and open().
|
inlinevirtual |
Destructor.
References close().
|
inlineprivate |
if character buffer does not exist, create it and assign it to internal ofstream
File must be closed for this operation to be successful, otherwise we can run into implementation dependent behavior of std::basic_filebuf.
References buffer_size_, char_buffer_p_, and of_stream_.
Referenced by open(), and open_append().
|
inlinevirtual |
|
inline |
get buffer size (bytes)
In uncompressed mode this is the size of the character buffer. In compressed mode this is the size of the zip buffer.
References buffer_size_.
Referenced by ozstream().
|
inline |
set buffer size (bytes)
In uncompressed mode this is the size of the character buffer. In compressed mode this is the size of the zip buffer. Lower bound of 4KB is enforced due to zipstream requirements. Operation is skipped if the file is currently open, as the buffer is considered locked.
References buffer_size_, max(), and of_stream_.
|
inlinevirtual |
Clear the stream.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inline |
Close the ofstream and reset the state.
References utility::io::mpi_stream::basic_mpi_ostream< Elem, Tr, ElemA, ByteT, ByteAT >::close(), compression_, destroy_char_buffer(), filename_, mpi_stream_p_, NONE, of_stream_, and zip_stream_p_.
Referenced by open(), open_append(), open_append_if_existed(), and ~ozstream().
|
inlinevirtual |
|
inlineprivate |
if character buffer exists, destroy it
File must be closed for this operation to be successful, otherwise we are deleting a buffer that's still in use.
References char_buffer_p_, and of_stream_.
Referenced by close().
References utility_exit_with_message.
|
inlinevirtual |
|
inlinevirtual |
|
inline |
File name.
References filename_.
|
inlinevirtual |
Flush the stream – currently alias to flush_finalize()
Instead doing a regular flush, we currently force a completion of the zip stream. We do this to pre-empt against several things: (1) the expensive operation of closing and re-opening a stream (2) confusion and inconvenience that may result from users calling flushes and ending upon with broken or corrupted files if a job is somehow interrupted (e.g. on a cluster). Please note that calling flush_finalize() too often can seriously degrade compression. zlib documentation appears to imply that every 1MB or so is a reasonable rule of thumb.
Implements utility::io::orstream.
References utility::io::mpi_stream::basic_mpi_ostream< Elem, Tr, ElemA, ByteT, ByteAT >::flush(), flush(), mpi_stream_p_, of_stream_, and zip_stream_p_.
Referenced by flush(), flush_finalize(), and operator<<().
|
inline |
Flush the streams and finalize the zip stream.
Calling this will complete the zip stream. Upon the next write, a new zip stream will be started. Please note that calling flush_finalize() too often can seriously degrade compression. zlib documentation appears to imply that every 1MB or so is a reasonable rule of thumb.
References utility::io::mpi_stream::basic_mpi_ostream< Elem, Tr, ElemA, ByteT, ByteAT >::flush(), flush(), mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlineprivate |
CRC of the uncompressed data (see zipstream documentation)
References zip_stream_p_.
|
inlineprivate |
Uncompressed data size.
References zip_stream_p_.
|
inlineprivate |
Compressed data size.
References zip_stream_p_.
|
inlinevirtual |
Good?
Implements utility::io::orstream.
References stream().
Referenced by open_append_if_existed().
|
inlinevirtual |
|
inlineprivate |
Is stream attached to a gzip file?
References zip_stream_p_.
|
inlinestatic |
References bMPI_reroute_stream_, and mpi_FileBuf_master_rank_.
void utility::io::ozstream::open | ( | std::string const & | filename_a, |
std::ios_base::openmode | open_mode = std::ios_base::out |
||
) |
Open a file.
References allocate_assign_char_buffer(), bMPI_reroute_stream_, buffer_size_, close(), compression_, utility::file::file_extension(), filename_, GZIP, mpi_FileBuf_master_rank_, mpi_stream_p_, NONE, of_stream_, utility::file::trytry_ofstream_open(), UNCOMPRESSED, and zip_stream_p_.
Referenced by open_append_if_existed(), and ozstream().
void utility::io::ozstream::open_append | ( | std::string const & | filename_a | ) |
Open a text file for appending.
Open a text file or gzip'd file for appending.
References allocate_assign_char_buffer(), bMPI_reroute_stream_, buffer_size_, close(), compression_, utility::io::oc::cout, utility::options::OptionKeys::options::exit, utility::file::file_extension(), filename_, GZIP, mpi_FileBuf_master_rank_, mpi_stream_p_, NONE, of_stream_, utility::file::trytry_ofstream_open(), UNCOMPRESSED, and zip_stream_p_.
Referenced by open_append_if_existed().
void utility::io::ozstream::open_append_if_existed | ( | std::string const & | filename_a, |
std::stringstream & | preprinted_header | ||
) |
open file as append if it exists, return true if file existed before, false if it is new.
special "atomic" method to open a stream and print a header if it is new. This is necessary to avoid process competition when checking for existance to decided whether or not to write a header.
References bMPI_reroute_stream_, close(), compression_, utility::file::file_exists(), filename_, good(), mpi_FileBuf_master_rank_, mpi_stream_p_, NONE, open(), open_append(), and UNCOMPRESSED.
|
inlinevirtual |
bool conversion
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Stream conversion.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Stream conversion.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Stream access.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Stream access.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inline |
Stream output: override to preserve type of return value.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Stream output overload to replace std::endl with
to avoid flushing.
and call ozstream::flush() when passed std::flush
Implements utility::io::orstream.
References flush(), mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
Write a char.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, put(), utility::io::mpi_stream::basic_mpi_ostream< Elem, Tr, ElemA, ByteT, ByteAT >::put(), and zip_stream_p_.
Referenced by put().
|
inlinevirtual |
|
inlinevirtual |
Stream access.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
Referenced by bad(), eof(), fail(), good(), rdbuf(), and write().
|
inlinevirtual |
Stream access.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, and zip_stream_p_.
|
inlinevirtual |
|
inlinevirtual |
Write a string.
Implements utility::io::orstream.
References mpi_stream_p_, of_stream_, write(), utility::io::mpi_stream::basic_mpi_ostream< Elem, Tr, ElemA, ByteT, ByteAT >::write(), and zip_stream_p_.
Referenced by write().
|
inlinevirtual |
|
inline |
|
inline |
Flush and finalize the zip_stream.
Calling this will complete the zip stream. Upon the next write, a new zip stream will be started Please note that calling zflush_finalize() too often can seriously degrade compression. zlib documentation appears to imply that every 1MB or so is a reasonable rule of thumb.
References zip_stream_p_.
|
friend |
|
friend |
|
staticprivate |
Referenced by MPI_reroute_rank(), open(), open_append(), and open_append_if_existed().
|
private |
size of buffer (in bytes)
In uncompressed mode this is the size of the character buffer. In compressed mode this is the size of the zip buffer. Must be at least 4KB otherwise zipstream will break.
Referenced by allocate_assign_char_buffer(), buffer_size(), open(), and open_append().
|
private |
character buffer pointer (owning)
Referenced by allocate_assign_char_buffer(), and destroy_char_buffer().
|
private |
Compression state.
Referenced by close(), compressed(), gzipped(), open(), open_append(), open_append_if_existed(), and uncompressed().
|
private |
File name.
Referenced by close(), filename(), open(), open_append(), and open_append_if_existed().
|
staticprivate |
Referenced by MPI_reroute_rank(), open(), open_append(), and open_append_if_existed().
|
private |
|
private |
File stream.
Referenced by allocate_assign_char_buffer(), buffer_size(), clear(), close(), destroy_char_buffer(), flush(), flush_finalize(), open(), open_append(), operator bool(), operator std::ostream &(), operator std::ostream const &(), operator()(), operator<<(), put(), stream(), and write().
|
private |
Zip file stream pointer (owning)
Referenced by clear(), close(), flush(), flush_finalize(), get_crc(), get_in_size(), get_out_size(), is_gzip(), open(), open_append(), operator bool(), operator std::ostream &(), operator std::ostream const &(), operator()(), operator<<(), put(), stream(), write(), zflush(), and zflush_finalize().