llvm.org GIT mirror llvm / 7cf7054
add a MemoryBuffer::getOpenFile method, which turns an open file descriptor into a MemoryBuffer (and closes the FD). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120065 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 16 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
5959 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
6060 /// specified, this means that the client knows that the file exists and that
6161 /// it has the specified size.
62 static MemoryBuffer *getFile(StringRef Filename,
63 std::string *ErrStr = 0,
62 static MemoryBuffer *getFile(StringRef Filename, std::string *ErrStr = 0,
6463 int64_t FileSize = -1);
65 static MemoryBuffer *getFile(const char *Filename,
66 std::string *ErrStr = 0,
64 static MemoryBuffer *getFile(const char *Filename, std::string *ErrStr = 0,
6765 int64_t FileSize = -1);
6866
67 /// getOpenFile - Given an already-open file descriptor, read the file and
68 /// return a MemoryBuffer. This takes ownership of the descriptor,
69 /// immediately closing it after reading the file.
70 static MemoryBuffer *getOpenFile(int FD, const char *Filename,
71 std::string *ErrStr = 0,
72 int64_t FileSize = -1);
73
6974 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
7075 /// that InputData must be null terminated.
7176 static MemoryBuffer *getMemBuffer(StringRef InputData,
186186
187187 MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
188188 int64_t FileSize) {
189 // Ensure the path is null terminated.
189190 SmallString<256> PathBuf(Filename.begin(), Filename.end());
190191 return MemoryBuffer::getFile(PathBuf.c_str(), ErrStr, FileSize);
191192 }
201202 if (ErrStr) *ErrStr = sys::StrError();
202203 return 0;
203204 }
205
206 return getOpenFile(FD, Filename, ErrStr, FileSize);
207 }
208
209 MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
210 std::string *ErrStr, int64_t FileSize) {
204211 FileCloser FC(FD); // Close FD on return.
205212
206213 // If we don't know the file size, use fstat to find out. fstat on an open