From c8d9cc7e5b72354a95c46215e5d16d29163af0ac Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 13 Aug 2017 00:37:50 -0700 Subject: [PATCH] Add filename constructor to raw_fd_istream. --- src/main/native/cpp/support/raw_istream.cpp | 24 +++++++++++++++++++ src/main/native/include/support/raw_istream.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/src/main/native/cpp/support/raw_istream.cpp b/src/main/native/cpp/support/raw_istream.cpp index 5b5c234743..382ed835b3 100644 --- a/src/main/native/cpp/support/raw_istream.cpp +++ b/src/main/native/cpp/support/raw_istream.cpp @@ -16,6 +16,7 @@ #include #endif +#include "llvm/FileSystem.h" #include "llvm/SmallVector.h" #include "llvm/StringRef.h" @@ -61,6 +62,29 @@ void raw_mem_istream::read_impl(void* data, std::size_t len) { m_left -= len; } +static int getFD(llvm::StringRef Filename, std::error_code &EC) { + // Handle "-" as stdin. Note that when we do this, we consider ourself + // the owner of stdin. This means that we can do things like close the + // file descriptor when we're done and set the "binary" flag globally. + if (Filename == "-") { + EC = std::error_code(); + return STDIN_FILENO; + } + + int FD; + + EC = llvm::sys::fs::openFileForRead(Filename, FD); + if (EC) + return -1; + + EC = std::error_code(); + return FD; +} + +raw_fd_istream::raw_fd_istream(llvm::StringRef filename, std::error_code& ec, + std::size_t bufSize) + : raw_fd_istream(getFD(filename, ec), true, bufSize) {} + raw_fd_istream::raw_fd_istream(int fd, bool shouldClose, std::size_t bufSize) : m_bufSize(bufSize), m_fd(fd), m_shouldClose(shouldClose) { m_cur = m_end = m_buf = static_cast(std::malloc(bufSize)); diff --git a/src/main/native/include/support/raw_istream.h b/src/main/native/include/support/raw_istream.h index 49aeffde25..3c2d18e40a 100644 --- a/src/main/native/include/support/raw_istream.h +++ b/src/main/native/include/support/raw_istream.h @@ -10,6 +10,7 @@ #include #include +#include namespace llvm { template @@ -92,6 +93,8 @@ class raw_mem_istream : public raw_istream { class raw_fd_istream : public raw_istream { public: + raw_fd_istream(llvm::StringRef filename, std::error_code& ec, + std::size_t bufSize = 4096); raw_fd_istream(int fd, bool shouldClose, std::size_t bufSize = 4096); ~raw_fd_istream() override; void close() override;