Rewrite MemoryBuffer::getSTDIN to use read(2) and a SmallVector buffer.

llvm-svn: 106856
This commit is contained in:
Benjamin Kramer 2010-06-25 16:07:18 +00:00
parent 2dc70bea54
commit 58e6c2eded
1 changed files with 15 additions and 15 deletions

View File

@ -272,26 +272,26 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
//===----------------------------------------------------------------------===//
MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) {
char Buffer[4096*4];
std::vector<char> FileData;
// Read in all of the data from stdin, we cannot mmap stdin.
//
// FIXME: That isn't necessarily true, we should try to mmap stdin and
// fallback if it fails.
sys::Program::ChangeStdinToBinary();
size_t ReadBytes;
do {
ReadBytes = fread(Buffer, sizeof(char), sizeof(Buffer), stdin);
FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
} while (ReadBytes == sizeof(Buffer));
if (!feof(stdin)) {
if (ErrStr) *ErrStr = "error reading from stdin";
const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> Buffer;
ssize_t ReadBytes;
// Read into Buffer until we hit EOF.
do {
Buffer.reserve(Buffer.size() + ChunkSize);
ReadBytes = read(0, Buffer.end(), ChunkSize);
if (ReadBytes == -1) {
if (errno == EINTR) continue;
if (ErrStr) *ErrStr = sys::StrError();
return 0;
}
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end().
return getMemBufferCopy(StringRef(&FileData[0],FileData.size()-1), "<stdin>");
return getMemBufferCopy(Buffer, "<stdin>");
}