2014-06-02 21:28:02 +04:00
|
|
|
#ifndef UTIL_READ_COMPRESSED_H
|
|
|
|
#define UTIL_READ_COMPRESSED_H
|
2012-10-30 23:33:22 +04:00
|
|
|
|
|
|
|
#include "util/exception.hh"
|
|
|
|
#include "util/scoped.hh"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
class CompressedException : public Exception {
|
|
|
|
public:
|
|
|
|
CompressedException() throw();
|
|
|
|
virtual ~CompressedException() throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
class GZException : public CompressedException {
|
|
|
|
public:
|
|
|
|
GZException() throw();
|
|
|
|
~GZException() throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
class BZException : public CompressedException {
|
|
|
|
public:
|
|
|
|
BZException() throw();
|
|
|
|
~BZException() throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
class XZException : public CompressedException {
|
|
|
|
public:
|
|
|
|
XZException() throw();
|
|
|
|
~XZException() throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadBase;
|
|
|
|
|
|
|
|
class ReadCompressed {
|
|
|
|
public:
|
|
|
|
static const std::size_t kMagicSize = 6;
|
2015-04-30 08:05:11 +03:00
|
|
|
// Must have at least kMagicSize bytes.
|
2012-10-30 23:33:22 +04:00
|
|
|
static bool DetectCompressedMagic(const void *from);
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
// Takes ownership of fd.
|
2012-10-30 23:33:22 +04:00
|
|
|
explicit ReadCompressed(int fd);
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
// Try to avoid using this. Use the fd instead.
|
|
|
|
// There is no decompression support for istreams.
|
|
|
|
explicit ReadCompressed(std::istream &in);
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
// Must call Reset later.
|
|
|
|
ReadCompressed();
|
|
|
|
|
|
|
|
~ReadCompressed();
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
// Takes ownership of fd.
|
2012-10-30 23:33:22 +04:00
|
|
|
void Reset(int fd);
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
// Same advice as the constructor.
|
|
|
|
void Reset(std::istream &in);
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
std::size_t Read(void *to, std::size_t amount);
|
|
|
|
|
2014-07-19 02:54:01 +04:00
|
|
|
// Repeatedly call read to fill a buffer unless EOF is hit.
|
|
|
|
// Return number of bytes read.
|
|
|
|
std::size_t ReadOrEOF(void *const to, std::size_t amount);
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
uint64_t RawAmount() const { return raw_amount_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class ReadBase;
|
|
|
|
|
|
|
|
scoped_ptr<ReadBase> internal_;
|
|
|
|
|
|
|
|
uint64_t raw_amount_;
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
// No copying.
|
2012-10-30 23:33:22 +04:00
|
|
|
ReadCompressed(const ReadCompressed &);
|
|
|
|
void operator=(const ReadCompressed &);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
#endif // UTIL_READ_COMPRESSED_H
|