Removed dependency of tlStream on zlib by using a delegate.

This commit is contained in:
Matthias Köfferlein 2018-09-09 16:49:21 +02:00
parent 8c2c888263
commit 4821b12780
3 changed files with 84 additions and 26 deletions

View File

@ -29,6 +29,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <zlib.h>
#ifdef _WIN32 #ifdef _WIN32
# include <io.h> # include <io.h>
#endif #endif
@ -121,6 +122,15 @@ public:
{ } { }
}; };
// ---------------------------------------------------------------
class ZLibFilePrivate
{
public:
ZLibFilePrivate () : zs (NULL) { }
gzFile zs;
};
// --------------------------------------------------------------- // ---------------------------------------------------------------
// InputStream implementation // InputStream implementation
@ -532,7 +542,7 @@ InputFile::filename () const
// InputZLibFile implementation // InputZLibFile implementation
InputZLibFile::InputZLibFile (const std::string &path) InputZLibFile::InputZLibFile (const std::string &path)
: m_zs (NULL) : mp_d (new ZLibFilePrivate ())
{ {
m_source = path; m_source = path;
#if defined(_WIN32) #if defined(_WIN32)
@ -540,11 +550,11 @@ InputZLibFile::InputZLibFile (const std::string &path)
if (fd < 0) { if (fd < 0) {
throw FileOpenErrorException (m_source, errno); throw FileOpenErrorException (m_source, errno);
} }
m_zs = gzdopen (fd, "rb"); mp_d->zs = gzdopen (fd, "rb");
#else #else
m_zs = gzopen (tl::string_to_system (path).c_str (), "rb"); m_zs = gzopen (tl::string_to_system (path).c_str (), "rb");
#endif #endif
if (m_zs == NULL) { if (mp_d->zs == NULL) {
throw FileOpenErrorException (m_source, errno); throw FileOpenErrorException (m_source, errno);
} }
} }
@ -552,25 +562,27 @@ InputZLibFile::InputZLibFile (const std::string &path)
InputZLibFile::~InputZLibFile () InputZLibFile::~InputZLibFile ()
{ {
close (); close ();
delete mp_d;
mp_d = 0;
} }
void void
InputZLibFile::close () InputZLibFile::close ()
{ {
if (m_zs != NULL) { if (mp_d->zs != NULL) {
gzclose (m_zs); gzclose (mp_d->zs);
m_zs = NULL; mp_d->zs = NULL;
} }
} }
size_t size_t
InputZLibFile::read (char *b, size_t n) InputZLibFile::read (char *b, size_t n)
{ {
tl_assert (m_zs != NULL); tl_assert (mp_d->zs != NULL);
int ret = gzread (m_zs, b, (unsigned int) n); int ret = gzread (mp_d->zs, b, (unsigned int) n);
if (ret < 0) { if (ret < 0) {
int gz_err = 0; int gz_err = 0;
const char *em = gzerror (m_zs, &gz_err); const char *em = gzerror (mp_d->zs, &gz_err);
if (gz_err == Z_ERRNO) { if (gz_err == Z_ERRNO) {
throw FileReadErrorException (m_source, errno); throw FileReadErrorException (m_source, errno);
} else { } else {
@ -584,8 +596,8 @@ InputZLibFile::read (char *b, size_t n)
void void
InputZLibFile::reset () InputZLibFile::reset ()
{ {
if (m_zs != NULL) { if (mp_d->zs != NULL) {
gzrewind (m_zs); gzrewind (mp_d->zs);
} }
} }
@ -804,7 +816,7 @@ OutputFile::write (const char *b, size_t n)
// OutputZLibFile implementation // OutputZLibFile implementation
OutputZLibFile::OutputZLibFile (const std::string &path) OutputZLibFile::OutputZLibFile (const std::string &path)
: m_zs (NULL) : mp_d (new ZLibFilePrivate ())
{ {
m_source = path; m_source = path;
#if defined(_WIN32) #if defined(_WIN32)
@ -812,31 +824,33 @@ OutputZLibFile::OutputZLibFile (const std::string &path)
if (file == NULL) { if (file == NULL) {
throw FileOpenErrorException (m_source, errno); throw FileOpenErrorException (m_source, errno);
} }
m_zs = gzdopen (_fileno (file), "wb"); mp_d->zs = gzdopen (_fileno (file), "wb");
#else #else
m_zs = gzopen (tl::string_to_system (path).c_str (), "wb"); m_zs = gzopen (tl::string_to_system (path).c_str (), "wb");
#endif #endif
if (m_zs == NULL) { if (mp_d->zs == NULL) {
throw FileOpenErrorException (m_source, errno); throw FileOpenErrorException (m_source, errno);
} }
} }
OutputZLibFile::~OutputZLibFile () OutputZLibFile::~OutputZLibFile ()
{ {
if (m_zs != NULL) { if (mp_d->zs != NULL) {
gzclose (m_zs); gzclose (mp_d->zs);
m_zs = NULL; mp_d->zs = NULL;
} }
delete mp_d;
mp_d = 0;
} }
void void
OutputZLibFile::write (const char *b, size_t n) OutputZLibFile::write (const char *b, size_t n)
{ {
tl_assert (m_zs != NULL); tl_assert (mp_d->zs != NULL);
int ret = gzwrite (m_zs, (char *) b, (unsigned int) n); int ret = gzwrite (mp_d->zs, (char *) b, (unsigned int) n);
if (ret < 0) { if (ret < 0) {
int gz_err = 0; int gz_err = 0;
const char *em = gzerror (m_zs, &gz_err); const char *em = gzerror (mp_d->zs, &gz_err);
if (gz_err == Z_ERRNO) { if (gz_err == Z_ERRNO) {
throw FileWriteErrorException (m_source, errno); throw FileWriteErrorException (m_source, errno);
} else { } else {

View File

@ -33,7 +33,6 @@
#include <sstream> #include <sstream>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <zlib.h>
namespace tl namespace tl
@ -104,6 +103,10 @@ public:
virtual std::string filename () const = 0; virtual std::string filename () const = 0;
}; };
// ---------------------------------------------------------------------------------
class ZLibFilePrivate;
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
// InputStreamBase implementations // InputStreamBase implementations
@ -162,6 +165,10 @@ public:
} }
private: private:
// no copying
InputMemoryStream (const InputMemoryStream &);
InputMemoryStream &operator= (const InputMemoryStream &);
const char *mp_data; const char *mp_data;
size_t m_length, m_pos; size_t m_length, m_pos;
}; };
@ -215,8 +222,12 @@ public:
virtual std::string filename () const; virtual std::string filename () const;
private: private:
// no copying
InputZLibFile (const InputZLibFile &);
InputZLibFile &operator= (const InputZLibFile &);
std::string m_source; std::string m_source;
gzFile m_zs; ZLibFilePrivate *mp_d;
}; };
/** /**
@ -263,6 +274,10 @@ public:
virtual std::string filename () const; virtual std::string filename () const;
private: private:
// no copying
InputFile (const InputFile &d);
InputFile &operator= (const InputFile &d);
std::string m_source; std::string m_source;
int m_fd; int m_fd;
}; };
@ -338,6 +353,10 @@ public:
} }
private: private:
// No copying
InputPipe (const InputPipe &);
InputPipe &operator= (const InputPipe &);
FILE *m_file; FILE *m_file;
std::string m_source; std::string m_source;
}; };
@ -665,6 +684,11 @@ public:
{ {
return false; return false;
} }
private:
// No copying
OutputStreamBase (const OutputStreamBase &);
OutputStreamBase &operator= (const OutputStreamBase &);
}; };
/** /**
@ -719,6 +743,10 @@ public:
} }
private: private:
// No copying
OutputMemoryStream (const OutputMemoryStream &);
OutputMemoryStream &operator= (const OutputMemoryStream &);
std::vector<char> m_buffer; std::vector<char> m_buffer;
}; };
@ -778,6 +806,10 @@ public:
} }
private: private:
// No copying
OutputStringStream (const OutputStringStream &);
OutputStringStream &operator= (const OutputStringStream &);
std::ostringstream m_stream; std::ostringstream m_stream;
}; };
@ -817,8 +849,12 @@ public:
virtual void write (const char *b, size_t n); virtual void write (const char *b, size_t n);
private: private:
// No copying
OutputZLibFile (const OutputZLibFile &);
OutputZLibFile &operator= (const OutputZLibFile &);
std::string m_source; std::string m_source;
gzFile m_zs; ZLibFilePrivate *mp_d;
}; };
/** /**
@ -873,6 +909,10 @@ public:
virtual void write (const char *b, size_t n); virtual void write (const char *b, size_t n);
private: private:
// No copying
OutputFile (const OutputFile &);
OutputFile &operator= (const OutputFile &);
std::string m_source; std::string m_source;
int m_fd; int m_fd;
}; };
@ -915,6 +955,10 @@ public:
virtual void write (const char *b, size_t n); virtual void write (const char *b, size_t n);
private: private:
// No copying
OutputPipe (const OutputPipe &);
OutputPipe &operator= (const OutputPipe &);
FILE *m_file; FILE *m_file;
std::string m_source; std::string m_source;
}; };

View File

@ -44,7 +44,7 @@ TEST(1)
0x00, 0x00 0x00, 0x00
}; };
tl::InputMemoryStream ims = tl::InputMemoryStream ((const char *) data, sizeof (data)); tl::InputMemoryStream ims ((const char *) data, sizeof (data));
tl::InputStream is (ims); tl::InputStream is (ims);
std::string out; std::string out;
@ -69,7 +69,7 @@ TEST(2)
std::string deflated = oss.string (); std::string deflated = oss.string ();
for (size_t i = 0; i < deflated.size(); ++i) { for (size_t i = 0; i < deflated.size(); ++i) {
} }
tl::InputMemoryStream ims = tl::InputMemoryStream ((const char *) deflated.c_str (), deflated.size ()); tl::InputMemoryStream ims ((const char *) deflated.c_str (), deflated.size ());
tl::InputStream is (ims); tl::InputStream is (ims);
std::string out; std::string out;
@ -104,7 +104,7 @@ TEST(3)
EXPECT_EQ (deflated.size () < 300000 && deflated.size () > 200000, true); EXPECT_EQ (deflated.size () < 300000 && deflated.size () > 200000, true);
EXPECT_EQ (deflated.size (), fg.compressed ()); EXPECT_EQ (deflated.size (), fg.compressed ());
EXPECT_EQ (n_hello, fg.uncompressed ()); EXPECT_EQ (n_hello, fg.uncompressed ());
tl::InputMemoryStream ims = tl::InputMemoryStream ((const char *) deflated.c_str (), deflated.size ()); tl::InputMemoryStream ims ((const char *) deflated.c_str (), deflated.size ());
tl::InputStream is (ims); tl::InputStream is (ims);
std::string out; std::string out;