/* */ #include "GZipFile.h" #include #include #include "a2io.h" #include "util.h" namespace aria2 { GZipFile::GZipFile(const char* filename, const char* mode) : BufferedFile(0), fp_(0), open_(false) { FILE* fp = #ifdef __MINGW32__ a2fopen(utf8ToWChar(filename).c_str(), utf8ToWChar(mode).c_str()); #else // !__MINGW32__ a2fopen(filename, mode); #endif // !__MINGW32__ open_ = fp; if (open_) { int fd = dup(fileno(fp)); if ((open_ = fd) >= 0) { open_ = (fp_ = gzdopen(fd, mode)); if (!open_) { ::close(fd); } } if (open_) { #if HAVE_GZBUFFER gzbuffer(fp_, 1<<17); #endif #if HAVE_GZSETPARAMS gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY); #endif } fclose(fp); } } int GZipFile::close() { if (open_) { open_ = false; return gzclose(fp_); } return 0; } bool GZipFile::isError() const { int rv = 0; const char *e = gzerror(fp_, &rv); return (e != 0 && *e != 0) || rv != 0; } size_t GZipFile::read(void* ptr, size_t count) { char *data = reinterpret_cast(ptr); size_t read = 0; while (count) { size_t len = std::min(count, (size_t)std::numeric_limits::max()); int rv = gzread(fp_, data, len); if (rv <= 0) { break; } count -= rv; read += rv; data += rv; } return read; } size_t GZipFile::write(const void* ptr, size_t count) { const char *data = reinterpret_cast(ptr); size_t written = 0; while (count) { size_t len = std::min(count, (size_t)std::numeric_limits::max()); int rv = gzwrite(fp_, data, len); if (rv <= 0) { break; } count -= rv; written += rv; data += rv; } return written; } char* GZipFile::gets(char* s, int size) { return gzgets(fp_, s, size); } int GZipFile::flush() { return gzflush(fp_, 0); } int GZipFile::vprintf(const char* format, va_list va) { char *buf = 0; size_t len; int rv = ::vasprintf(&buf, format, va); if (rv <= 0) { // buf is undefined at this point // do not attempt to free it return rv; } len = strlen(buf); if (len) { rv = gzwrite(fp_, buf, len); } if (buf) { free(buf); } return rv; } } // namespace aria2