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

View File

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

View File

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