diff options
Diffstat (limited to 'lib/Handle.cc')
-rw-r--r-- | lib/Handle.cc | 91 |
1 files changed, 78 insertions, 13 deletions
diff --git a/lib/Handle.cc b/lib/Handle.cc index da29a51..0470929 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -3,14 +3,23 @@ #include <string.h> #include <errno.h> #include <fcntl.h> -#include "Handle.h" +#ifdef HAVE_CONFIG_H #include "config.h" +#endif +#include "Handle.h" +#include "gettext.h" -Handle::Handle(const Handle & nh) : h(nh.h >= 0 ? dup(nh.h) : nh.h), closed(nh.closed), nonblock(nh.closed), zfile(0), z(0) { +Handle::Handle(const Handle & nh) : itell(0), h(nh.h >= 0 ? dup(nh.h) : nh.h), closed(nh.closed), nonblock(nh.closed) +#ifdef HAVE_ZLIB +, zfile(0), z(0) +#endif +{ // cerr << "Duplication of handle " << nh.h << " to " << h << endl; +#ifdef HAVE_ZLIB if ((h >= 0) && (nh.z)) { SetZ(nh.z); } +#endif } Handle::~Handle() { @@ -18,7 +27,11 @@ Handle::~Handle() { close(); } -Handle::Handle(int nh) : h(nh), closed(false), nonblock(false), zfile(0), z(0) { +Handle::Handle(int nh) : h(nh), closed(false), nonblock(false) +#ifdef HAVE_ZLIB +, zfile(0), z(0) +#endif +{ // cerr << "Initialising handle " << h << endl; } @@ -26,6 +39,10 @@ int Handle::GetHandle() { return h; } +int Handle::GetHandle() const { + return h; +} + ssize_t Handle::write(const void *buf, size_t count) throw (GeneralException) { ssize_t r, tr = 0; bool done, full = false; @@ -92,11 +109,11 @@ ssize_t Handle::read(void *buf, size_t count) throw (GeneralException) { return r; } -bool Handle::IsClosed(void) { +bool Handle::IsClosed(void) const { return closed; } -bool Handle::IsNonBlock(void) { +bool Handle::IsNonBlock(void) const { return nonblock; } @@ -145,6 +162,7 @@ void Handle::close() throw (GeneralException) { } if (h >= 0) { +#ifdef HAVE_ZLIB if (z) { // cerr << "Performing gzclose on handle " << h << endl; int err = gzclose(zfile); @@ -157,6 +175,9 @@ void Handle::close() throw (GeneralException) { } } } else { +#else + { +#endif int err = ::close(h); if (err) { throw GeneralException(String(_("Error during close: ")) + strerror(errno)); @@ -169,27 +190,27 @@ void Handle::close() throw (GeneralException) { closed = 1; } -bool Handle::CanRead(void) { +bool Handle::CanRead(void) const { return false; } -bool Handle::CanWrite(void) { +bool Handle::CanWrite(void) const { return false; } -String Handle::GetName(void) { +String Handle::GetName(void) const { return _("Bare Handle - should not happend"); } -ssize_t Handle::GetSize(void) { +ssize_t Handle::GetSize(void) const { return -1; } -time_t Handle::GetModif(void) { +time_t Handle::GetModif(void) const { return -1; } -bool Handle::CanWatch(void) { +bool Handle::CanWatch(void) const { return true; } @@ -200,6 +221,7 @@ void Handle::Dup(const Handle & H) { } } +#ifdef HAVE_ZLIB void Handle::SetZ(int az) throw (GeneralException) { if (z) { throw GeneralException(_("Can't SetZ a Handle twice.")); @@ -225,11 +247,18 @@ void Handle::SetZ(int az) throw (GeneralException) { z = az; } } +#endif ssize_t Handle::uwrite(const void * buf, size_t count) throw (GeneralException) { +#ifdef HAVE_ZLIB if (z) { + itell += count; // cerr << "Performing gzwrite of " << count << " byte for handle " << h << endl; +#ifdef HAVE_CLEAN_ZLIB int err = gzwrite(zfile, buf, count); +#else + int err = gzwrite(zfile, (char *) buf, count); +#endif // cerr << "gzwrite returned " << err << endl; if (err == 0) { const char * m = gzerror(zfile, &err); @@ -241,12 +270,18 @@ ssize_t Handle::uwrite(const void * buf, size_t count) throw (GeneralException) } return err; } else { - return ::write(h, buf, count); +#else + { +#endif + itell += count = ::write(h, buf, count); + return count; } } ssize_t Handle::uread(void * buf, size_t count) { +#ifdef HAVE_ZLIB if (z) { + itell += count; // cerr << "Performing gzread of " << count << " byte for handle " << h << endl; int err = gzread(zfile, buf, count); // cerr << "gzwrite returned " << err << endl; @@ -260,6 +295,36 @@ ssize_t Handle::uread(void * buf, size_t count) { } return err; } else { - return ::read(h, buf, count); +#else + { +#endif + itell += count = ::read(h, buf, count); + return count; + } +} + +off_t Handle::tell() const { + return itell; +} + +bool Handle::CanSeek() const { + return 0; +} + +off_t Handle::seek(off_t, int) throw(GeneralException) { + throw IOGeneral("Handle " + GetName() + " can't seek"); +} + +void copy(Handle * s, Handle * d, ssize_t size) { + long i; + unsigned char c; + long r; + + for (i = 0; (i < size) || (size < 0); i++) { + r = s->read(&c, 1); + if (r == 0) { + break; + } + d->write(&c, 1); } } |