Mercurial > octave
changeset 23098:03f817ed37c5
style fixes
* gzip.cc: Use m_ prefix for class data members. Declare unused
constructors and assignment operators as deleted. Declare default
destructors. Reorder public/private class sections.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 26 Jan 2017 23:46:47 -0500 |
parents | 0c5f04b9dca1 |
children | d44537a50f4b |
files | libinterp/dldfcn/gzip.cc |
diffstat | 1 files changed, 165 insertions(+), 126 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/dldfcn/gzip.cc Thu Jan 26 23:19:45 2017 -0500 +++ b/libinterp/dldfcn/gzip.cc Thu Jan 26 23:46:47 2017 -0500 @@ -94,102 +94,117 @@ class CFile { public: - std::FILE* fp; + + CFile (void) = delete; CFile (const std::string& path, const std::string& mode) + : m_fp (std::fopen (path.c_str (), mode.c_str ())) { - fp = std::fopen (path.c_str (), mode.c_str ()); - if (! fp) + if (! m_fp) throw std::runtime_error ("unable to open file"); } - void - close (void) + CFile (const CFile&) = delete; + + CFile& operator = (const CFile&) = delete; + + ~CFile (void) { - if (std::fclose (fp)) - throw std::runtime_error ("unable to close file"); - fp = nullptr; + if (m_fp) + std::fclose (m_fp); } - ~CFile () + void close (void) { - if (fp) - std::fclose (fp); + if (std::fclose (m_fp)) + throw std::runtime_error ("unable to close file"); + + m_fp = nullptr; } + + std::FILE* m_fp; }; #if defined (HAVE_BZ2) class bz2 { - private: - class zipper - { - private: - int status = BZ_OK; - CFile source; - CFile dest; - BZFILE* bz; - - public: - zipper (const std::string& source_path, const std::string& dest_path) - : source (source_path, "rb"), dest (dest_path, "wb") - { - bz = BZ2_bzWriteOpen (&status, dest.fp, 9, 0, 30); - if (status != BZ_OK) - throw std::runtime_error ("failed to open bzip2 stream"); - } + public: - void - deflate (void) - { - const std::size_t buf_len = 8192; - char buf[buf_len]; - std::size_t n_read; - while ((n_read = std::fread (buf, sizeof (buf[0]), buf_len, source.fp)) != 0) - { - if (std::ferror (source.fp)) - throw std::runtime_error ("failed to read from source file"); - BZ2_bzWrite (&status, bz, buf, n_read); - if (status == BZ_IO_ERROR) - throw std::runtime_error ("failed to write or compress"); - } - if (std::ferror (source.fp)) - throw std::runtime_error ("failed to read from source file"); - } - - void - close (void) - { - int abandon = (status == BZ_IO_ERROR) ? 1 : 0; - BZ2_bzWriteClose (&status, bz, abandon, 0, 0); - if (status != BZ_OK) - throw std::runtime_error ("failed to close bzip2 stream"); - bz = nullptr; - - // We have no error handling for failing to close source, let - // the destructor close it. - dest.close (); - } - - ~zipper () - { - if (bz != nullptr) - BZ2_bzWriteClose (&status, bz, 1, 0, 0); - } - }; - - public: static const constexpr char* extension = ".bz2"; - static void - zip (const std::string& source_path, const std::string& dest_path) + static void zip (const std::string& source_path, + const std::string& dest_path) { bz2::zipper z (source_path, dest_path); z.deflate (); z.close (); } + private: + + class zipper + { + public: + + zipper (void) = delete; + + zipper (const std::string& source_path, const std::string& dest_path) + : m_status (BZ_OK), m_source (source_path, "rb"), + m_dest (dest_path, "wb"), + m_bz (BZ2_bzWriteOpen (&m_status, m_dest.m_fp, 9, 0, 30)) + { + if (m_status != BZ_OK) + throw std::runtime_error ("failed to open bzip2 stream"); + } + + zipper (const zipper&) = delete; + + zipper& operator = (const zipper&) = delete; + + ~zipper (void) + { + if (m_bz != nullptr) + BZ2_bzWriteClose (&m_status, m_bz, 1, 0, 0); + } + + void deflate (void) + { + const std::size_t buf_len = 8192; + char buf[buf_len]; + std::size_t n_read; + while ((n_read = std::fread (buf, sizeof (buf[0]), buf_len, m_source.m_fp)) != 0) + { + if (std::ferror (m_source.m_fp)) + throw std::runtime_error ("failed to read from source file"); + BZ2_bzWrite (&m_status, m_bz, buf, n_read); + if (m_status == BZ_IO_ERROR) + throw std::runtime_error ("failed to write or compress"); + } + if (std::ferror (m_source.m_fp)) + throw std::runtime_error ("failed to read from source file"); + } + + void close (void) + { + int abandon = (m_status == BZ_IO_ERROR) ? 1 : 0; + BZ2_bzWriteClose (&m_status, m_bz, abandon, 0, 0); + if (m_status != BZ_OK) + throw std::runtime_error ("failed to close bzip2 stream"); + m_bz = nullptr; + + // We have no error handling for failing to close source, let + // the destructor close it. + m_dest.close (); + } + + private: + + int m_status; + CFile m_source; + CFile m_dest; + BZFILE* m_bz; + }; }; #endif @@ -225,33 +240,51 @@ class gz { + public: + + static const constexpr char* extension = ".gz"; + + static void zip (const std::string& source_path, + const std::string& dest_path) + { + gz::zipper z (source_path, dest_path); + z.deflate (); + z.close (); + } + private: // Util class to get a non-const char* class uchar_array { public: + // Bytef is a typedef for unsigned char unsigned char* p; + uchar_array (void) = delete; + uchar_array (const std::string& str) { p = new Bytef[str.length () +1]; std::strcpy (reinterpret_cast<char*> (p), str.c_str ()); } + uchar_array (const uchar_array&) = delete; + + uchar_array& operator = (const uchar_array&) = delete; + ~uchar_array (void) { delete[] p; } }; class gzip_header : public gz_header { - private: - // This must be kept for gz_header.name - uchar_array basename; + public: - public: + gzip_header (void) = delete; + gzip_header (const std::string& source_path) - : basename (octave::sys::env::base_pathname (source_path)) + : m_basename (octave::sys::env::base_pathname (source_path)) { const octave::sys::file_stat source_stat (source_path); if (! source_stat) @@ -271,7 +304,7 @@ // directory components removed, and, if the file being // compressed is on a file system with case insensitive names, // forced to lower case. - name = basename.p; + name = m_basename.p; // If we don't set it to Z_NULL, then it will set FCOMMENT (4th bit) // on the FLG byte, and then write {0, 3} comment. @@ -316,41 +349,59 @@ os = 3; #endif } + + gzip_header (const gzip_header&) = delete; + + gzip_header& operator = (const gzip_header&) = delete; + + ~gzip_header (void) = default; + + private: + + // This must be kept for gz_header.name + uchar_array m_basename; }; class zipper { - private: - CFile source; - CFile dest; - gzip_header header; - z_stream* strm; + public: + + zipper (void) = delete; - public: zipper (const std::string& source_path, const std::string& dest_path) - : source (source_path, "rb"), dest (dest_path, "wb"), - header (source_path), strm (new z_stream) + : m_source (source_path, "rb"), m_dest (dest_path, "wb"), + m_header (source_path), m_strm (new z_stream) { - strm->zalloc = Z_NULL; - strm->zfree = Z_NULL; - strm->opaque = Z_NULL; + m_strm->zalloc = Z_NULL; + m_strm->zfree = Z_NULL; + m_strm->opaque = Z_NULL; } - void - deflate () + zipper (const zipper&) = delete; + + zipper& operator = (const zipper&) = delete; + + ~zipper (void) { - // int deflateInit2 (z_streamp strm, + if (m_strm) + deflateEnd (m_strm); + delete m_strm; + } + + void deflate (void) + { + // int deflateInit2 (z_streamp m_strm, // int level, // compression level (default is 8) // int method, // int windowBits, // 15 (default) + 16 (gzip format) // int memLevel, // memory usage (default is 8) // int strategy); - int status = deflateInit2 (strm, 8, Z_DEFLATED, 31, 8, + int status = deflateInit2 (m_strm, 8, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY); if (status != Z_OK) throw std::runtime_error ("failed to open zlib stream"); - deflateSetHeader (strm, &header); + deflateSetHeader (m_strm, &m_header); const std::size_t buf_len = 8192; unsigned char buf_in[buf_len]; @@ -360,34 +411,34 @@ do { - strm->avail_in = std::fread (buf_in, sizeof (buf_in[0]), - buf_len, source.fp); + m_strm->avail_in = std::fread (buf_in, sizeof (buf_in[0]), + buf_len, m_source.m_fp); - if (std::ferror (source.fp)) + if (std::ferror (m_source.m_fp)) throw std::runtime_error ("failed to read source file"); - strm->next_in = buf_in; - flush = std::feof (source.fp) ? Z_FINISH : Z_NO_FLUSH; + m_strm->next_in = buf_in; + flush = std::feof (m_source.m_fp) ? Z_FINISH : Z_NO_FLUSH; // If deflate returns Z_OK and with zero avail_out, it must be // called again after making room in the output buffer because // there might be more output pending. do { - strm->avail_out = buf_len; - strm->next_out = buf_out; - status = ::deflate (strm, flush); + m_strm->avail_out = buf_len; + m_strm->next_out = buf_out; + status = ::deflate (m_strm, flush); if (status == Z_STREAM_ERROR) throw std::runtime_error ("failed to deflate"); std::fwrite (buf_out, sizeof (buf_out[0]), - buf_len - strm->avail_out, dest.fp); - if (std::ferror (dest.fp)) + buf_len - m_strm->avail_out, m_dest.m_fp); + if (std::ferror (m_dest.m_fp)) throw std::runtime_error ("failed to write file"); } - while (strm->avail_out == 0); + while (m_strm->avail_out == 0); - if (strm->avail_in != 0) + if (m_strm->avail_in != 0) throw std::runtime_error ("failed to write file"); } while (flush != Z_FINISH); @@ -396,36 +447,24 @@ throw std::runtime_error ("failed to write file"); } - void - close (void) + void close (void) { - if (deflateEnd (strm) != Z_OK) + if (deflateEnd (m_strm) != Z_OK) throw std::runtime_error ("failed to close zlib stream"); - strm = nullptr; + m_strm = nullptr; // We have no error handling for failing to close source, let // the destructor close it. - dest.close (); + m_dest.close (); } - ~zipper (void) - { - if (strm) - deflateEnd (strm); - delete strm; - } - }; + private: - public: - static const constexpr char* extension = ".gz"; - - static void - zip (const std::string& source_path, const std::string& dest_path) - { - gz::zipper z (source_path, dest_path); - z.deflate (); - z.close (); - } + CFile m_source; + CFile m_dest; + gzip_header m_header; + z_stream* m_strm; + }; }; #endif