Remove open_ member from BufferedFile and GZipFile

Just checking fp_ is sufficient.
pull/97/head
Tatsuhiro Tsujikawa 2013-05-23 23:28:08 +09:00
parent b0799b6e46
commit bc3b162569
4 changed files with 24 additions and 27 deletions

View File

@ -50,12 +50,11 @@ BufferedFile::BufferedFile(const char* filename, const char* mode)
#else // !__MINGW32__ #else // !__MINGW32__
fp_(a2fopen(filename, mode)), fp_(a2fopen(filename, mode)),
#endif // !__MINGW32__ #endif // !__MINGW32__
open_(fp_),
supportsColor_(fp_ ? isatty(fileno(fp_)) : false) supportsColor_(fp_ ? isatty(fileno(fp_)) : false)
{} {}
BufferedFile::BufferedFile(FILE* fp) BufferedFile::BufferedFile(FILE* fp)
: fp_(fp), open_(fp_), supportsColor_(fp_ ? isatty(fileno(fp_)) : false) : fp_(fp), supportsColor_(fp_ ? isatty(fileno(fp_)) : false)
{} {}
BufferedFile::~BufferedFile() BufferedFile::~BufferedFile()
@ -80,11 +79,12 @@ char* BufferedFile::onGets(char* s, int size)
int BufferedFile::onClose() int BufferedFile::onClose()
{ {
if (open_) { int rv = 0;
open_ = false; if (fp_) {
return fclose(fp_); rv = fclose(fp_);
fp_ = 0;
} }
return 0; return rv;
} }
int BufferedFile::onVprintf(const char* format, va_list va) int BufferedFile::onVprintf(const char* format, va_list va)
@ -114,7 +114,7 @@ bool BufferedFile::isEOF() const
bool BufferedFile::isOpen() const bool BufferedFile::isOpen() const
{ {
return open_; return fp_;
} }
} // namespace aria2 } // namespace aria2

View File

@ -69,7 +69,6 @@ private:
BufferedFile& operator=(const BufferedFile&); BufferedFile& operator=(const BufferedFile&);
FILE* fp_; FILE* fp_;
bool open_;
bool supportsColor_; bool supportsColor_;
}; };

View File

@ -44,7 +44,7 @@
namespace aria2 { namespace aria2 {
GZipFile::GZipFile(const char* filename, const char* mode) GZipFile::GZipFile(const char* filename, const char* mode)
: fp_(0), open_(false), : fp_(0),
buflen_(1024), buf_(reinterpret_cast<char*>(malloc(buflen_))) buflen_(1024), buf_(reinterpret_cast<char*>(malloc(buflen_)))
{ {
FILE* fp = FILE* fp =
@ -54,22 +54,21 @@ GZipFile::GZipFile(const char* filename, const char* mode)
a2fopen(filename, mode); a2fopen(filename, mode);
#endif // !__MINGW32__ #endif // !__MINGW32__
open_ = fp; if (fp) {
if (open_) {
int fd = dup(fileno(fp)); int fd = dup(fileno(fp));
if ((open_ = (fd >= 0))) { if (fd != -1) {
open_ = (fp_ = gzdopen(fd, mode)); fp_ = gzdopen(fd, mode);
if (!open_) { if (fp_) {
::close(fd); // fp_ retains fd and gzclose() will close fd as well.
}
}
if (open_) {
#if HAVE_GZBUFFER #if HAVE_GZBUFFER
gzbuffer(fp_, 1<<17); gzbuffer(fp_, 1<<17);
#endif #endif
#if HAVE_GZSETPARAMS #if HAVE_GZSETPARAMS
gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY); gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY);
#endif #endif
} else {
::close(fd);
}
} }
fclose(fp); fclose(fp);
} }
@ -83,11 +82,12 @@ GZipFile::~GZipFile()
int GZipFile::onClose() int GZipFile::onClose()
{ {
if (open_) { int rv = 0;
open_ = false; if (fp_) {
return gzclose(fp_); rv = gzclose(fp_);
fp_ = 0;
} }
return 0; return rv;
} }
bool GZipFile::onSupportsColor() bool GZipFile::onSupportsColor()
@ -109,7 +109,7 @@ bool GZipFile::isEOF() const
bool GZipFile::isOpen() const bool GZipFile::isOpen() const
{ {
return open_; return fp_;
} }
size_t GZipFile::onRead(void* ptr, size_t count) size_t GZipFile::onRead(void* ptr, size_t count)

View File

@ -63,8 +63,6 @@ private:
GZipFile& operator=(const GZipFile&); GZipFile& operator=(const GZipFile&);
gzFile fp_; gzFile fp_;
bool open_;
size_t buflen_; size_t buflen_;
char* buf_; char* buf_;
}; };