LCOV - code coverage report
Current view: top level - src - gzipoutputstreambuf.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 65 1.5 %
Date: 2015-05-05 Functions: 2 14 14.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Zipios++ - a small C++ library that provides easy access to .zip files.
       3             : 
       4             :   Copyright (C) 2000-2007  Thomas Sondergaard
       5             :   Copyright (C) 2015  Made to Order Software Corporation
       6             : 
       7             :   This library is free software; you can redistribute it and/or
       8             :   modify it under the terms of the GNU Lesser General Public
       9             :   License as published by the Free Software Foundation; either
      10             :   version 2 of the License, or (at your option) any later version.
      11             : 
      12             :   This library is distributed in the hope that it will be useful,
      13             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :   Lesser General Public License for more details.
      16             : 
      17             :   You should have received a copy of the GNU Lesser General Public
      18             :   License along with this library; if not, write to the Free Software
      19             :   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
      20             : */
      21             : 
      22             : /** \file
      23             :  * \brief This file is the implementation of zipios::GZIPOutputStreambuf class.
      24             :  *
      25             :  * This class is an output stream filter which knows how to creates a .gz
      26             :  * file from the data you write to it.
      27             :  *
      28             :  * The compression makes use of the zlib library.
      29             :  */
      30             : 
      31             : #include "gzipoutputstreambuf.hpp"
      32             : 
      33             : #include "zipios/zipiosexceptions.hpp"
      34             : 
      35             : 
      36             : namespace zipios
      37             : {
      38             : 
      39             : /** \class GZIPOutputStreambuf
      40             :  * \brief Save the output stream buffer.
      41             :  *
      42             :  * This class is used to output the data of a file in a gzip stream
      43             :  * including the necessary header and footer.
      44             :  */
      45             : 
      46             : 
      47             : /** \brief Initialize a GZIPOutputStreambuf object.
      48             :  *
      49             :  * A newly constructed GZIPOutputStreambuf is ready to accept data.
      50             :  *
      51             :  * \param[in,out] outbuf  The streambuf to use for output.
      52             :  * \param[in] compression_level  The compression level to use to compress.
      53             :  */
      54           0 : GZIPOutputStreambuf::GZIPOutputStreambuf(std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
      55           0 :     : DeflateOutputStreambuf(outbuf)
      56             :     //, m_open(false) -- auto-init
      57             : {
      58           0 :     if(!init(compression_level))
      59             :     {
      60           0 :         throw InvalidStateException("GZIPOutputStreambuf::GZIPOutputStreambuf() failed initializing zlib.");
      61             :     }
      62           0 : }
      63             : 
      64             : 
      65             : /** \brief Ensures that the stream gets closed properly.
      66             :  *
      67             :  * This function makes sure that the stream gets closed properly
      68             :  * which means that the compress terminates by calling finish()
      69             :  * and the streams get closed.
      70             :  */
      71           0 : GZIPOutputStreambuf::~GZIPOutputStreambuf()
      72             : {
      73           0 :     finish();
      74           0 : }
      75             : 
      76             : 
      77           0 : void GZIPOutputStreambuf::setFilename(std::string const& filename)
      78             : {
      79           0 :     m_filename = filename;
      80           0 : }
      81             : 
      82             : 
      83           0 : void GZIPOutputStreambuf::setComment(std::string const& comment)
      84             : {
      85           0 :     m_comment = comment;
      86           0 : }
      87             : 
      88             : 
      89             : /** \brief Close the stream.
      90             :  *
      91             :  * This function ensures that the streams get closed.
      92             :  */
      93           0 : void GZIPOutputStreambuf::close()
      94             : {
      95           0 :     finish();
      96           0 : }
      97             : 
      98             : 
      99             : /** \brief Finishes the compression.
     100             :  *
     101             :  * Write whatever is still necessary and close the streams.
     102             :  */
     103           0 : void GZIPOutputStreambuf::finish()
     104             : {
     105           0 :     if(!m_open)
     106             :     {
     107           0 :         return;
     108             :     }
     109           0 :     m_open = false;
     110             : 
     111           0 :     closeStream();
     112           0 :     writeTrailer();
     113             : }
     114             : 
     115             : 
     116           0 : int GZIPOutputStreambuf::overflow(int c)
     117             : {
     118           0 :     if(!m_open)
     119             :     {
     120           0 :         writeHeader();
     121           0 :         m_open = true;
     122             :     }
     123             : 
     124           0 :     return DeflateOutputStreambuf::overflow(c);
     125             : }
     126             : 
     127             : 
     128           0 : int GZIPOutputStreambuf::sync()
     129             : {
     130           0 :     return DeflateOutputStreambuf::sync();
     131             : }
     132             : 
     133             : 
     134           0 : void GZIPOutputStreambuf::writeHeader()
     135             : {
     136             :     unsigned char const flg(
     137           0 :                   (m_filename.empty() ? 0x00 : 0x08)
     138           0 :                 | (m_comment.empty()  ? 0x00 : 0x10)
     139           0 :             );
     140             : 
     141             :     /** \TODO:
     142             :      * We need to know of the last modification time instead of
     143             :      * saving all zeros for MTIME values.
     144             :      *
     145             :      * Also, I am thinking that the OS should be 3 under Unices.
     146             :      */
     147             : 
     148           0 :     std::ostream os(m_outbuf) ;
     149           0 :     os << static_cast<unsigned char>(0x1f);  // Magic #
     150           0 :     os << static_cast<unsigned char>(0x8b);  // Magic #
     151           0 :     os << static_cast<unsigned char>(0x08);  // Deflater.DEFLATED
     152           0 :     os << flg;                               // FLG
     153           0 :     os << static_cast<unsigned char>(0x00);  // MTIME
     154           0 :     os << static_cast<unsigned char>(0x00);  // MTIME
     155           0 :     os << static_cast<unsigned char>(0x00);  // MTIME
     156           0 :     os << static_cast<unsigned char>(0x00);  // MTIME
     157           0 :     os << static_cast<unsigned char>(0x00);  // XFLG
     158           0 :     os << static_cast<unsigned char>(0x00);  // OS
     159             : 
     160           0 :     if(!m_filename.empty())
     161             :     {
     162           0 :         os << m_filename.c_str();             // Filename
     163           0 :         os << static_cast<unsigned char>(0x00);
     164             :     }
     165             : 
     166           0 :     if(!m_comment.empty())
     167             :     {
     168           0 :         os << m_comment.c_str();              // Comment
     169           0 :         os << static_cast<unsigned char>(0x00);
     170           0 :     }
     171           0 : }
     172             : 
     173             : 
     174           0 : void GZIPOutputStreambuf::writeTrailer()
     175             : {
     176             :     // write the CRC32 and Size at the end of the file
     177           0 :     writeInt(getCrc32());
     178           0 :     writeInt(getSize());
     179           0 : }
     180             : 
     181             : 
     182           0 : void GZIPOutputStreambuf::writeInt(uint32_t i)
     183             : {
     184             :     /** \TODO: add support for 64 bit files if it exists? */
     185           0 :     std::ostream os(m_outbuf);
     186           0 :     os << static_cast<unsigned char>( i        & 0xFF);
     187           0 :     os << static_cast<unsigned char>((i >>  8) & 0xFF);
     188           0 :     os << static_cast<unsigned char>((i >> 16) & 0xFF);
     189           0 :     os << static_cast<unsigned char>((i >> 24) & 0xFF);
     190           0 : }
     191             : 
     192             : 
     193           3 : } // zipios namespace
     194             : 
     195             : // Local Variables:
     196             : // mode: cpp
     197             : // indent-tabs-mode: nil
     198             : // c-basic-offset: 4
     199             : // tab-width: 4
     200             : // End:
     201             : 
     202             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10