]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/zlibcompressed.cpp
Remove unnecessary std:: qualifiers
[libs/core.git] / source / io / zlibcompressed.cpp
index ea5e8030142f5fcfb90b72171b268ed1cdba3b7b..2220bcc46f063372698915a95e1ad1a45d1f74f0 100644 (file)
@@ -8,7 +8,7 @@ using namespace std;
 namespace Msp {
 namespace IO {
 
-zlib_error::zlib_error(const std::string &w, int c):
+zlib_error::zlib_error(const string &w, int c):
 #ifdef WITH_ZLIB
        runtime_error(w+": "+zError(c)),
 #else
@@ -38,17 +38,32 @@ ZlibCompressed::Private::Private()
 
 
 ZlibCompressed::ZlibCompressed(Base &b, unsigned level):
-       below(b),
-       buffer_size(1024),
-       in_buffer(0),
-       out_buffer(0),
-       priv(0)
+       below(b)
 {
-#ifdef WITH_ZLIB
        mode = below.get_mode()&M_RDWR;
        if(mode!=M_READ && mode!=M_WRITE)
                throw invalid_access(mode);
 
+       init(level);
+}
+
+ZlibCompressed::ZlibCompressed(Base &b, Mode m, unsigned level):
+       below(b)
+{
+       mode = m&below.get_mode()&M_RDWR;
+       if(mode!=M_READ && mode!=M_WRITE)
+               throw invalid_access(m);
+
+       init(level);
+}
+
+void ZlibCompressed::init(unsigned level)
+{
+#ifdef WITH_ZLIB
+       buffer_size = 1024;
+       in_buffer = 0;
+       out_buffer = 0;
+       stream_end = false;
        priv = new Private;
 
        if(mode==M_WRITE)
@@ -74,6 +89,8 @@ ZlibCompressed::ZlibCompressed(Base &b, unsigned level):
 
        below.signal_flush_required.connect(sigc::mem_fun(this, &ZlibCompressed::flush));
 #else
+       (void)buffer_size;
+       (void)stream_end;
        (void)level;
        throw zlib_error("unsupported", -1);
 #endif
@@ -96,6 +113,16 @@ ZlibCompressed::~ZlibCompressed()
        delete priv;
 }
 
+void ZlibCompressed::set_block(bool)
+{
+       throw logic_error("ZlibCompressed::set_block");
+}
+
+void ZlibCompressed::set_inherit(bool)
+{
+       throw logic_error("ZlibCompressed::set_inherit");
+}
+
 void ZlibCompressed::flush()
 {
 #ifdef WITH_ZLIB
@@ -114,15 +141,15 @@ void ZlibCompressed::flush()
 #endif
 }
 
-unsigned ZlibCompressed::do_write(const char *data, unsigned size)
+size_t ZlibCompressed::do_write(const char *data, size_t size)
 {
        check_access(M_WRITE);
 
-       unsigned processed = 0;
+       size_t processed = 0;
 #ifdef WITH_ZLIB
        while(processed<size)
        {
-               unsigned free_in = (in_buffer+buffer_size-priv->stream.next_in);
+               size_t free_in = (in_buffer+buffer_size-priv->stream.next_in);
                if(free_in<size && priv->stream.next_in>in_buffer)
                {
                        // Not all of the data fits in the buffer, so make some more room
@@ -134,7 +161,7 @@ unsigned ZlibCompressed::do_write(const char *data, unsigned size)
                if(free_in)
                {
                        // Copy as much data into the input buffer as possible
-                       unsigned len = min(free_in, size-processed);
+                       size_t len = min(free_in, size-processed);
                        copy(data+processed, data+processed+len, priv->stream.next_in+priv->stream.avail_in);
                        priv->stream.avail_in += len;
                        processed += len;
@@ -169,12 +196,12 @@ bool ZlibCompressed::compress_data(int flush_mode)
        }
 
        // Write compressed data into the underlying object
-       unsigned len = 0;
+       size_t len = 0;
        if(priv->stream.next_out>out_buffer)
                len = below.write(reinterpret_cast<char *>(out_buffer), priv->stream.next_out-out_buffer);
        if(len>0)
        {
-               if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
+               if(len<static_cast<size_t>(priv->stream.next_out-out_buffer))
                        copy(out_buffer+len, priv->stream.next_out, out_buffer);
                priv->stream.avail_out += len;
                priv->stream.next_out -= len;
@@ -190,23 +217,23 @@ bool ZlibCompressed::compress_data(int flush_mode)
 #endif
 }
 
-unsigned ZlibCompressed::do_read(char *data, unsigned size)
+size_t ZlibCompressed::do_read(char *data, size_t size)
 {
        check_access(M_READ);
 
-       unsigned processed = 0;
+       size_t processed = 0;
 #ifdef WITH_ZLIB
        while(processed<size)
        {
                if(priv->stream.next_out>out_buffer)
                {
                        // We have some pending output, give it out first
-                       unsigned len = min<unsigned>(priv->stream.next_out-out_buffer, size-processed);
+                       size_t len = min<size_t>(priv->stream.next_out-out_buffer, size-processed);
 
                        copy(out_buffer, out_buffer+len, data+processed);
                        processed += len;
 
-                       if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
+                       if(len<static_cast<size_t>(priv->stream.next_out-out_buffer))
                                copy(out_buffer+len, priv->stream.next_out, out_buffer);
                        priv->stream.next_out -= len;
                        priv->stream.avail_out += len;
@@ -219,7 +246,7 @@ unsigned ZlibCompressed::do_read(char *data, unsigned size)
                {
                        int ret = inflate(&priv->stream, Z_NO_FLUSH);
                        if(ret==Z_STREAM_END)
-                               set_eof();
+                               stream_end = true;
                        else if(ret!=Z_OK)
                                throw zlib_error("inflate", ret);
                        need_more_input = (priv->stream.next_out==out_buffer);
@@ -227,19 +254,22 @@ unsigned ZlibCompressed::do_read(char *data, unsigned size)
 
                if(need_more_input)
                {
-                       if(eof_flag)
+                       if(stream_end)
                                break;
 
                        if(priv->stream.next_in>in_buffer)
                                copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
                        priv->stream.next_in = in_buffer;
 
-                       unsigned len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
+                       size_t len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
                        priv->stream.avail_in += len;
                        if(!len && below.eof())
-                               set_eof();
+                               stream_end = true;
                }
        }
+
+       if(size>0 && processed==0 && stream_end)
+               set_eof();
 #else
        (void)data;
        (void)size;
@@ -248,5 +278,10 @@ unsigned ZlibCompressed::do_read(char *data, unsigned size)
        return processed;
 }
 
+const Handle &ZlibCompressed::get_handle(Mode)
+{
+       throw logic_error("ZlibCompressed::get_handle");
+}
+
 } // namespace IO
 } // namespace Msp