Browse Source

Remove open_ member from BufferedFile and GZipFile

Just checking fp_ is sufficient.
Tatsuhiro Tsujikawa 12 years ago
parent
commit
bc3b162569
4 changed files with 24 additions and 27 deletions
  1. 7 7
      src/BufferedFile.cc
  2. 0 1
      src/BufferedFile.h
  3. 17 17
      src/GZipFile.cc
  4. 0 2
      src/GZipFile.h

+ 7 - 7
src/BufferedFile.cc

@@ -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_) {
-    open_ = false;
-    return fclose(fp_);
+  int rv = 0;
+  if (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

+ 0 - 1
src/BufferedFile.h

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

+ 17 - 17
src/GZipFile.cc

@@ -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 (open_) {
+  if (fp) {
     int fd = dup(fileno(fp));
     int fd = dup(fileno(fp));
-    if ((open_ = (fd >= 0))) {
-      open_ = (fp_ = gzdopen(fd, mode));
-      if (!open_) {
-        ::close(fd);
-      }
-    }
-    if (open_) {
+    if (fd != -1) {
+      fp_ = gzdopen(fd, mode);
+      if (fp_) {
+        // fp_ retains fd and gzclose() will close fd as well.
 #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_) {
-    open_ = false;
-    return gzclose(fp_);
+  int rv = 0;
+  if (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)

+ 0 - 2
src/GZipFile.h

@@ -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_;
 };
 };