Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
src/kenlm/util/read_compressed.hh
00001 #ifndef UTIL_READ_COMPRESSED_H
00002 #define UTIL_READ_COMPRESSED_H
00003 
00004 #include "util/exception.hh"
00005 #include "util/scoped.hh"
00006 
00007 #include <cstddef>
00008 #include <stdint.h>
00009 
00010 namespace util {
00011 
00012 class CompressedException : public Exception {
00013   public:
00014     CompressedException() throw();
00015     virtual ~CompressedException() throw();
00016 };
00017 
00018 class GZException : public CompressedException {
00019   public:
00020     GZException() throw();
00021     ~GZException() throw();
00022 };
00023 
00024 class BZException : public CompressedException {
00025   public:
00026     BZException() throw();
00027     ~BZException() throw();
00028 };
00029 
00030 class XZException : public CompressedException {
00031   public:
00032     XZException() throw();
00033     ~XZException() throw();
00034 };
00035 
00036 class ReadBase;
00037 
00038 class ReadCompressed {
00039   public:
00040     static const std::size_t kMagicSize = 6;
00041     // Must have at least kMagicSize bytes.
00042     static bool DetectCompressedMagic(const void *from);
00043 
00044     // Takes ownership of fd.
00045     explicit ReadCompressed(int fd);
00046 
00047     // Try to avoid using this.  Use the fd instead.
00048     // There is no decompression support for istreams.
00049     explicit ReadCompressed(std::istream &in);
00050 
00051     // Must call Reset later.
00052     ReadCompressed();
00053 
00054     ~ReadCompressed();
00055 
00056     // Takes ownership of fd.
00057     void Reset(int fd);
00058 
00059     // Same advice as the constructor.
00060     void Reset(std::istream &in);
00061 
00062     std::size_t Read(void *to, std::size_t amount);
00063 
00064     // Repeatedly call read to fill a buffer unless EOF is hit.
00065     // Return number of bytes read.
00066     std::size_t ReadOrEOF(void *const to, std::size_t amount);
00067 
00068     uint64_t RawAmount() const { return raw_amount_; }
00069 
00070   private:
00071     friend class ReadBase;
00072 
00073     scoped_ptr<ReadBase> internal_;
00074 
00075     uint64_t raw_amount_;
00076 
00077     // No copying.
00078     ReadCompressed(const ReadCompressed &);
00079     void operator=(const ReadCompressed &);
00080 };
00081 
00082 } // namespace util
00083 
00084 #endif // UTIL_READ_COMPRESSED_H