muduo之GzipFile
生活随笔
收集整理的這篇文章主要介紹了
muduo之GzipFile
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ??GzipFile封裝了gzip的一些API,記錄一下
// Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com)#pragma once#include "muduo/base/StringPiece.h" #include "muduo/base/noncopyable.h" #include <zlib.h>namespace muduo {class GzipFile : noncopyable {public:GzipFile(GzipFile&& rhs) noexcept: file_(rhs.file_){rhs.file_ = NULL;}~GzipFile(){if (file_){::gzclose(file_);}}GzipFile& operator=(GzipFile&& rhs) noexcept{swap(rhs);return *this;}bool valid() const { return file_ != NULL; }void swap(GzipFile& rhs) { std::swap(file_, rhs.file_); } #if ZLIB_VERNUM >= 0x1240bool setBuffer(int size) { return ::gzbuffer(file_, size) == 0; } #endif// return the number of uncompressed bytes actually read, 0 for eof, -1 for errorint read(void* buf, int len) { return ::gzread(file_, buf, len); }// return the number of uncompressed bytes actually writtenint write(StringPiece buf) { return ::gzwrite(file_, buf.data(), buf.size()); }// number of uncompressed bytesoff_t tell() const { return ::gztell(file_); }#if ZLIB_VERNUM >= 0x1240// number of compressed bytesoff_t offset() const { return ::gzoffset(file_); } #endif// int flush(int f) { return ::gzflush(file_, f); }static GzipFile openForRead(StringArg filename){return GzipFile(::gzopen(filename.c_str(), "rbe"));}static GzipFile openForAppend(StringArg filename){return GzipFile(::gzopen(filename.c_str(), "abe"));}static GzipFile openForWriteExclusive(StringArg filename){return GzipFile(::gzopen(filename.c_str(), "wbxe"));}static GzipFile openForWriteTruncate(StringArg filename){return GzipFile(::gzopen(filename.c_str(), "wbe"));}private:explicit GzipFile(gzFile file): file_(file){}gzFile file_; };} // namespace muduo?
總結(jié)
以上是生活随笔為你收集整理的muduo之GzipFile的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: muduo之LogFile
- 下一篇: muduo之mutex和conditio