Factor FDHandle out of the bytecode reader into the FileUtilities.h support
authorChris Lattner <sabre@nondot.org>
Mon, 29 Dec 2003 21:35:05 +0000 (21:35 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 29 Dec 2003 21:35:05 +0000 (21:35 +0000)
routines.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10642 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/FileUtilities.h
include/llvm/Support/FileUtilities.h
lib/Bytecode/Reader/ReaderWrappers.cpp
lib/Support/FileUtilities.cpp

index 4c02605400a07ee8d1c304a2742354b4cdd89f65..a6a1dc2456ffb8b3da0096dbe542cec3abf5da72 100644 (file)
@@ -102,6 +102,27 @@ bool MakeFileExecutable (const std::string & Filename);
 ///
 bool MakeFileReadable (const std::string & Filename);
 
+
+/// FDHandle - Simple handle class to make sure a file descriptor gets closed
+/// when the object is destroyed.
+///
+class FDHandle {
+  int FD;
+  FDHandle(const FDHandle &);      // DO NOT IMPLEMENT
+  void operator=(const FDHandle&); // DO NOT IMPLEMENT
+public:
+  FDHandle() : FD(-1) {}
+  FDHandle(int fd) : FD(fd) {}
+  ~FDHandle();
+
+  operator int() const { return FD; }
+
+  FDHandle &operator=(int fd);
+
+  /// take - Take ownership of the file descriptor away from the FDHandle
+  /// object, so that the file is not closed when the FDHandle is destroyed.
+  int take();
+};
 } // End llvm namespace
 
 #endif
index 4c02605400a07ee8d1c304a2742354b4cdd89f65..a6a1dc2456ffb8b3da0096dbe542cec3abf5da72 100644 (file)
@@ -102,6 +102,27 @@ bool MakeFileExecutable (const std::string & Filename);
 ///
 bool MakeFileReadable (const std::string & Filename);
 
+
+/// FDHandle - Simple handle class to make sure a file descriptor gets closed
+/// when the object is destroyed.
+///
+class FDHandle {
+  int FD;
+  FDHandle(const FDHandle &);      // DO NOT IMPLEMENT
+  void operator=(const FDHandle&); // DO NOT IMPLEMENT
+public:
+  FDHandle() : FD(-1) {}
+  FDHandle(int fd) : FD(fd) {}
+  ~FDHandle();
+
+  operator int() const { return FD; }
+
+  FDHandle &operator=(int fd);
+
+  /// take - Take ownership of the file descriptor away from the FDHandle
+  /// object, so that the file is not closed when the FDHandle is destroyed.
+  int take();
+};
 } // End llvm namespace
 
 #endif
index 291ad87344363eb815c5ce93075ae607891d76e4..bc3cbbd330938765cf92e3d730517514f0949542 100644 (file)
 #include "ReaderInternals.h"
 #include "llvm/Module.h"
 #include "llvm/Instructions.h"
+#include "Support/FileUtilities.h"
 #include "Support/StringExtras.h"
 #include "Config/fcntl.h"
-#include <sys/stat.h>
-#include <cerrno>
 #include "Config/unistd.h"
 #include "Config/sys/mman.h"
+#include <sys/stat.h>
+#include <cerrno>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -29,19 +30,6 @@ using namespace llvm;
 //
 
 namespace {
-  /// FDHandle - Simple handle class to make sure a file descriptor gets closed
-  /// when the object is destroyed.
-  ///
-  class FDHandle {
-    int FD;
-  public:
-    FDHandle(int fd) : FD(fd) {}
-    operator int() const { return FD; }
-    ~FDHandle() {
-      if (FD != -1) close(FD);
-    }
-  };
-
   /// BytecodeFileReader - parses a bytecode file from a file
   ///
   class BytecodeFileReader : public BytecodeParser {
@@ -63,7 +51,7 @@ static std::string ErrnoMessage (int savedErrNum, std::string descr) {
 }
 
 BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
-  FDHandle FD = open(Filename.c_str(), O_RDONLY);
+  FDHandle FD(open(Filename.c_str(), O_RDONLY));
   if (FD == -1)
     throw ErrnoMessage(errno, "open '" + Filename + "'");
 
index e6abc8fb40e181464cb947521e2890c5d2e252f9..02b4edd557324260e9d56f15f2ff961f3884b934 100644 (file)
@@ -194,3 +194,26 @@ bool llvm::MakeFileExecutable(const std::string &Filename) {
 bool llvm::MakeFileReadable(const std::string &Filename) {
   return AddPermissionsBits(Filename, 0444);
 }
+
+//===----------------------------------------------------------------------===//
+// FDHandle class implementation
+//
+
+FDHandle::~FDHandle() {
+  if (FD != -1) close(FD);
+}
+
+FDHandle &FDHandle::operator=(int fd) {
+  if (FD != -1) close(FD);
+  FD = fd;
+  return *this;
+}
+
+
+/// take - Take ownership of the file descriptor away from the FDHandle
+/// object, so that the file is not closed when the FDHandle is destroyed.
+int FDHandle::take() {
+  int Ret = FD;
+  FD = -1;
+  return Ret;
+}