/** * @file include/ActivMiddleware/ActivBase/ZlibFile.h * * @brief Header file for the zlib file class. */ #if (!defined ACTIV_ZLIB_FILE_H) #define ACTIV_ZLIB_FILE_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/FileTypeHelper.h" struct gzFile_s; typedef struct gzFile_s *gzFile; //! Activ namespace. namespace Activ { /* * @brief Zlib file class for zlib compression. */ class ZlibFile ACTIV_FINAL : public FileTypeHelper { ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ZlibFile) public: typedef FileTypeHelper Parent; ///< The parent type definiton. static const FileType FILE_TYPE = FILE_TYPE_ZLIB; ///< The file type. static const uint8_t HEADER_SIZE = 2; ///< The file header size. /** * @brief Default constructor. */ ACTIV_BASE_API ZlibFile(); /** * @brief Constructor. * * @param compressionLevel the compression level */ ACTIV_BASE_API ZlibFile(const int32_t compressionLevel); /** * @brief Move constructor. * * @param rhs transfer ownership of the underlying file managed by @a rhs to this. */ ZlibFile(ACTIV_BOOST_RV_REF(ZlibFile) rhs) : Parent(ACTIV_BOOST_MOVE_BASE(Parent, rhs)), m_compressionLevel(rhs.m_compressionLevel), m_pGzFile(rhs.m_pGzFile) { // Inline here for G++. rhs.m_pGzFile = ACTIV_NULLPTR; } /** * @brief Destructor. */ ACTIV_BASE_API virtual ~ZlibFile() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Create(const String& fileName, const AccessFlags::Type accessFlags, const ShareFlags::Type shareFlags, const bool shouldFailIfExists = false) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Open(const String& fileName, const AccessFlags::Type accessFlags, const ShareFlags::Type shareFlags) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Close() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Flush() ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode GetDecodedPosition(uint64_t& position) const ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode SetPosition(int64_t position, const SeekMethod seekMethod = SEEK_METHOD_BEGIN) ACTIV_OVERRIDE; /** * @brief Set the zlib parameters. * * @param compressionLevel the compressionLevel. * * @retval STATUS_CODE_SUCCESS * @retval ... */ ACTIV_BASE_API StatusCode SetParameters(const CompressionLevel compressionLevel); /** * @brief Move assignment operator. * * Transfer ownership of a file from another ZlibFile object to this. * * If this object has a file open, it is closed. The underlying file associated with rhs is transferred to this object, * and no file is then associated with rhs. * * @param rhs the file to transfer to this object. * * @return *this */ ZlibFile &operator=(ACTIV_BOOST_RV_REF(ZlibFile) rhs) { // Inline here for G++. if (this != &rhs) { Parent::operator=(ACTIV_BOOST_MOVE_BASE(Parent, rhs)); m_compressionLevel = rhs.m_compressionLevel; m_pGzFile = rhs.m_pGzFile; rhs.m_pGzFile = ACTIV_NULLPTR; } return *this; } /** * @brief Check if the header contains the gz file header. * First two bytes of a gzip file are 0x1f and 0x8b. * * @param pHeader the pointer to the header. * @param headerLength the length of the header. * * @return whether the header matches the codec. */ ACTIV_BASE_API static bool IsFileType(const void *pHeader, const size_t headerLength); /** * @brief Get the zlib file extension. * * @return the zlib file extension. */ ACTIV_BASE_API static const String &GetFileExtension(); protected: ACTIV_BASE_API virtual StatusCode Decode(void *pBuffer, const size_t nBytesToRead, size_t &nBytesRead) ACTIV_OVERRIDE; ACTIV_BASE_API virtual StatusCode Encode(const void *pBuffer, const size_t nBytesToWrite, size_t &nBytesWritten) ACTIV_OVERRIDE; private: /** * @brief Convert Activ compression level to zlib compression level. * * @param compressionLevel the compressionLevel * * @return zlib compression level. */ CompressionLevel ConvertCompressionLevel(const CompressionLevel compressionLevel); /** * @brief Initialize zlib. * * @return a reference to this object. */ ACTIV_BASE_API StatusCode InitializeZlib(); /** * @brief Return last gzlib error StatusCode. * * @return converted StatusCode. */ StatusCode LastGzipError() const; int32_t m_compressionLevel; ///< The compression level. gzFile m_pGzFile; ///< The gz handle. }; } // namespace Activ #endif // (!defined ACTIV_ZLIB_FILE_H)