mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Add filename constructor to raw_fd_istream.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include <unistd.h>
|
||||
#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<char*>(std::malloc(bufSize));
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <system_error>
|
||||
|
||||
namespace llvm {
|
||||
template <typename T>
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user